From 538d4587d77e153dcd44864bc9b99a00dff69f77 Mon Sep 17 00:00:00 2001 From: enjay27 Date: Sat, 12 Jun 2021 00:09:51 +0900 Subject: [PATCH] =?UTF-8?q?[#21]feat:=20=EB=8B=B5=EB=B3=80=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/controller/reply/ReplyController.java | 19 ++++----- .../com/devin/dev/service/ReplyService.java | 40 +++++++++++++++++++ 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/devin/dev/controller/reply/ReplyController.java b/src/main/java/com/devin/dev/controller/reply/ReplyController.java index efa36b7..b01e530 100644 --- a/src/main/java/com/devin/dev/controller/reply/ReplyController.java +++ b/src/main/java/com/devin/dev/controller/reply/ReplyController.java @@ -1,5 +1,6 @@ package com.devin.dev.controller.reply; +import com.devin.dev.controller.post.ReplyUpdateForm; import com.devin.dev.dto.reply.ReplyDto; import com.devin.dev.model.DefaultResponse; import com.devin.dev.service.ReplyService; @@ -10,6 +11,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.*; +import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; import javax.validation.constraints.NotEmpty; import java.util.List; @@ -30,6 +32,11 @@ public DefaultResponse reply(@PathVariable("id") Long replyId, @RequestBody @ return replyService.editReply(replyId, form, request); } + @DeleteMapping("/reply/{id}") + public DefaultResponse reply(@PathVariable("id") Long replyId, HttpServletRequest request) { + return replyService.deleteReply(replyId, request); + } + @Data private static class CreateReplyResponse { private Long id; @@ -39,18 +46,6 @@ public CreateReplyResponse(Long id) { } } - @Data - private static class CreateReplyRequest { - @NotEmpty - private Long postId; - @NotEmpty - private Long userId; - @NotEmpty - private String content; - @NotEmpty - private List replyImages; - } - @Data @AllArgsConstructor static class Result { diff --git a/src/main/java/com/devin/dev/service/ReplyService.java b/src/main/java/com/devin/dev/service/ReplyService.java index 437db13..82afcb0 100644 --- a/src/main/java/com/devin/dev/service/ReplyService.java +++ b/src/main/java/com/devin/dev/service/ReplyService.java @@ -302,4 +302,44 @@ public DefaultResponse editReply(Long replyId, ReplyUpdateForm form, HttpServ return new DefaultResponse<>(StatusCode.SUCCESS, ResponseMessage.REPLY_EDIT_SUCCESS, replyDto); } + @Transactional + public DefaultResponse deleteReply(Long replyId, HttpServletRequest request) { + String token = tokenProvider.parseToken(request); + Long userId; + if (tokenProvider.validateToken(token)) { + userId = tokenProvider.getUserId(token); + } else { + return new DefaultResponse<>(StatusCode.FAIL_AUTH, ResponseMessage.NOT_FOUND_USER); + } + Optional userOptional = userRepository.findById(userId); + if (userOptional.isEmpty()) { + return new DefaultResponse<>(StatusCode.NOT_EXIST, ResponseMessage.NOT_FOUND_USER); + } + User user = userOptional.get(); + + Optional replyOptional = replyRepository.findById(replyId); + if (replyOptional.isEmpty()) { + return new DefaultResponse<>(StatusCode.NOT_EXIST, ResponseMessage.NOT_FOUND_REPLY); + } + Reply reply = replyOptional.get(); + + // 작성자 확인 + if (isNotSameUser(user, reply.getUser())) { + return new DefaultResponse<>(StatusCode.CONDITION_FAIL, ResponseMessage.NOT_SAME_USER); + } + + // 채택된 답변인지 확인 + if (reply.getStatus() == ReplyStatus.SELECTED) { + return new DefaultResponse<>(StatusCode.CONDITION_FAIL, ResponseMessage.CANNOT_DELETE_SELECTED); + } + + // 리플 작성자 경험치 삭제 + user.changeExp(User.ExpChangeType.DELETE_REPLY); + + // 댓글 상태변경 DELETED 후 저장 + reply.setStatus(ReplyStatus.DELETED); + replyRepository.save(reply); + + return new DefaultResponse<>(StatusCode.SUCCESS, ResponseMessage.REPLY_DELETE_SUCCESS); + } }