diff --git a/WableServer/src/main/java/com/wable/www/WableServer/api/comment/controller/CommentController.java b/WableServer/src/main/java/com/wable/www/WableServer/api/comment/controller/CommentController.java index 92a5225..acc3c17 100644 --- a/WableServer/src/main/java/com/wable/www/WableServer/api/comment/controller/CommentController.java +++ b/WableServer/src/main/java/com/wable/www/WableServer/api/comment/controller/CommentController.java @@ -2,6 +2,7 @@ import com.wable.www.WableServer.api.comment.dto.request.CommentLikedRequestDto; import com.wable.www.WableServer.api.comment.dto.request.CommentPostRequestDto; +import com.wable.www.WableServer.api.comment.dto.request.CommentPostRequestDtoVer2; import com.wable.www.WableServer.api.comment.service.CommentCommendService; import com.wable.www.WableServer.api.comment.service.CommentQueryService; import com.wable.www.WableServer.common.response.ApiResponse; @@ -107,4 +108,11 @@ public ResponseEntity> getCommentAllByMemberWithImage(Princi Long usingMemberId = MemberUtil.getMemberId(principal); return ApiResponse.success(GET_MEMBER_COMMENT_SECCESS, commentQueryService.getCommentAllByMemberWithImage(usingMemberId,memberId,cursor)); } + + @PostMapping("v3/content/{contentId}/comment") + @Operation(summary = "답글 작성 API입니다.(+대댓글)", description = "CommentPostWithParentChildComment") + public ResponseEntity> postCommentWithParentChildComment(Principal principal, @PathVariable Long contentId, @Valid @RequestBody CommentPostRequestDtoVer2 commentPostRequestDtoVer2) { + commentCommendService.postCommentWithParentChildComment(MemberUtil.getMemberId(principal),contentId, commentPostRequestDtoVer2); + return ApiResponse.success(POST_COMMENT_SUCCESS); + } } \ No newline at end of file diff --git a/WableServer/src/main/java/com/wable/www/WableServer/api/comment/domain/Comment.java b/WableServer/src/main/java/com/wable/www/WableServer/api/comment/domain/Comment.java index b6bde78..6751777 100644 --- a/WableServer/src/main/java/com/wable/www/WableServer/api/comment/domain/Comment.java +++ b/WableServer/src/main/java/com/wable/www/WableServer/api/comment/domain/Comment.java @@ -49,11 +49,16 @@ public class Comment extends BaseTimeEntity { @Column(name = "is_blind", columnDefinition = "BOOLEAN DEFAULT false") private boolean isBlind; + @Column(name = "parent_comment_id", columnDefinition = "BIGINT DEFAULT -1") + private Long parentCommentId; + @Builder - public Comment(Member member, Content content, String commentText) { + public Comment(Member member, Content content, String commentText, Long parentCommentId) { this.member = member; this.content = content; this.commentText = commentText; + this.parentCommentId = parentCommentId; + this.isBlind = false; } public void setCommentImage(String commentImageUrl) { this.commentImage = commentImageUrl; diff --git a/WableServer/src/main/java/com/wable/www/WableServer/api/comment/dto/request/CommentPostRequestDtoVer2.java b/WableServer/src/main/java/com/wable/www/WableServer/api/comment/dto/request/CommentPostRequestDtoVer2.java new file mode 100644 index 0000000..b3911bb --- /dev/null +++ b/WableServer/src/main/java/com/wable/www/WableServer/api/comment/dto/request/CommentPostRequestDtoVer2.java @@ -0,0 +1,10 @@ +package com.wable.www.WableServer.api.comment.dto.request; + +import jakarta.validation.constraints.NotBlank; + +public record CommentPostRequestDtoVer2( + @NotBlank String commentText, + Long parentCommentId, + Long parentCommentWriterId +) { +} diff --git a/WableServer/src/main/java/com/wable/www/WableServer/api/comment/service/CommentCommendService.java b/WableServer/src/main/java/com/wable/www/WableServer/api/comment/service/CommentCommendService.java index ed2bb66..e2c9ca3 100644 --- a/WableServer/src/main/java/com/wable/www/WableServer/api/comment/service/CommentCommendService.java +++ b/WableServer/src/main/java/com/wable/www/WableServer/api/comment/service/CommentCommendService.java @@ -4,6 +4,7 @@ import com.wable.www.WableServer.api.comment.domain.CommentLiked; import com.wable.www.WableServer.api.comment.dto.request.CommentLikedRequestDto; import com.wable.www.WableServer.api.comment.dto.request.CommentPostRequestDto; +import com.wable.www.WableServer.api.comment.dto.request.CommentPostRequestDtoVer2; import com.wable.www.WableServer.api.comment.repository.CommentLikedRepository; import com.wable.www.WableServer.api.comment.repository.CommentRepository; import com.wable.www.WableServer.api.content.domain.Content; @@ -49,6 +50,7 @@ public void postComment(Long memberId, Long contentId, CommentPostRequestDto com .member(usingMember) .content(content) .commentText(commentPostRequestDto.commentText()) + .parentCommentId(-1L) .build(); Comment savedComment = commentRepository.save(comment); @@ -104,6 +106,7 @@ public void postCommentVer2(Long memberId, Long contentId, MultipartFile comment .member(usingMember) .content(content) .commentText(commentPostRequestDto.commentText()) + .parentCommentId(-1L) .build(); Comment savedComment = commentRepository.save(comment); @@ -252,6 +255,100 @@ public void unlikeComment(Long memberId, Long commentId){ } } + public void postCommentWithParentChildComment(Long memberId, Long contentId, CommentPostRequestDtoVer2 commentPostRequestDtoVer2){ + Content content = contentRepository.findContentByIdOrThrow(contentId); + Member usingMember = memberRepository.findMemberByIdOrThrow(memberId); + + usingMember.increaseExpPostComment(); + + GhostUtil.isGhostMember(usingMember.getMemberGhost()); + + Comment comment = Comment.builder() + .member(usingMember) + .content(content) + .commentText(commentPostRequestDtoVer2.commentText()) + .parentCommentId(commentPostRequestDtoVer2.parentCommentId()) + .build(); + Comment savedComment = commentRepository.save(comment); + + //답글 작성 시 게시물 작상자에게 알림 발생 + Member contentWritingMember = memberRepository.findMemberByIdOrThrow(content.getMember().getId()); + + if(usingMember != contentWritingMember) { ////자신 게시물에 대한 좋아요 누르면 알림 발생 x + if(commentPostRequestDtoVer2.parentCommentId().equals(-1L)) { + Notification notification = Notification.builder() + .notificationTargetMember(contentWritingMember) + .notificationTriggerMemberId(usingMember.getId()) + .notificationTriggerType("comment") + .notificationTriggerId(comment.getId()) + .isNotificationChecked(false) + .notificationText(comment.getCommentText()) + .build(); + Notification savedNotification = notificationRepository.save(notification); + + if (Boolean.TRUE.equals(contentWritingMember.getIsPushAlarmAllowed())) { + String FcmMessageTitle = usingMember.getNickname() + "님이 댓글을 작성했습니다."; + contentWritingMember.increaseFcmBadge(); + FcmMessageDto commentFcmMessage = FcmMessageDto.builder() + .validateOnly(false) + .message(FcmMessageDto.Message.builder() + .notificationDetails(FcmMessageDto.NotificationDetails.builder() + .title(FcmMessageTitle) + .body(commentPostRequestDtoVer2.commentText()) + .build()) + .token(contentWritingMember.getFcmToken()) + .data(FcmMessageDto.Data.builder() + .name("comment") + .description("댓글 푸시 알림") + .relateContentId(String.valueOf(contentId)) + .build()) + .badge(contentWritingMember.getFcmBadge()) + .build()) + .build(); + + fcmService.sendMessage(commentFcmMessage); + } + }else { //대댓글의 경우 + Member parentCommentWriter = memberRepository.findMemberByIdOrThrow(commentPostRequestDtoVer2.parentCommentWriterId()); + + if(!usingMember.equals(contentWritingMember)) { + Notification notification = Notification.builder() + .notificationTargetMember(parentCommentWriter) + .notificationTriggerMemberId(usingMember.getId()) + .notificationTriggerType("childComment") + .notificationTriggerId(comment.getId()) + .isNotificationChecked(false) + .notificationText(comment.getCommentText()) + .build(); + Notification savedNotification = notificationRepository.save(notification); + + if (Boolean.TRUE.equals(parentCommentWriter.getIsPushAlarmAllowed())) { + String FcmMessageTitle = usingMember.getNickname() + "님이 답글을 작성했습니다."; + parentCommentWriter.increaseFcmBadge(); + FcmMessageDto commentFcmMessage = FcmMessageDto.builder() + .validateOnly(false) + .message(FcmMessageDto.Message.builder() + .notificationDetails(FcmMessageDto.NotificationDetails.builder() + .title(FcmMessageTitle) + .body(commentPostRequestDtoVer2.commentText()) + .build()) + .token(parentCommentWriter.getFcmToken()) + .data(FcmMessageDto.Data.builder() + .name("childComment") + .description("답글 푸시 알림") + .relateContentId(String.valueOf(contentId)) + .build()) + .badge(parentCommentWriter.getFcmBadge()) + .build()) + .build(); + + fcmService.sendMessage(commentFcmMessage); + } + } + } + } + } + private void isDuplicateCommentLike(Comment comment, Member member) { if (commentLikedRepository.existsByCommentAndMember(comment, member)) { throw new BadRequestException(ErrorStatus.DUPLICATION_COMMENT_LIKE.getMessage());