728x90
최종 코드
CommentController
package com.example.board.controller;
import com.example.board.dto.Comment;
import com.example.board.dto.LoginInfo;
import com.example.board.dto.Post;
import com.example.board.service.CommentService;
import com.example.board.service.PostService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller
@RequiredArgsConstructor
public class CommentController {
private final CommentService commentService;
private final PostService postService;
@GetMapping("/detail")
public String detail(@RequestParam("postId") int postId, HttpSession session, Model model) {
LoginInfo loginInfo = (LoginInfo)session.getAttribute("loginInfo");
model.addAttribute("loginInfo", loginInfo);
// 게시글 정보 가져오기 (조회수 증가 안 함)
Post post = postService.getPost(postId, false);
model.addAttribute("post", post);
// 댓글 목록 가져오기
List<Comment> comments = commentService.getComments(postId);
model.addAttribute("comments", comments);
return "detail";
}
@PostMapping("/comment/add")
public String addComment(
@RequestParam("postId") int postId,
@RequestParam("content") String content,
@RequestParam(value = "parentId", required = false, defaultValue = "") String parentIdStr,
HttpSession session) {
LoginInfo loginInfo = (LoginInfo)session.getAttribute("loginInfo");
if(loginInfo == null) {
return "redirect:/loginform";
}
Integer parentId = null;
if(parentIdStr != null && !parentIdStr.isEmpty()) {
try {
parentId = Integer.parseInt(parentIdStr);
} catch (NumberFormatException e) {
parentId = null;
}
}
commentService.addComment(postId, loginInfo.getUserId(), content, parentId);
return "redirect:/detail?postId=" + postId;
}
@GetMapping("/comment/delete")
public String deleteComment(
@RequestParam("commentId") int commentId,
@RequestParam("postId") int postId,
HttpSession session) {
LoginInfo loginInfo = (LoginInfo)session.getAttribute("loginInfo");
if(loginInfo == null) {
return "redirect:/loginform";
}
commentService.deleteComment(commentId, postId, loginInfo.getUserId());
return "redirect:/detail?postId=" + postId;
}
@PostMapping("/comment/update")
public String updateComment(
@RequestParam("commentId") int commentId,
@RequestParam("postId") int postId,
@RequestParam("content") String content,
HttpSession session) {
LoginInfo loginInfo = (LoginInfo)session.getAttribute("loginInfo");
if(loginInfo == null) {
return "redirect:/loginform";
}
commentService.updateComment(commentId, content);
return "redirect:/detail?postId=" + postId;
}
}
CommentService
package com.example.board.service;
import com.example.board.dao.CommentDao;
import com.example.board.dao.PostDao;
import com.example.board.dto.Comment;
import com.example.board.dto.Post;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
@Service
@RequiredArgsConstructor
public class CommentService {
private final CommentDao commentDao;
private final PostDao postDao;
@Transactional
public void addComment(int postId, int userId, String content, Integer parentId) { commentDao.addComment(postId, userId, content, parentId); }
@Transactional(readOnly = true)
public List<Comment> getComments(int postId) {
return commentDao.getComments(postId);
}
@Transactional
public Comment getComment(int commentId) { return commentDao.getComment(commentId); }
@Transactional
public void deleteComment(int commentId, int postId, int userId) {
Comment comment = commentDao.getComment(commentId);
Post post = postDao.getPost(postId);
if((comment.getUserId() == userId) || (post.getUserId() == userId)) {
commentDao.deleteComment(commentId);
}
}
@Transactional
public void updateComment(int commentId, String content) {
commentDao.updateComment(commentId, content);
}
}
Comment
package com.example.board.dto;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import java.time.LocalDateTime;
@Getter
@Setter
@NoArgsConstructor
@ToString
public class Comment {
private int commentId;
private int postId;
private int userId;
private Integer parentId;
private String name;
private String content;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
}
728x90
'백엔드 > Java' 카테고리의 다른 글
| 🧠 자바 면접 질문 정리 및 답변 예시 (0) | 2025.10.12 |
|---|---|
| 동시성(Concurrency)과의 싸움: 자바 멀티스레딩의 이해와 동기화 기법 (0) | 2025.10.11 |
| ☕️ 자바 개발자의 필수 지식: JVM의 작동 원리 깊이 이해하기 - 메모리 구조와 가비지 컬렉션(GC) (0) | 2025.10.09 |
| 🔥 자바 8 이후 필수 문법: 람다(Lambda)와 스트림(Stream) API 활용법 (0) | 2025.10.08 |
| 🌟 예외(Exception) 처리, 깔끔하게 하는 법: try-catch-finally 실전 노하우 (0) | 2025.10.07 |