728x90
반응형
SMALL
답변 삭제 기능 추가하기
상세 템플릿에 삭제 버튼을 추가
/templates/question_detail.html
...
<!-- 수정 및 삭제 버튼 -->
<div class="my-3">
<a th:href="@{|/answer/modify/${answer.id}|}" class="btn btn-sm btn-outline-secondary"
sec:authorize="isAuthenticated()"
th:if="${answer.author != null and #authentication.getPrincipal().getUsername() == answer.author.username}"
th:text="수정"></a>
<a href="javascript:void(0);" th:data-uri="@{|/answer/delete/${answer.id}|}"
class="delete btn btn-sm btn-outline-secondary"
sec:authorize="isAuthenticated()"
th:if="${answer.author != null and #authentication.getPrincipal().getUsername() == answer.author.username}"
th:text="삭제"></a>
</div>
<!-- 수정 및 삭제 버튼 끝 -->
...
서비스 수정하기
/answer/AnswerService.java
public void delete(Answer answer) {
this.answerRepository.delete(answer);
}
컨트롤러 수정하기
GET방식으로 요청되는 URL 처리하기 위해 메서드를 추가하자
/answer/AnswerController.java
@PreAuthorize("isAuthenticated()")
@GetMapping("/delete/{id}")
public String answerDelete(Principal principal, @PathVariable("id")Integer id) {
Answer answer = this.answerService.getAnswer(id);
if (!answer.getAuthor().getUsername().equals(principal.getName())) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "삭제 권한이 업습니다.");
}
this.answerService.delete(answer);
return String.format("redirect:/question/detail/%s", answer.getQuestion().getId());
}
로컬 서버를 재시작한 후 삭제를 해보자.
728x90
반응형
LIST
'IT > Spring Boot' 카테고리의 다른 글
[Spring Boot] 34. 게시글 추천, 좋아요 기능 구현하기 (0) | 2024.05.20 |
---|---|
[Spring Boot] 33. 수정 일시 나타내기 (0) | 2024.05.20 |
[Spring Boot] 31. 게시판 답변 수정 기능 추가하기 (0) | 2024.05.20 |
[Spring Boot] 30. 게시판 질문 삭제 기능 추가하기 (0) | 2024.05.20 |
[Spring Boot] 29. 게시판 질문 수정 기능 추가하기 (0) | 2024.05.20 |