40. 댓글삭제하기v6

송민경's avatar
Mar 19, 2024
40. 댓글삭제하기v6

1. detail.mustache에 주소 수정하기

  • boardId와 replyId 두 개가 필요함!
{{> /layout/header}} <div class="container p-5"> {{#board.boardOwner}} <!-- 수정삭제버튼 --> <div class="d-flex justify-content-end"> <a href="/board/{{board.id}}/update-form" class="btn btn-warning me-1">수정</a> <form action="/board/{{board.id}}/delete" method="post"> <button class="btn btn-danger">삭제</button> </form> </div> {{/board.boardOwner}} <div class="d-flex justify-content-end"> <b>작성자</b> : {{board.user.username}} </div> <!-- 게시글내용 --> <div> <h2><b>{{board.title}}</b></h2> <hr/> <div class="m-4 p-2"> {{board.content}} </div> </div> <!-- 댓글 --> <div class="card mt-3"> <!-- 댓글등록 --> <div class="card-body"> <form action="/reply/save" method="post"> <input type="hidden" name="boardId" value="{{board.id}}"> <textarea class="form-control" rows="2" name="comment"></textarea> <div class="d-flex justify-content-end"> <button type="submit" class="btn btn-outline-primary mt-1">댓글등록</button> </div> </form> </div> <!-- 댓글목록 --> <div class="card-footer"> <b>댓글리스트</b> </div> <div class="list-group"> {{#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> {{#replyOwner}} <form action="/board/{{board.id}}/reply/{{id}}/delete" method="post"> <button class="btn">🗑</button> </form> {{/replyOwner}} </div> {{/board.replies}} </div> </div> </div> {{> /layout/footer}}
 

2. ReplyController에서 delete 만들기

  • boardId와 replyId 두 개가 필요함!
package shop.mtcoding.blog.reply; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import shop.mtcoding.blog.user.User; @RequiredArgsConstructor @Controller public class ReplyController { private final ReplyService replyService; private final HttpSession session; @PostMapping("/reply/save") public String save(ReplyRequest.SaveDTO reqDTO) { User sessionUser = (User) session.getAttribute("sessionUser"); replyService.save(reqDTO, sessionUser); return "redirect:/board/" + reqDTO.getBoardId(); } @PostMapping("/board/{boardId}/reply/{replyId}/delete") public String delete(@PathVariable Integer boardId, @PathVariable Integer replyId) { User sessionUser = (User) session.getAttribute("sessionUser"); replyService.delete(replyId, sessionUser.getId()); return "redirect:/board/" + boardId; } }
 

3. ReplyService 에서 delete() 만들기

package shop.mtcoding.blog.reply; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import shop.mtcoding.blog._core.errors.exception.Exception403; import shop.mtcoding.blog._core.errors.exception.Exception404; import shop.mtcoding.blog.board.Board; import shop.mtcoding.blog.board.BoardJPARepository; import shop.mtcoding.blog.user.User; @RequiredArgsConstructor @Service public class ReplyService { private final BoardJPARepository boardJPARepository; private final ReplyJPARepository replyJPARepository; @Transactional public void delete(int boardId, Integer sessionUserId) { Reply reply = replyJPARepository.findById(boardId) .orElseThrow(() -> new Exception404("댓글을 찾을 수 없습니다")); if (sessionUserId != reply.getUser().getId()) { throw new Exception403("댓글을 삭제할 권한이 없습니다"); } replyJPARepository.deleteById(boardId); } @Transactional public void save(ReplyRequest.SaveDTO reqDTO, User sessionUser) { Board board = boardJPARepository.findById(reqDTO.getBoardId()) .orElseThrow(() -> new Exception404("없는 게시글에 댓글을 작성할 수 없어요")); Reply reply = reqDTO.toEntity(sessionUser, board); replyJPARepository.save(reply); } }
notion image
notion image
Share article
RSSPowered by inblog