diff --git a/src/main/java/com/example/prdoit/controller/CommentController.java b/src/main/java/com/example/prdoit/controller/CommentController.java index 89bed6d..660c5a7 100644 --- a/src/main/java/com/example/prdoit/controller/CommentController.java +++ b/src/main/java/com/example/prdoit/controller/CommentController.java @@ -1,51 +1,100 @@ package com.example.prdoit.controller; import com.example.prdoit.dto.comment.*; +import com.example.prdoit.exception.CustomException; import com.example.prdoit.service.comment.CommentService; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController +@Slf4j @RequiredArgsConstructor -@RequestMapping("/api/comments") +@RequestMapping("content/comments") public class CommentController { private final CommentService commentService; - // 1. 댓글 조회 + // 1. 댓글 목록 조회 @GetMapping("/{contentId}") - public ResponseEntity> getComments(@PathVariable String contentId) { - return ResponseEntity.ok(commentService.getCommentList(contentId)); + public ResponseEntity getComments(@PathVariable String contentId) { + log.info("[CommentController] 댓글 목록 조회 시작 - Content ID: {}", contentId); + try { + List comments = commentService.getCommentList(contentId); + return ResponseEntity.ok(comments); + } catch (CustomException e) { + log.error("[CommentController] 댓글 조회 실패: {}", e.getMessage()); + return ResponseEntity.badRequest().body(e.getMessage()); + } catch (RuntimeException e) { + log.error("[CommentController] 서버 오류 발생: {}", e.getMessage()); + return ResponseEntity.internalServerError().body("댓글 목록 조회에 실패했습니다."); + } } // 2. 댓글 작성 @PostMapping - public ResponseEntity createComment(@RequestBody CommentRequestDto commentRequestDto) { - commentService.postComment(commentRequestDto); - return ResponseEntity.ok("댓글이 성공적으로 작성되었습니다!"); + public ResponseEntity createComment(@RequestBody CommentRequestDto commentRequestDto) { + log.info("[CommentController] 댓글 작성 시작 - Content ID: {}", commentRequestDto.getContentId()); + try { + commentService.postComment(commentRequestDto); + return ResponseEntity.ok("댓글이 성공적으로 작성되었습니다!"); + } catch (CustomException e) { + log.error("[CommentController] 댓글 작성 실패: {}", e.getMessage()); + return ResponseEntity.badRequest().body(e.getMessage()); + } catch (RuntimeException e) { + log.error("[CommentController] 서버 오류 발생: {}", e.getMessage()); + return ResponseEntity.internalServerError().body("댓글 작성에 실패했습니다."); + } } // 3. 대댓글 작성 @PostMapping("/reply") - public ResponseEntity createReply(@RequestBody CommentReplyRequestDto commentReplyRequestDto) { - commentService.postCommentReply(commentReplyRequestDto); - return ResponseEntity.ok("대댓글이 성공적으로 작성되었습니다!"); + public ResponseEntity createReply(@RequestBody CommentReplyRequestDto commentReplyRequestDto) { + log.info("[CommentController] 대댓글 작성 시작 - Comment ID: {}", commentReplyRequestDto.getCommentId()); + try { + commentService.postCommentReply(commentReplyRequestDto); + return ResponseEntity.ok("대댓글이 성공적으로 작성되었습니다!"); + } catch (CustomException e) { + log.error("[CommentController] 대댓글 작성 실패: {}", e.getMessage()); + return ResponseEntity.badRequest().body(e.getMessage()); + } catch (RuntimeException e) { + log.error("[CommentController] 서버 오류 발생: {}", e.getMessage()); + return ResponseEntity.internalServerError().body("대댓글 작성에 실패했습니다."); + } } // 4. 댓글 수정 @PatchMapping - public ResponseEntity updateComment(@RequestBody CommentPatchDto commentPatchDto) { - commentService.patchComment(commentPatchDto); - return ResponseEntity.ok("댓글이 성공적으로 수정되었습니다!"); + public ResponseEntity updateComment(@RequestBody CommentPatchDto commentPatchDto) { + log.info("[CommentController] 댓글 수정 시작 - Comment ID: {}", commentPatchDto.getCommentId()); + try { + commentService.patchComment(commentPatchDto); + return ResponseEntity.ok("댓글이 성공적으로 수정되었습니다!"); + } catch (CustomException e) { + log.error("[CommentController] 댓글 수정 실패: {}", e.getMessage()); + return ResponseEntity.badRequest().body(e.getMessage()); + } catch (RuntimeException e) { + log.error("[CommentController] 서버 오류 발생: {}", e.getMessage()); + return ResponseEntity.internalServerError().body("댓글 수정에 실패했습니다."); + } } - // 5.대댓글 수정 + // 5. 대댓글 수정 @PatchMapping("/reply") - public ResponseEntity updateReply(@RequestBody CommentReplyPatchDto commentReplyPatchDto) { - commentService.patchCommentReply(commentReplyPatchDto); - return ResponseEntity.ok("대댓글이 성공적으로 수정되었습니다!"); + public ResponseEntity updateReply(@RequestBody CommentReplyPatchDto commentReplyPatchDto) { + log.info("[CommentController] 대댓글 수정 시작 - CommentReply ID: {}", commentReplyPatchDto.getCommentReplyId()); + try { + commentService.patchCommentReply(commentReplyPatchDto); + return ResponseEntity.ok("대댓글이 성공적으로 수정되었습니다!"); + } catch (CustomException e) { + log.error("[CommentController] 대댓글 수정 실패: {}", e.getMessage()); + return ResponseEntity.badRequest().body(e.getMessage()); + } catch (RuntimeException e) { + log.error("[CommentController] 서버 오류 발생: {}", e.getMessage()); + return ResponseEntity.internalServerError().body("대댓글 수정에 실패했습니다."); + } } } diff --git a/src/main/java/com/example/prdoit/dto/comment/CommentResponseDto.java b/src/main/java/com/example/prdoit/dto/comment/CommentResponseDto.java index 8068c06..0a11b43 100644 --- a/src/main/java/com/example/prdoit/dto/comment/CommentResponseDto.java +++ b/src/main/java/com/example/prdoit/dto/comment/CommentResponseDto.java @@ -17,4 +17,6 @@ public class CommentResponseDto { private String commentNickname; // 작성자 닉네임 private LocalDateTime commentDate; // 댓글 작성 날짜 private List commentReplyResponseDtoList; // 대댓글 리스트 -} + private int isRead; // 읽음 여부 + private int isContent; // 내용 여부 +} \ No newline at end of file diff --git a/src/main/java/com/example/prdoit/service/comment/CommentServiceImpl.java b/src/main/java/com/example/prdoit/service/comment/CommentServiceImpl.java index 87f5b5e..ff15769 100644 --- a/src/main/java/com/example/prdoit/service/comment/CommentServiceImpl.java +++ b/src/main/java/com/example/prdoit/service/comment/CommentServiceImpl.java @@ -27,111 +27,153 @@ public class CommentServiceImpl implements CommentService { private final NotificationTableRepository notificationTableRepository; private final IdTableRepository idTableRepository; - // 1. 댓글 목록 조회 (GET) + + // 1. 댓글 목록 조회 (GET) - try문 추가 @Override public List getCommentList(String contentId) { - log.info("[getCommentList] 댓글 목록 조회 시작 - Content ID: {}", contentId); - - ContentTable contentTable = contentTableRepository.findById(contentId) - .orElseThrow(() -> new CustomException("해당 게시글이 없습니다. ID = " + contentId)); - - return contentTable.getCommentTable().stream() - .map(comment -> CommentResponseDto.builder() - .commentId(comment.getCommentId()) - .commentContent(comment.getCommentContent()) - .commentNickname(comment.getCommentNickname()) - .commentDate(comment.getCommentDate()) - .commentReplyResponseDtoList(comment.getCommentCommentTable().stream() - .map(reply -> CommentReplyResponseDto.builder() - .commentReplyId(reply.getCommentCommentId()) - .commentReplyContent(reply.getCommentCommentContent()) - .commentReplyNickname(reply.getUserNickname()) - .commentReplyDate(reply.getCommentCommentDate()) - .build()) - .collect(Collectors.toList())) - .build()) - .collect(Collectors.toList()); + log.info("[getCommentList] 시작 - Content ID: {}", contentId); + + try { + ContentTable contentTable = contentTableRepository.findById(contentId) + .orElseThrow(() -> new CustomException("해당 게시글이 없습니다. ID = " + contentId)); + + List comments = contentTable.getCommentTable().stream() + .map(comment -> CommentResponseDto.builder() + .commentId(comment.getCommentId()) + .commentContent(comment.getCommentContent()) + .commentNickname(comment.getCommentNickname()) + .commentDate(comment.getCommentDate()) + .commentReplyResponseDtoList(comment.getCommentCommentTable().stream() + .map(reply -> CommentReplyResponseDto.builder() + .commentReplyId(reply.getCommentCommentId()) + .commentReplyContent(reply.getCommentCommentContent()) + .commentReplyNickname(reply.getUserNickname()) + .commentReplyDate(reply.getCommentCommentDate()) + .build()) + .collect(Collectors.toList())) + .build()) + .collect(Collectors.toList()); + + log.info("[getCommentList] 성공 - Content ID: {}, 총 댓글 개수: {}", contentId, comments.size()); + return comments; + } catch (Exception e) { + log.error("[getCommentList] 실패 - Content ID: {}, 오류: {}", contentId, e.getMessage()); + throw new CustomException("댓글 목록 조회 중 오류가 발생했습니다."); + } } + // 2. 댓글 작성 (POST) @Override public void postComment(CommentRequestDto commentRequestDto) { - log.info("[postComment] 댓글 등록 시작 - Content ID: {}", commentRequestDto.getContentId()); - - ContentTable contentTable = contentTableRepository.findById(commentRequestDto.getContentId()) - .orElseThrow(() -> new CustomException("해당 게시글이 없습니다.")); - - commentTableRepository.save(CommentTable.builder() - .commentContent(commentRequestDto.getCommentContent()) - .commentNickname(commentRequestDto.getCommentNickname()) - .commentDate(commentRequestDto.getCommentDate()) - .contentId(contentTable) - .build()); - - notificationTableRepository.save(NotificationTable.builder() - .notificationContent("댓글이 작성되었습니다.") - .contentId(contentTable.getContentId()) - .userId(contentTable.getUserId()) - .isRead(0) - .isContent(1) - .build()); - - log.info("[postComment] 댓글 등록을 완료했습니다."); + log.info("[postComment] 시작 - Content ID: {}, 작성자: {}, 내용: {}", + commentRequestDto.getContentId(), + commentRequestDto.getCommentNickname(), + commentRequestDto.getCommentContent()); + + try { + ContentTable contentTable = contentTableRepository.findById(commentRequestDto.getContentId()) + .orElseThrow(() -> new CustomException("해당 게시글이 없습니다.")); + + CommentTable comment = commentTableRepository.save(CommentTable.builder() + .commentContent(commentRequestDto.getCommentContent()) + .commentNickname(commentRequestDto.getCommentNickname()) + .commentDate(commentRequestDto.getCommentDate()) + .contentId(contentTable) + .isRead(0) // 새 댓글은 당연히 아직 안 읽었지 + .isContent(1) // 내용이 있음 + .build()); + + log.info("[postComment] 성공 - Comment ID: {}, 작성자: {}, isRead: {}, isContent: {}", + comment.getCommentId(), comment.getCommentNickname(), comment.getIsRead(), comment.getIsContent()); + } catch (Exception e) { + log.error("[postComment] 실패 - Content ID: {}, 작성자: {}, 오류: {}", + commentRequestDto.getContentId(), + commentRequestDto.getCommentNickname(), + e.getMessage()); + throw new CustomException("댓글 등록 중 오류가 발생했습니다."); + } } + // 3. 대댓글 작성 (POST) @Override public void postCommentReply(CommentReplyRequestDto commentReplyRequestDto) { - log.info("[postCommentReply] 대댓글 등록 시작"); - - CommentTable parentComment = commentTableRepository.findById(commentReplyRequestDto.getCommentId()) - .orElseThrow(() -> new CustomException("해당 댓글이 없습니다.")); - - commentCommentTableRepository.save(CommentCommentTable.builder() - .commentCommentContent(commentReplyRequestDto.getCommentReplyContent()) - .userNickname(commentReplyRequestDto.getCommentReplyNickname()) - .commentCommentDate(commentReplyRequestDto.getCommentReplyDate()) - .commentId(parentComment) - .build()); - - notificationTableRepository.save(NotificationTable.builder() - .notificationContent("대댓글이 작성되었습니다.") - .contentId(parentComment.getContentId().getContentId()) - .userId(idTableRepository.findByNickname(parentComment.getCommentNickname())) - .isRead(0) - .isContent(1) - .build()); - - log.info("[postCommentReply] 대댓글 등록을 완료했습니다."); + log.info("[postCommentReply] 시작 - 원본 댓글 ID: {}, 작성자: {}, 내용: {}", + commentReplyRequestDto.getCommentId(), + commentReplyRequestDto.getCommentReplyNickname(), + commentReplyRequestDto.getCommentReplyContent()); + + try { + CommentTable parentComment = commentTableRepository.findById(commentReplyRequestDto.getCommentId()) + .orElseThrow(() -> new CustomException("해당 댓글이 없습니다.")); + + CommentCommentTable reply = commentCommentTableRepository.save(CommentCommentTable.builder() + .commentCommentContent(commentReplyRequestDto.getCommentReplyContent()) + .userNickname(commentReplyRequestDto.getCommentReplyNickname()) + .commentCommentDate(commentReplyRequestDto.getCommentReplyDate()) + .commentId(parentComment) + .isRead(0) // 새 대댓글도 아직 안 읽어야 맞음 + .isContent(1) // 내용이 있음 + .build()); + + log.info("[postCommentReply] 성공 - 대댓글 ID: {}, 원본 댓글 ID: {}, 작성자: {}, isRead: {}, isContent: {}", + reply.getCommentCommentId(), parentComment.getCommentId(), reply.getUserNickname(), reply.getIsRead(), reply.getIsContent()); + } catch (Exception e) { + log.error("[postCommentReply] 실패 - 원본 댓글 ID: {}, 작성자: {}, 오류: {}", + commentReplyRequestDto.getCommentId(), + commentReplyRequestDto.getCommentReplyNickname(), + e.getMessage()); + throw new CustomException("대댓글 등록 중 오류가 발생했습니다."); + } } + // 4. 댓글 수정 (PATCH) @Override public void patchComment(CommentPatchDto commentPatchDto) { - log.info("[patchComment] 댓글 수정 시작 - Comment ID: {}", commentPatchDto.getCommentId()); - - CommentTable comment = commentTableRepository.findById(commentPatchDto.getCommentId()) - .orElseThrow(() -> new CustomException("해당 댓글이 없습니다.")); - - comment.setCommentContent(commentPatchDto.getCommentContent()); - comment.setCommentDate(commentPatchDto.getCommentDate()); - commentTableRepository.save(comment); - - log.info("[patchComment] 댓글 수정이 완료되었습니다."); + log.info("[patchComment] 시작 - Comment ID: {}, 새 내용: {}", + commentPatchDto.getCommentId(), + commentPatchDto.getCommentContent()); + + try { + CommentTable comment = commentTableRepository.findById(commentPatchDto.getCommentId()) + .orElseThrow(() -> new CustomException("해당 댓글이 없습니다.")); + + comment.setCommentContent(commentPatchDto.getCommentContent()); + comment.setCommentDate(commentPatchDto.getCommentDate()); + comment.setIsRead(comment.getIsRead()); + comment.setIsContent(comment.getIsContent()); + commentTableRepository.save(comment); + + log.info("[patchComment] 성공 - 수정된 Comment ID: {}", comment.getCommentId()); + } catch (Exception e) { + log.error("[patchComment] 실패 - Comment ID: {}, 오류: {}", commentPatchDto.getCommentId(), e.getMessage()); + throw new CustomException("댓글 수정 중 오류가 발생했습니다."); + } } + // 5. 대댓글 수정 (PATCH) @Override public void patchCommentReply(CommentReplyPatchDto commentReplyPatchDto) { - log.info("[patchCommentReply] 대댓글 수정 시작 - CommentReply ID: {}", commentReplyPatchDto.getCommentReplyId()); - - CommentCommentTable reply = commentCommentTableRepository.findById(commentReplyPatchDto.getCommentReplyId()) - .orElseThrow(() -> new CustomException("해당 대댓글이 없습니다.")); - - reply.setCommentCommentContent(commentReplyPatchDto.getCommentReplyContent()); - reply.setCommentCommentDate(commentReplyPatchDto.getCommentReplyDate()); - commentCommentTableRepository.save(reply); - - log.info("[patchCommentReply] 대댓글 수정이 완료되었습니다."); + log.info("[patchCommentReply] 시작 - 대댓글 ID: {}, 새 내용: {}", + commentReplyPatchDto.getCommentReplyId(), + commentReplyPatchDto.getCommentReplyContent()); + + try { + CommentCommentTable reply = commentCommentTableRepository.findById(commentReplyPatchDto.getCommentReplyId()) + .orElseThrow(() -> new CustomException("해당 대댓글이 없습니다.")); + + reply.setCommentCommentContent(commentReplyPatchDto.getCommentReplyContent()); + reply.setCommentCommentDate(commentReplyPatchDto.getCommentReplyDate()); + commentCommentTableRepository.save(reply); + + log.info("[patchCommentReply] 성공 - 수정된 대댓글 ID: {}", reply.getCommentCommentId()); + } catch (Exception e) { + log.error("[patchCommentReply] 실패 - 대댓글 ID: {}, 오류: {}", commentReplyPatchDto.getCommentReplyId(), e.getMessage()); + throw new CustomException("대댓글 수정 중 오류가 발생했습니다."); + } } } +