Skip to content

Commit

Permalink
πŸ”¨ Refactor/#173 - μ’‹μ•„μš” μˆ˜μ™€ λŒ“κΈ€ 수λ₯Ό Board의 ν•„λ“œλ‘œ μΆ”κ°€ 및, ν•„λ“œμ—μ„œ κ°’ μ½μ–΄μ˜€λ„λ‘ 둜직 λ³€κ²½
Browse files Browse the repository at this point in the history
  • Loading branch information
dongkyeomjang committed Nov 27, 2024
1 parent 1f87319 commit 6f8e7ae
Show file tree
Hide file tree
Showing 12 changed files with 1,872 additions and 42 deletions.
903 changes: 903 additions & 0 deletions http/ConcurrencyIssueTestHttpRequest.http

Large diffs are not rendered by default.

903 changes: 903 additions & 0 deletions http/ConcurrencyIssueTestHttpRequest2.http

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion http/onjung/OnjungControllerHttpRequest.http
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ Authorization: Bearer {{access_token}}
Content-Type: application/json

{
"event_id": {{onjung.API_4_7.event_id}},
"donation_amount": {{onjung.API_4_7.donation_amount}}
}

Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ public BoardInfoDto(Long id, String imgUrl, String title, String content, String

public static BoardInfoDto of(
Board board,
Integer likeCount,
Integer commentCount,
Boolean isLiked
) {
return BoardInfoDto.builder()
Expand All @@ -136,17 +134,17 @@ public static BoardInfoDto of(
.title(board.getTitle())
.content(board.getContent())
.postedAgo(DateTimeUtil.calculatePostedAgo(board.getCreatedAt()))
.likeCount(likeCount)
.commentCount(commentCount)
.likeCount(board.getLikeCount())
.commentCount(board.getCommentCount())
.isLiked(isLiked)
.build();
}
}

public static ReadBoardDetailResponseDto of(Board board, User user, Boolean isMe, Integer likeCount, Integer commentCount, Boolean isLiked) {
public static ReadBoardDetailResponseDto of(Board board, User user, Boolean isMe, Boolean isLiked) {
return ReadBoardDetailResponseDto.builder()
.writerInfo(WriterInfoDto.of(user, isMe))
.boardInfo(BoardInfoDto.of(board, likeCount, commentCount, isLiked))
.boardInfo(BoardInfoDto.of(board, isLiked))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,15 @@ public BoardListDto(Long id, String imgUrl, String titleSummary, String contentS
this.validateSelf();
}

public static BoardListDto of(
Board board,
Integer likeCount,
Integer commentCount
) {
public static BoardListDto fromEntity(Board board) {
return BoardListDto.builder()
.id(board.getId())
.imgUrl(board.getImgUrl())
.titleSummary(board.getTitle().length() > 15 ? board.getTitle().substring(0, 15) + "..." : board.getTitle())
.contentSummary(board.getContent().length() > 30 ? board.getContent().substring(0, 30) + "..." : board.getContent())
.postedAgo(DateTimeUtil.calculatePostedAgo(board.getCreatedAt()))
.likeCount(likeCount)
.commentCount(commentCount)
.likeCount(board.getLikeCount())
.commentCount(board.getCommentCount())
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.daon.onjung.suggestion.application.usecase.CreateCommentUseCase;
import com.daon.onjung.suggestion.domain.Board;
import com.daon.onjung.suggestion.domain.Comment;
import com.daon.onjung.suggestion.domain.service.BoardService;
import com.daon.onjung.suggestion.domain.service.CommentService;
import com.daon.onjung.suggestion.repository.mysql.BoardRepository;
import com.daon.onjung.suggestion.repository.mysql.CommentRepository;
Expand All @@ -26,6 +27,7 @@ public class CreateCommentService implements CreateCommentUseCase {
private final CommentRepository commentRepository;

private final CommentService commentService;
private final BoardService boardService;

@Override
@Transactional
Expand All @@ -46,5 +48,9 @@ public void execute(UUID accountId, Long boardId, CreateCommentRequestDto reques
board
);
commentRepository.save(comment);

// κ²Œμ‹œκΈ€ λŒ“κΈ€ 수 증가
board = boardService.addCommentCount(board);
boardRepository.save(board);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.daon.onjung.suggestion.application.usecase.CreateOrDeleteLikeUseCase;
import com.daon.onjung.suggestion.domain.Board;
import com.daon.onjung.suggestion.domain.Like;
import com.daon.onjung.suggestion.domain.service.BoardService;
import com.daon.onjung.suggestion.domain.service.LikeService;
import com.daon.onjung.suggestion.repository.mysql.BoardRepository;
import com.daon.onjung.suggestion.repository.mysql.LikeRepository;
Expand All @@ -23,8 +24,11 @@ public class CreateOrDeleteLikeService implements CreateOrDeleteLikeUseCase {
private final BoardRepository boardRepository;
private final UserRepository userRepository;
private final LikeRepository likeRepository;

private final BoardService boardService;
private final LikeService likeService;


@Override
@Transactional
public Boolean execute(UUID accountId, Long boardId) {
Expand All @@ -43,10 +47,24 @@ public Boolean execute(UUID accountId, Long boardId) {

// μ’‹μ•„μš” 생성 λ˜λŠ” μ‚­μ œ
if (like != null) {

// μ’‹μ•„μš”κ°€ 이미 μ‘΄μž¬ν•˜λ©΄ μ‚­μ œ
likeRepository.delete(like);

// κ²Œμ‹œκΈ€ μ’‹μ•„μš” 수 κ°μ†Œ
board = boardService.subtractLikeCount(board);
boardRepository.save(board);

return false;
} else {

// μ’‹μ•„μš”κ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠμœΌλ©΄ 생성
likeRepository.save(likeService.createLike(user,board));

// κ²Œμ‹œκΈ€ μ’‹μ•„μš” 수 증가
board = boardService.addLikeCount(board);
boardRepository.save(board);

return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.daon.onjung.suggestion.application.usecase.ReadBoardDetailUseCase;
import com.daon.onjung.suggestion.domain.Board;
import com.daon.onjung.suggestion.repository.mysql.BoardRepository;
import com.daon.onjung.suggestion.repository.mysql.CommentRepository;
import com.daon.onjung.suggestion.repository.mysql.LikeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -22,7 +21,6 @@ public class ReadBoardDetailService implements ReadBoardDetailUseCase {

private final BoardRepository boardRepository;
private final LikeRepository likeRepository;
private final CommentRepository commentRepository;
private final UserRepository userRepository;

@Override
Expand All @@ -37,12 +35,6 @@ public ReadBoardDetailResponseDto execute(UUID accountId, Long boardId) {
Board board = boardRepository.findById(boardId)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE));

// μ’‹μ•„μš” 개수 쑰회
Integer likeCount = likeRepository.countByBoard(board);

// λŒ“κΈ€ 개수 쑰회
Integer commentCount = commentRepository.countByBoard(board);

// μ’‹μ•„μš” μ—¬λΆ€ 쑰회
Boolean isLiked = likeRepository.existsByBoardAndUser(board, user);

Expand All @@ -56,8 +48,6 @@ public ReadBoardDetailResponseDto execute(UUID accountId, Long boardId) {
board,
writer,
isMe,
likeCount,
commentCount,
isLiked
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.daon.onjung.suggestion.application.usecase.ReadBoardOverviewUseCase;
import com.daon.onjung.suggestion.domain.Board;
import com.daon.onjung.suggestion.repository.mysql.BoardRepository;
import com.daon.onjung.suggestion.repository.mysql.CommentRepository;
import com.daon.onjung.suggestion.repository.mysql.LikeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -20,8 +18,6 @@
public class ReadBoardOverviewService implements ReadBoardOverviewUseCase {

private final BoardRepository boardRepository;
private final CommentRepository commentRepository;
private final LikeRepository likeRepository;

@Override
@Transactional(readOnly = true)
Expand All @@ -31,21 +27,9 @@ public ReadBoardOverviewResponseDto execute(Integer page, Integer size) {
Pageable pageable = PageRequest.of(page-1, size);
Page<Board> boardsPage = boardRepository.findAllByOrderByCreatedAtDesc(pageable);

// BoardListDto 생성
List<ReadBoardOverviewResponseDto.BoardListDto> boardList = boardsPage.getContent().stream()
.map(board -> {

// 각 κ²Œμ‹œκΈ€μ— λŒ€ν•΄ λŒ“κΈ€ 수 쑰회
Integer commentCount = commentRepository.countByBoard(board);

// 각 κ²Œμ‹œκΈ€μ— λŒ€ν•΄ μ’‹μ•„μš” 수 쑰회
Integer likeCount = likeRepository.countByBoard(board);

// BoardListDto 생성
ReadBoardOverviewResponseDto.BoardListDto boardListDto =
ReadBoardOverviewResponseDto.BoardListDto.of(board, commentCount, likeCount);

return boardListDto;
})
.map(ReadBoardOverviewResponseDto.BoardListDto::fromEntity)
.toList();

// ResponseDto 생성
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/daon/onjung/suggestion/domain/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class Board {
@Column(name = "like_count", nullable = false)
private Integer likeCount;

@Column(name = "comment_count", nullable = false)
private Integer commentCount;

/* -------------------------------------------- */
/* Timestamp Column --------------------------- */
/* -------------------------------------------- */
Expand All @@ -58,10 +61,20 @@ public Board(String title, String content, User user) {
this.content = content;
this.user = user;
this.likeCount = 0;
this.commentCount = 0;
this.createdAt = LocalDateTime.now();
}

public void updateImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}

public void updateLikeCount(Integer likeCount) {
this.likeCount = likeCount;
}

public void
updateCommentCount(Integer commentCount) {
this.commentCount = commentCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,24 @@ public Board updateBoardFile(Board board, String imgUrl) {
board.updateImgUrl(imgUrl);
return board;
}

public Board addLikeCount(Board board) {
board.updateLikeCount(board.getLikeCount() + 1);
return board;
}

public Board subtractLikeCount(Board board) {
board.updateLikeCount(board.getLikeCount() - 1);
return board;
}

public Board addCommentCount(Board board) {
board.updateCommentCount(board.getCommentCount() + 1);
return board;
}

public Board subtractCommentCount(Board board) {
board.updateCommentCount(board.getCommentCount() - 1);
return board;
}
}

0 comments on commit 6f8e7ae

Please sign in to comment.