Skip to content

Commit

Permalink
[J2KB-3rd-Season#23]feat: 답변 채택 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
enjay27 committed Jun 11, 2021
1 parent 420c9b6 commit ced6254
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/devin/dev/service/ReplyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -393,4 +394,48 @@ public DefaultResponse<ReplyDto> 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<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.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);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/devin/dev/utils/ResponseMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("회원 정보 수정 성공"),
Expand Down

0 comments on commit ced6254

Please sign in to comment.