Skip to content

Commit

Permalink
[J2KB-3rd-Season#20]feat: 답변 수정 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
enjay27 committed Jun 11, 2021
1 parent 7a3ae03 commit 216746a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ public DefaultResponse<?> reply(@RequestBody @Valid ReplyForm form, HttpServletR
return replyService.reply(form, request);
}

@GetMapping("/reply/{id}")
public Page<ReplyDto> findReplies(
@PathVariable("id") Long postId, Pageable pageable) {
DefaultResponse<Page<ReplyDto>> 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
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/com/devin/dev/service/ReplyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<User> userOptional = userRepository.findById(userId);
if (userOptional.isEmpty()) {
return new DefaultResponse<>(StatusCode.NOT_EXIST, ResponseMessage.NOT_FOUND_USER);
}
User user = userOptional.get();

Optional<Reply> 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<ReplyImage> replyImages = replyImageRepository.findByReply(reply);
replyImageRepository.deleteInBatch(replyImages);

// 수정된 내용 반영
List<ReplyImage> 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);
}

}

0 comments on commit 216746a

Please sign in to comment.