1. View 확인

{{#board.replies}}
<!-- 댓글아이템 -->
<div class="list-group-item d-flex justify-content-between align-items-center">
<div class="d-flex">
<div class="px-1 me-1 bg-primary text-white rounded">{{user.username}}</div>
<div>{{comment}}</div>
</div>
<form action="/reply/{{id}}/delete" method="post">
<button class="btn">🗑</button>
</form>
</div>
{{/board.replies}}
댓글을 작성한 사람만 삭제할 수 있도록 댓글의 권한을 부여해보자.
2. 권한부여하기
Reply
package shop.mtcoding.blog.reply;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import shop.mtcoding.blog.board.Board;
import shop.mtcoding.blog.user.User;
import java.time.LocalDateTime;
@NoArgsConstructor // 빈생성자가 필요
@Entity
@Data
@Table(name = "reply_tb")
public class Reply {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id ;
private String comment;
//user_id 로 테이블 만들어짐.
@ManyToOne(fetch = FetchType.LAZY)
private User user ;
@ManyToOne(fetch = FetchType.LAZY)
private Board board ;
@CreationTimestamp
private LocalDateTime createdAt;
@Transient
private boolean isReplyOwner;
@Builder
public Reply(int id, String comment, User user, Board board, LocalDateTime createdAt) {
this.id = id;
this.comment = comment;
this.user = user;
this.board = board;
this.createdAt = createdAt;
}
}
@Transient
private boolean isReplyOwner;
Reply 에 isReplyOwner 를 추가한다. board 객체의 reply 필드에 포함되기 때문에 board 객체에 담을 수 있다.
BoardJPARepository
public Board 글상세보기(int boardId, User sessionUser) {
Board board = boardJPARepository.findByIdJoinUser(boardId)
.orElseThrow(() -> new Exception404("게시글을 찾을 수 없습니다"));
boolean isBoardOwner = false;
if (sessionUser != null) {
if (sessionUser.getId() == board.getUser().getId()) {
isBoardOwner = true;
}
}
board.setBoardOwner(isBoardOwner);
board.getReplies().forEach(reply ->{
boolean isReplyOwner = false ;
if(sessionUser!=null) {
if (reply.getUser().getId() == sessionUser.getId()) {
isReplyOwner =true;
}
}
reply.setReplyOwner(isReplyOwner);
});
return board;
}
반복문을 돌면서 isReplyOwner 값을 확인한다. 로그인이 되어있고, 댓글 작성 id와 로그인한 id를 비교한다.
{{#isReplyOwner}}
<form action="/reply/{{id}}/delete" method="post">
<button class="btn">🗑</button>
</form>
{{/isReplyOwner}}

작성한 댓글만 삭제 버튼이 활성화된다.
3. 댓글 삭제하기
ReplyService
@Transactional
public int 댓글삭제(Integer replyId,User sessionUser){
Reply reply = replyJPARepository.findById(replyId).orElseThrow(() -> new Exception404("댓글을 찾을 수 없습니다."));
int boardId = reply.getBoard().getId(); // 댓글 삭제 전 게시글 번호 남겨놓음
if(sessionUser.getId()!=reply.getUser().getId()){
throw new Exception403("댓글을 삭제할 권한이 없습니다.");
}
replyJPARepository.deleteById(replyId);
return boardId;
}
ReplyController
@PostMapping("/reply/{id}/delete")
public String delete(@PathVariable("id") Integer id){
User sessionUser = (User) session.getAttribute("sessionUser");
int boardId = replyService.댓글삭제(id,sessionUser);
return "redirect:/board/"+boardId ;
}
댓글이 삭제된 후 원래의 게시글로 리다이렉션을 위해서는 게시글 번호가 필요하다. reply에 board 객체가 있기 때문에 reply.getBoard().getId() 를 이용하면 되지만, 댓글 삭제 이후엔 reply 값이 null이 된다. 따라서 데이터가 삭제되기 전에 boardId 를 저장했다 삭제 후 boardId 만 리턴했다.
Share article