Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 대댓글 POST API개발 #47

Merged
merged 3 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading