Skip to content

Commit

Permalink
#6 - Feat: 글 삭제 기능 구현(GET 요청)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahah525 committed Oct 18, 2022
1 parent a61244f commit f8b7c9e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,22 @@ public String modify(@PathVariable long id, @AuthenticationPrincipal MemberConte

return "redirect:/post/%d".formatted(post.getId());
}

// TODO: POST 방식으로 바꾸기
// 글 삭제
@PreAuthorize("isAuthenticated()")
@GetMapping("/{id}/delete")
public String delete(@PathVariable long id, @AuthenticationPrincipal MemberContext memberContext) {
Member member = memberContext.getMember();
Post post = postService.findById(id);

// TODO : 예외 처리
if(!postService.canDelete(member, post)) {
throw new RuntimeException();
}
postService.delete(post);

// 글 리스트 페이지로 리다이렉트
return "redirect:/post/list";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,18 @@ public void modify(Post post, WriteForm writeForm) {
postRepository.save(post);
}

// 수정 권한 여부 체크(글쓴이 본인인지)
@Transactional
public void delete(Post post) {
postRepository.delete(post);
}

// 수정 권한 여부 체크(수정 권한: 글쓴이 본인)
public boolean canModify(Member member, Post post) {
return member.getId().equals(post.getAuthor().getId());
}

// 삭제 권한 여부 체크(삭제 권한: 글쓴이 본인)
public boolean canDelete(Member member, Post post) {
return canModify(member, post);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ <h1 class="font-bold text-lg">글 상세(임시)</h1>

<div class="grid grid-cols-2 mt-2 gap-2">
<a th:href="|/post/${post.id}/modify|" class="btn btn-secondary btn-outline">수정</a>
<a th:href="|/post/${post.id}/delete|" class="btn btn-secondary btn-outline">삭제</a>
<a onclick="if(!confirm('해당 글을 삭제하시겠습니까?')) return false;" th:href="|/post/${post.id}/delete|" class="btn btn-secondary btn-outline">삭제</a>
</div>
</div>
</div>
Expand Down

0 comments on commit f8b7c9e

Please sign in to comment.