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 5d7992c..395d12e 100644 --- a/src/main/java/com/devin/dev/controller/reply/ReplyController.java +++ b/src/main/java/com/devin/dev/controller/reply/ReplyController.java @@ -42,6 +42,11 @@ public DefaultResponse changePostLike(@PathVariable("id") Long replyId, HttpS return replyService.changeReplyLike(replyId, request); } + @PatchMapping("/reply/{id}/select") + public DefaultResponse selectReply(@PathVariable("id") Long replyId, HttpServletRequest request) { + return replyService.selectReply(replyId, request); + } + @Data private static class CreateReplyResponse { private Long id; diff --git a/src/main/java/com/devin/dev/service/ReplyService.java b/src/main/java/com/devin/dev/service/ReplyService.java index a90cd62..cd4e558 100644 --- a/src/main/java/com/devin/dev/service/ReplyService.java +++ b/src/main/java/com/devin/dev/service/ReplyService.java @@ -6,6 +6,7 @@ import com.devin.dev.dto.reply.ReplyLikeDto; import com.devin.dev.dto.reply.ReplyMapper; import com.devin.dev.entity.post.Post; +import com.devin.dev.entity.post.PostStatus; import com.devin.dev.entity.reply.Reply; import com.devin.dev.entity.reply.ReplyImage; import com.devin.dev.entity.reply.ReplyLike; @@ -393,4 +394,48 @@ public DefaultResponse changeReplyLike(Long replyId, HttpServletReques return new DefaultResponse<>(StatusCode.SUCCESS, ResponseMessage.REPLY_LIKE_CHANGE_SUCCESS, replyDto); } + + public DefaultResponse selectReply(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.getPost().getUser())) { + return new DefaultResponse<>(StatusCode.CONDITION_FAIL, ResponseMessage.NOT_SAME_USER); + } + + if (user.getId().equals(reply.getUser().getId())) { + return new DefaultResponse<>(StatusCode.CONDITION_FAIL, ResponseMessage.SAME_USER); + } + + user.changeExp(User.ExpChangeType.REPLY_SELECT); + reply.getUser().changeExp(User.ExpChangeType.REPLY_BE_SELECTED); + + reply.setStatus(ReplyStatus.SELECTED); + reply.getPost().setStatus(PostStatus.SELECTED); + + replyRepository.save(reply); + userRepository.save(user); + userRepository.save(reply.getUser()); + postRepository.save(reply.getPost()); + + ReplyDto replyDto = new ReplyDto(reply); + + return new DefaultResponse<>(StatusCode.SUCCESS, ResponseMessage.REPLY_LIKE_CHANGE_SUCCESS, replyDto); + } } diff --git a/src/main/java/com/devin/dev/utils/ResponseMessage.java b/src/main/java/com/devin/dev/utils/ResponseMessage.java index 024bb69..05ebaf0 100644 --- a/src/main/java/com/devin/dev/utils/ResponseMessage.java +++ b/src/main/java/com/devin/dev/utils/ResponseMessage.java @@ -12,6 +12,7 @@ public enum ResponseMessage { NOT_FOUND_REPLY("답변을 찾을 수 없습니다."), NOT_FOUND_SUBJECT("주제를 찾을 수 없습니다."), NOT_SAME_USER("작성자가 아닙니다."), + SAME_USER("작성자 입니다."), CANNOT_DELETE_SELECTED("채택된 답변은 삭제할 수 없습니다."), CREATED_USER("회원 가입 성공"), UPDATE_USER("회원 정보 수정 성공"),