Skip to content

Commit

Permalink
Feat: 댓글 수 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
resource777 committed Nov 3, 2023
1 parent 34e8b2c commit 89aba0b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 48 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class BoardResponse {
private final LocalDateTime createdAt;
private LocalDateTime updatedAt;
private List<CommentResponseDto> comments;
private Long likeCount;
private Long commentCount;

public BoardResponse(Board article) {
this.id = article.getId();
Expand All @@ -32,5 +34,7 @@ public BoardResponse(Board article) {
this.characterId = article.getCharacterId();
this.updatedAt = article.getUpdatedAt();
this.comments = article.getComments().stream().map(CommentResponseDto::new).collect(Collectors.toList());
this.likeCount = article.getLikeCount();
this.commentCount = article.getCommentCount();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ public class Board {
@Column(name = "updated_at")
private LocalDateTime updatedAt;

@OneToMany(mappedBy = "board", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "board", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@OrderBy("id asc") // 댓글 정렬
private List<Comment> comments;

private Long likeCount = 0L;
private Long commentCount = 0L;



@Builder // 빌더 패턴으로 객체 생성
public Board(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ResponseEntity<List<CommentResponseDto>> getComments(@PathVariable Long a
return ResponseEntity.ok(comments);
}

@PutMapping("/comments/{commentId}")
@PutMapping("/boards/{characterId}/{articleId}/comments/{commentId}")
public ResponseEntity<Long> updateComment(
@PathVariable Long commentId, @RequestBody CommentUpdateDto dto, @Login Claims claims) {
Long userId = claims.get("userId", Long.class);
Expand All @@ -62,15 +62,16 @@ public ResponseEntity<Long> updateComment(
return ResponseEntity.ok(updatedCommentId);
}

@DeleteMapping("/comments/{commentId}")
public ResponseEntity<Void> deleteComment(@PathVariable Long commentId, @Login Claims claims) {
@DeleteMapping("/boards/{characterId}/{articleId}/comments/{commentId}")
public ResponseEntity<Void> deleteComment(
@PathVariable Long commentId, @PathVariable Long articleId, @Login Claims claims) {
Long userId = claims.get("userId", Long.class);
if (!commentService.findById(commentId).getMember().getId().equals(userId)) {
log.error("댓글 작성자가 아닌 댓글 삭제 요청");
return ResponseEntity.badRequest()
.build();
}
commentService.deleteComment(commentId);
commentService.deleteComment(articleId, commentId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public Long commentSave(Long userId, Long articleId, CommentRequestDto dto) {
new IllegalArgumentException("댓글 쓰기 실패: 해당 유저가 존재하지 않습니다." + userId));
Board board = boardRepository.findById(articleId).orElseThrow(() ->
new IllegalArgumentException("댓글 쓰기 실패: 해당 게시글이 존재하지 않습니다." + articleId));
board.setCommentCount(board.getCommentCount() + 1);
dto.setMember(member);
dto.setBoard(board);
Comment comment = dto.toEntity();
Expand All @@ -55,9 +56,12 @@ public Long updateComment(Long commentId, CommentUpdateDto dto) {
}

@Transactional
public void deleteComment(Long commentId) {
public void deleteComment(Long articleId, Long commentId) {
Comment comment = commentRepository.findById(commentId).orElseThrow(() ->
new IllegalArgumentException("댓글 삭제 실패: 해당 댓글이 존재하지 않습니다." + commentId));
Board board = boardRepository.findById(articleId).orElseThrow(() ->
new IllegalArgumentException("댓글 삭제 실패: 해당 게시글이 존재하지 않습니다." + articleId));
board.setCommentCount(board.getCommentCount() - 1);
commentRepository.delete(comment);
}

Expand Down

0 comments on commit 89aba0b

Please sign in to comment.