Skip to content

Commit

Permalink
Merge pull request #47 from Team-Wable/feat/#46
Browse files Browse the repository at this point in the history
[FEAT] 대댓글 POST API개발
  • Loading branch information
Hong0329 authored Nov 17, 2024
2 parents d136cad + a6323cc commit df3f4e9
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,4 +108,11 @@ public ResponseEntity<ApiResponse<Object>> 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<ApiResponse<Object>> postCommentWithParentChildComment(Principal principal, @PathVariable Long contentId, @Valid @RequestBody CommentPostRequestDtoVer2 commentPostRequestDtoVer2) {
commentCommendService.postCommentWithParentChildComment(MemberUtil.getMemberId(principal),contentId, commentPostRequestDtoVer2);
return ApiResponse.success(POST_COMMENT_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit df3f4e9

Please sign in to comment.