백엔드/Java

[개인 프로젝트] 게시판 만들기 끝

hawon6691 2025. 10. 14. 17:49
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