From 9e2098943dcecaa0a4bcfa077097b38b70b13c6a Mon Sep 17 00:00:00 2001 From: dnjsals45 Date: Thu, 22 Feb 2024 19:41:46 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix/comment,=20contentMark=20=EC=9D=91?= =?UTF-8?q?=EB=8B=B5=20=EC=9D=BC=EB=B6=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/controller/CommentController.java | 8 ++--- .../api/comment/service/CommentService.java | 19 ++++++++--- .../api/mark/service/ContentMarkService.java | 14 ++++++-- .../coding/domain/comment/dto/CommentDTO.java | 4 +-- .../domain/comment/dto/GetCommentRes.java | 34 ++++++++++++++++--- .../content/repository/ContentRepository.java | 2 ++ 6 files changed, 64 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/oopy/coding/api/comment/controller/CommentController.java b/src/main/java/io/oopy/coding/api/comment/controller/CommentController.java index 368f5c3..f93f264 100644 --- a/src/main/java/io/oopy/coding/api/comment/controller/CommentController.java +++ b/src/main/java/io/oopy/coding/api/comment/controller/CommentController.java @@ -39,21 +39,21 @@ public ResponseEntity createComment(@Parameter(name = "content_id", descripti @Operation(summary = "댓글 수정", description = "comment_id 댓글 수정") @PatchMapping("/{comment_id}") - @PreAuthorize("isAuthenticated() && @authorManager.isCommentAuthor(#authentication.getPrincipal(), #commentId)") + @PreAuthorize("isAuthenticated() && @authorManager.isCommentAuthor(authentication.getPrincipal(), #commentId)") public ResponseEntity updateComment(@Parameter(name = "content_id", description = "게시글 번호") @PathVariable(name = "content_id") Long contentId, @Parameter(name = "comment_id", description = "댓글 번호") @PathVariable(name = "comment_id") Long commentId, @Valid @RequestBody UpdateCommentReq request, @AuthenticationPrincipal CustomUserDetails securityUser) { - return ResponseEntity.ok().body(SuccessResponse.from(commentService.updateComment(commentId, request, securityUser))); + return ResponseEntity.ok().body(SuccessResponse.from(commentService.updateComment(contentId, commentId, request, securityUser))); } @Operation(summary = "댓글 삭제", description = "comment_id 댓글 삭제. Soft Delete") @DeleteMapping("/{comment_id}") - @PreAuthorize("isAuthenticated() && @authorManager.isCommentAuthor(#authentication.getPrincipal(), #commentId)") + @PreAuthorize("isAuthenticated() && @authorManager.isCommentAuthor(authentication.getPrincipal(), #commentId)") public ResponseEntity deleteComment(@Parameter(name = "content_id", description = "게시글 번호") @PathVariable(name = "content_id") Long contentId, @Parameter(name = "comment_id", description = "댓글 번호") @PathVariable(name = "comment_id") Long commentId, @AuthenticationPrincipal CustomUserDetails securityUser) { - return ResponseEntity.ok().body(SuccessResponse.from(commentService.deleteComment(commentId, securityUser))); + return ResponseEntity.ok().body(SuccessResponse.from(commentService.deleteComment(contentId, commentId, securityUser))); } } diff --git a/src/main/java/io/oopy/coding/api/comment/service/CommentService.java b/src/main/java/io/oopy/coding/api/comment/service/CommentService.java index 112bb4e..5986015 100644 --- a/src/main/java/io/oopy/coding/api/comment/service/CommentService.java +++ b/src/main/java/io/oopy/coding/api/comment/service/CommentService.java @@ -34,16 +34,16 @@ public class CommentService { * @param contentId */ @Transactional - public List getComments(Long contentId) { + public List getComments(Long contentId) { contentRepository.findById(contentId) .orElseThrow(() -> new ContentErrorException(ContentErrorCode.INVALID_CONTENT_ID)); List comments = commentRepository.findCommentsByContentId(contentId); - List response = new ArrayList<>(); + List response = new ArrayList<>(); for (Comment comment : comments) { - CommentDTO dto = CommentDTO.fromEntity(comment); + GetCommentRes dto = GetCommentRes.fromEntity(comment); response.add(dto); } @@ -84,7 +84,11 @@ public CreateCommentRes createComment(Long contentId, CreateCommentReq req, Cust * 댓글 수정 * @param req */ - public UpdateCommentRes updateComment(Long commentId, UpdateCommentReq req, CustomUserDetails securityUser) { + public UpdateCommentRes updateComment(Long contentId, Long commentId, UpdateCommentReq req, CustomUserDetails securityUser) { + if (!contentRepository.existsByIdAndDeleteAtIsNull(contentId)) { + throw new ContentErrorException(ContentErrorCode.INVALID_CONTENT_ID); + } + Comment comment = commentRepository.findById(commentId) .orElseThrow(() -> new CommentErrorException(CommentErrorCode.INVALID_COMMENT_ID)); @@ -101,7 +105,12 @@ public UpdateCommentRes updateComment(Long commentId, UpdateCommentReq req, Cust * @param commentId */ @Transactional - public DeleteCommentRes deleteComment(Long commentId, CustomUserDetails securityUser) { + public DeleteCommentRes deleteComment(Long contentId, Long commentId, CustomUserDetails securityUser) { + System.out.println("repository result = " + contentRepository.existsByIdAndDeleteAtIsNull(contentId)); + if (!contentRepository.existsByIdAndDeleteAtIsNull(contentId)) { + throw new ContentErrorException(ContentErrorCode.INVALID_CONTENT_ID); + } + Comment comment = commentRepository.findById(commentId) .orElseThrow(() -> new CommentErrorException(CommentErrorCode.INVALID_COMMENT_ID)); diff --git a/src/main/java/io/oopy/coding/api/mark/service/ContentMarkService.java b/src/main/java/io/oopy/coding/api/mark/service/ContentMarkService.java index 9aeffa8..9ac1db2 100644 --- a/src/main/java/io/oopy/coding/api/mark/service/ContentMarkService.java +++ b/src/main/java/io/oopy/coding/api/mark/service/ContentMarkService.java @@ -1,6 +1,10 @@ package io.oopy.coding.api.mark.service; +import io.oopy.coding.api.content.exception.ContentErrorCode; +import io.oopy.coding.api.content.exception.ContentErrorException; import io.oopy.coding.api.content.service.ContentService; +import io.oopy.coding.api.mark.exception.ContentMarkErrorCode; +import io.oopy.coding.api.mark.exception.ContentMarkErrorException; import io.oopy.coding.domain.mark.entity.MarkType; import io.oopy.coding.domain.mark.dto.ChangeUserPressReq; import io.oopy.coding.domain.mark.dto.ContentMarkDto; @@ -40,8 +44,8 @@ public ContentMarkDto getMarkByContent(Long contentId) { } return ContentMarkDto.builder() - .like(likeCount) - .bookmark(bookmarkCount) + .like(likeCount == null ? 0 : likeCount) + .bookmark(bookmarkCount == null ? 0 : bookmarkCount) .build(); } @@ -72,8 +76,14 @@ else if (contentMark.getType().equals(MarkType.BOOKMARK)) @Transactional public void changeUserPress(Long contentId, CustomUserDetails securityUser, ChangeUserPressReq req) { Content content = contentService.findContent(contentId); + if (content.getDeleteAt() != null) { + throw new ContentErrorException(ContentErrorCode.DELETED_CONTENT); + } MarkType markType = MarkType.fromString(req.getType()); + if (markType == null) { + throw new ContentMarkErrorException(ContentMarkErrorCode.INVALID_TYPE); + } ContentMark userMark = contentMarkRepository.findContentMarksByContentIdAndUserIdAndType(contentId, securityUser.getUserId(), markType); diff --git a/src/main/java/io/oopy/coding/domain/comment/dto/CommentDTO.java b/src/main/java/io/oopy/coding/domain/comment/dto/CommentDTO.java index 676a609..b47c24b 100644 --- a/src/main/java/io/oopy/coding/domain/comment/dto/CommentDTO.java +++ b/src/main/java/io/oopy/coding/domain/comment/dto/CommentDTO.java @@ -15,14 +15,14 @@ public class CommentDTO { private Long id; private String commentBody; private Long parentId; - private LocalDateTime deleteAt; + private LocalDateTime deletedAt; public static CommentDTO fromEntity(Comment comment) { return CommentDTO.builder() .id(comment.getId()) .commentBody(comment.getCommentBody()) .parentId(comment.getParentId()) - .deleteAt(comment.getDeleteAt()) + .deletedAt(comment.getDeleteAt()) .build(); } } diff --git a/src/main/java/io/oopy/coding/domain/comment/dto/GetCommentRes.java b/src/main/java/io/oopy/coding/domain/comment/dto/GetCommentRes.java index 5823ce4..ce4e128 100644 --- a/src/main/java/io/oopy/coding/domain/comment/dto/GetCommentRes.java +++ b/src/main/java/io/oopy/coding/domain/comment/dto/GetCommentRes.java @@ -1,5 +1,7 @@ package io.oopy.coding.domain.comment.dto; +import io.oopy.coding.domain.comment.entity.Comment; +import io.oopy.coding.domain.user.entity.User; import lombok.*; import java.time.LocalDateTime; @@ -9,23 +11,47 @@ @Builder public class GetCommentRes { - private Comment comment; - private User user; + private ResComment comment; + private ResUser user; @Builder @Getter - public static class Comment { + public static class ResComment { private String body; private Long parentId; private LocalDateTime createdAt; private LocalDateTime updatedAt; private LocalDateTime deletedAt; + + public static ResComment from(Comment comment) { + return ResComment.builder() + .body(comment.getCommentBody()) + .parentId(comment.getParentId()) + .createdAt(comment.getCreatedAt()) + .updatedAt(comment.getUpdatedAt()) + .deletedAt(comment.getDeleteAt()) + .build(); + } } @Builder @Getter - public static class User { + public static class ResUser { private String name; private String profileImageUrl; + + public static ResUser from(User user) { + return ResUser.builder() + .name(user.getName()) + .profileImageUrl(user.getProfileImageUrl()) + .build(); + } + } + + public static GetCommentRes fromEntity(Comment comment) { + return GetCommentRes.builder() + .comment(ResComment.from(comment)) + .user(ResUser.from(comment.getUser())) + .build(); } } diff --git a/src/main/java/io/oopy/coding/domain/content/repository/ContentRepository.java b/src/main/java/io/oopy/coding/domain/content/repository/ContentRepository.java index 46e7017..370bb5a 100644 --- a/src/main/java/io/oopy/coding/domain/content/repository/ContentRepository.java +++ b/src/main/java/io/oopy/coding/domain/content/repository/ContentRepository.java @@ -10,4 +10,6 @@ public interface ContentRepository extends JpaRepository { List findContentsByUserId(Long userId); boolean existsByIdAndUserId(Long id, Long userId); + + boolean existsByIdAndDeleteAtIsNull(Long id); } From 216df9973d14890ccd4715f4c30d24b8b5a8295b Mon Sep 17 00:00:00 2001 From: dnjsals45 Date: Fri, 23 Feb 2024 15:02:28 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix/=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C=20?= =?UTF-8?q?=EC=A4=84=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/oopy/coding/api/comment/service/CommentService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/io/oopy/coding/api/comment/service/CommentService.java b/src/main/java/io/oopy/coding/api/comment/service/CommentService.java index 5986015..2864191 100644 --- a/src/main/java/io/oopy/coding/api/comment/service/CommentService.java +++ b/src/main/java/io/oopy/coding/api/comment/service/CommentService.java @@ -106,7 +106,6 @@ public UpdateCommentRes updateComment(Long contentId, Long commentId, UpdateComm */ @Transactional public DeleteCommentRes deleteComment(Long contentId, Long commentId, CustomUserDetails securityUser) { - System.out.println("repository result = " + contentRepository.existsByIdAndDeleteAtIsNull(contentId)); if (!contentRepository.existsByIdAndDeleteAtIsNull(contentId)) { throw new ContentErrorException(ContentErrorCode.INVALID_CONTENT_ID); }