diff --git a/src/main/java/tavebalak/OTTify/community/controller/CommunityController.java b/src/main/java/tavebalak/OTTify/community/controller/CommunityController.java index 4d581537..3ac891be 100644 --- a/src/main/java/tavebalak/OTTify/community/controller/CommunityController.java +++ b/src/main/java/tavebalak/OTTify/community/controller/CommunityController.java @@ -203,12 +203,10 @@ public BaseResponse deleteComment(@PathVariable Long subjectId, @ApiImplicitParam(name = "commentId", value = "토론 댓글의 id", required = true, paramType = "path"), @ApiImplicitParam(name = "recommentId", value = "토론 대댓글의 id", required = true, paramType = "path") }) - @DeleteMapping("/recomment/{subjectId}/{commentId}/{recommentId}") + @DeleteMapping("/recomment/{recommentId}") public BaseResponse deleteRecomment( - @PathVariable Long subjectId, - @PathVariable Long commentId, @PathVariable Long recommentId) throws NotFoundException { - replyService.deleteRecomment(subjectId, commentId, recommentId); + replyService.deleteRecomment(recommentId); return BaseResponse.success("성공적으로 토론대댓글을 삭제하였습니다."); } diff --git a/src/main/java/tavebalak/OTTify/community/entity/Reply.java b/src/main/java/tavebalak/OTTify/community/entity/Reply.java index d97cb044..dfc880f5 100644 --- a/src/main/java/tavebalak/OTTify/community/entity/Reply.java +++ b/src/main/java/tavebalak/OTTify/community/entity/Reply.java @@ -79,4 +79,10 @@ public ReplyCommentEditorDTO toEditor() { public void edit(ReplyCommentEditorDTO c) { this.content = c.getComment(); } + + public void cancelChildReply(Reply reply) { + if (child.contains(reply)) { + child.remove(reply); + } + } } diff --git a/src/main/java/tavebalak/OTTify/community/service/ReplyService.java b/src/main/java/tavebalak/OTTify/community/service/ReplyService.java index b785221a..a83da1d6 100644 --- a/src/main/java/tavebalak/OTTify/community/service/ReplyService.java +++ b/src/main/java/tavebalak/OTTify/community/service/ReplyService.java @@ -20,7 +20,7 @@ public interface ReplyService { void deleteComment(Long subjectId, Long commentId); - void deleteRecomment(Long subjectId, Long commentId, Long recommentId); + void deleteRecomment(Long recommentId); List getComment(Long commentId); } diff --git a/src/main/java/tavebalak/OTTify/community/service/ReplyServiceImpl.java b/src/main/java/tavebalak/OTTify/community/service/ReplyServiceImpl.java index 0f77a317..01f25b8c 100644 --- a/src/main/java/tavebalak/OTTify/community/service/ReplyServiceImpl.java +++ b/src/main/java/tavebalak/OTTify/community/service/ReplyServiceImpl.java @@ -23,6 +23,7 @@ import tavebalak.OTTify.error.exception.UnauthorizedException; import tavebalak.OTTify.oauth.jwt.SecurityUtil; import tavebalak.OTTify.user.entity.User; +import tavebalak.OTTify.user.repository.LikedReplyRepository; import tavebalak.OTTify.user.repository.UserRepository; @Service @@ -34,6 +35,7 @@ public class ReplyServiceImpl implements ReplyService { private final CommunityRepository communityRepository; private final ReplyRepository replyRepository; private final UserRepository userRepository; + private final LikedReplyRepository likedReplyRepository; @Override public Reply saveComment(ReplyCommentCreateDTO c) { @@ -126,18 +128,14 @@ public void deleteComment(Long subjectId, Long commentId) { throw new BadRequestException(ErrorCode.CAN_NOT_OTHER_COMMENT_DELETE_REQUEST); } + likedReplyRepository.deleteAllByReply(savedReply); replyRepository.delete(savedReply); } @Override - public void deleteRecomment(Long subjectId, Long commentId, Long recommentId) { - communityRepository.findById(subjectId).orElseThrow( - () -> new NotFoundException(ErrorCode.COMMUNITY_NOT_FOUND) - ); - replyRepository.findById(commentId).orElseThrow( - () -> new NotFoundException(ErrorCode.REPLY_NOT_FOUND) - ); + public void deleteRecomment(Long recommentId) { + Reply savedReply = replyRepository.findById(recommentId).orElseThrow( () -> new NotFoundException(ErrorCode.REPLY_NOT_FOUND) ); @@ -146,7 +144,10 @@ public void deleteRecomment(Long subjectId, Long commentId, Long recommentId) { throw new BadRequestException(ErrorCode.CAN_NOT_OTHER_COMMENT_DELETE_REQUEST); } - replyRepository.delete(savedReply); + Reply parent = savedReply.getParent(); + parent.cancelChildReply(savedReply); + + likedReplyRepository.deleteAllByReply(savedReply); } @Override diff --git a/src/main/java/tavebalak/OTTify/user/repository/LikedReplyRepository.java b/src/main/java/tavebalak/OTTify/user/repository/LikedReplyRepository.java index 3c8ea1fd..26bef75d 100644 --- a/src/main/java/tavebalak/OTTify/user/repository/LikedReplyRepository.java +++ b/src/main/java/tavebalak/OTTify/user/repository/LikedReplyRepository.java @@ -3,13 +3,17 @@ import java.util.List; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; +import tavebalak.OTTify.community.entity.Reply; import tavebalak.OTTify.user.entity.LikedReply; public interface LikedReplyRepository extends JpaRepository { - Optional findByUserIdAndReplyIdAndCommunityId(Long userId, Long replyId, Long communityId); + Optional findByUserIdAndReplyIdAndCommunityId(Long userId, Long replyId, + Long communityId); List findByCommunityIdAndReplyId(Long communityId, Long replyId); int countByCommunityId(Long communityId); + + void deleteAllByReply(Reply reply); } diff --git a/src/test/java/tavebalak/OTTify/community/controller/CommunityControllerTest.java b/src/test/java/tavebalak/OTTify/community/controller/CommunityControllerTest.java index 8de60885..19958b38 100644 --- a/src/test/java/tavebalak/OTTify/community/controller/CommunityControllerTest.java +++ b/src/test/java/tavebalak/OTTify/community/controller/CommunityControllerTest.java @@ -353,7 +353,7 @@ public void getFailBadIdTestRecommentContent() throws Exception, NotFoundExcepti @DisplayName("DELETE 토론 대댓글 삭제 컨트롤러 성공") public void 대댓글_삭제_성공() throws NotFoundException, Exception { //given - doNothing().when(replyService).deleteRecomment(anyLong(), anyLong(), anyLong()); + doNothing().when(replyService).deleteRecomment(anyLong()); //when ResultActions resultActions = mockMvc.perform(