728x90
question_detail.html ์์ ๋ฒํผ ์๋์ ์ถ๊ฐ
<a
onclick="return confirm('์ ๋ง๋ก ์ญ์ ํ์๊ฒ ์ต๋๊น?')"
th:href="@{|/question/delete/${question.id}|}"
class="delete btn btn-sm btn-outline-danger"
sec:authorize="isAuthenticated()"
th:if="${question.author != null and #authentication.getPrincipal().getUsername() == question.author.username}"
th:text="์ญ์ "
></a>

์ธ์ฆ๋ ์ ์ ๋ง ๋ณด์ฌ์ฃผ๊ธฐ

ํ์ฌ ๋ก๊ทธ์ธ ๋ ์ฌ๋์ด ๊ธ์ด์ด์ ๋์ผํ ๋
QuestionService
//์ง๋ฌธ ์ญ์ ์ฒ๋ฆฌ
public void delete(Question question) {
qRepo.delete(question);
}
QuestionController
//์ง๋ฌธ ์ญ์ ์ฒ๋ฆฌ
@PreAuthorize("isAuthenticated()")
@GetMapping("/delete/{id}")
public String questionDelete(Principal principal, @PathVariable("id") int id ) {
Question question = qService.getQuestion(id);
if(!question.getAuthor().getUsername().equals(principal.getName())){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"์ญ์ ๊ถํ์ด ์์ต๋๋ค.");
}
//DB์์ ์ง๋ฌธ ์ญ์
qService.delete(question);
return "redirect:/";
}
ํ ์คํธํ๊ธฐ




728x90
'BACKEND > SpringBoot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๋ต๋ณ ์ญ์ (1) | 2023.11.13 |
|---|---|
| ๋ต๋ณ ์์ (0) | 2023.11.13 |
| ์ง๋ฌธ ์์ ์ฒ๋ฆฌ (0) | 2023.11.10 |
| ์ํฐํฐ ๋ณ๊ฒฝ ๊ธ์ด์ด ์ถ๊ฐ ๋ฐ ๊ธ ๋ชฉ๋ก,์์ธ๋ณด๊ธฐ,๋ต๋ณ์ ๋ฐ์ํ๊ธฐ (0) | 2023.11.10 |
| ๋ก๊ทธ์์ ์ฒ๋ฆฌ (0) | 2023.11.10 |