Skip to content

Commit

Permalink
[J2KB-3rd-Season#21]feat: 답변 삭제 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
enjay27 committed Jun 11, 2021
1 parent 216746a commit 538d458
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/main/java/com/devin/dev/controller/reply/ReplyController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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<String> replyImages;
}

@Data
@AllArgsConstructor
static class Result<T> {
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/devin/dev/service/ReplyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<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.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);
}
}

0 comments on commit 538d458

Please sign in to comment.