[ 댓글 쓰기 view 수정 - detail.mustache ]
해당 부분도 추가함. 보일 필요가 없어서 hidden으로 함.
[ ReplyRequest ] - DTO 생성
package shop.mtcoding.blog.reply; import lombok.Data; import shop.mtcoding.blog.user.User; public class ReplyRequest { @Data public static class SaveDTO { private Integer boardId; private String comment; public Reply toEntity(User sessionUser) { return Reply.builder() .comment(comment) .board(null) .user(sessionUser) .build(); } } }
board에는 Board 객체를 넣어줘야함.
(보드가 있어야지 댓글을 쓸 수 있으니 조회해서 넣어야함)
-> 지금은 못하기 때문에 null을 기입
이렇게 해도 되긴 하는데... 없는게 들어갈 수도 있으니 위험한 코드 !
[ ReplyController ]
package shop.mtcoding.blog.reply; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; 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 requestDTO) { User sessionUser = (User) session.getAttribute("sessionUser"); replyService.댓글쓰기(requestDTO, sessionUser); return "redirect:/board/" + requestDTO.getBoardId(); } }
[ 인터셉터 수정 ]
리플라이 객체도 추가 인증이 필요하다고 설정해주자
[ ReplyService ] - 정방향 insert
package shop.mtcoding.blog.reply; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import shop.mtcoding.blog._core.errs.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 ReplyJPARepository replyJPARepository; private final BoardJPARepository boardJPARepository; @Transactional public void 댓글쓰기(ReplyRequest.SaveDTO requestDTO, User sessionUser) { Board board = boardJPARepository.findById(requestDTO.getBoardId()) .orElseThrow(() -> new Exception404("없는 게시글에 댓글을 작성할 수 없어요")); Reply reply = requestDTO.toEntity(sessionUser, board); replyJPARepository.save(reply); } }
이제 만들어졌죠? 그럼 아까 DTO에서 null처리 했던 거 추가하러 가자!
[ ReplyRequest 수정 ]
package shop.mtcoding.blog.reply; import lombok.Data; import shop.mtcoding.blog.board.Board; import shop.mtcoding.blog.user.User; public class ReplyRequest { @Data public static class SaveDTO { private Integer boardId; private String comment; public Reply toEntity(User sessionUser, Board board) { return Reply.builder() .comment(comment) .board(board) .user(sessionUser) .build(); } } }
여기까지 이렇게 하면 SAVE가 되어요
[ 정렬하기 ]
댓글도 DESC 정렬하고 싶다. @OrderBy를 사용하자!
[ ReplyOwner도 설정해주기 ]
is 생략 가능하다! isOwner를 boardOwner로 바꿔도 됨
<!-- 댓글 --> <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="/reply/{{id}}/delete" method="post"> <button class="btn">🗑</button> </form> {{/replyOwner}} </div> {{/board.replies}} <!-- 댓글아이템 -->
[ 화면 확인 ]
Owner 여부 작동 잘 됨
댓글 쓰기도 잘 됨
Share article