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 54ac8d8..efa36b7 100644 --- a/src/main/java/com/devin/dev/controller/reply/ReplyController.java +++ b/src/main/java/com/devin/dev/controller/reply/ReplyController.java @@ -25,11 +25,9 @@ public DefaultResponse reply(@RequestBody @Valid ReplyForm form, HttpServletR return replyService.reply(form, request); } - @GetMapping("/reply/{id}") - public Page findReplies( - @PathVariable("id") Long postId, Pageable pageable) { - DefaultResponse> response = replyService.findRepliesInPost(postId, pageable); - return response.getData(); + @PutMapping("/reply/{id}/update") + public DefaultResponse reply(@PathVariable("id") Long replyId, @RequestBody @Valid ReplyUpdateForm form, HttpServletRequest request) { + return replyService.editReply(replyId, form, request); } @Data diff --git a/src/main/java/com/devin/dev/service/ReplyService.java b/src/main/java/com/devin/dev/service/ReplyService.java index 8b23fb3..437db13 100644 --- a/src/main/java/com/devin/dev/service/ReplyService.java +++ b/src/main/java/com/devin/dev/service/ReplyService.java @@ -253,5 +253,53 @@ public DefaultResponse reply(ReplyForm form, HttpServletRequest request) { return new DefaultResponse<>(StatusCode.SUCCESS, ResponseMessage.REPLY_UPLOAD_SUCCESS, replyDto); } + @Transactional + public DefaultResponse editReply(Long replyId, ReplyUpdateForm form, 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.NOT_SAME_USER); + } + + // 기존 이미지 경로 삭제 + List replyImages = replyImageRepository.findByReply(reply); + replyImageRepository.deleteInBatch(replyImages); + + // 수정된 내용 반영 + List newReplyImages = ReplyImage.createReplyImages(form.getReply_image()); + reply.setContent(form.getContent()); + reply.setReplyImages(newReplyImages); + + // 저장 + replyImageRepository.saveAll(newReplyImages); + replyRepository.save(reply); + + // DTO 변환 + ReplyDto replyDto = ReplyMapper.replyToReplyDto(reply); + + // response 객체 리턴 + return new DefaultResponse<>(StatusCode.SUCCESS, ResponseMessage.REPLY_EDIT_SUCCESS, replyDto); + } }