본문 바로가기

Spring boot/3차 프로젝트 중 알게 된 것!

[3차 프로젝트] 게시글에 들어갔을 때 댓글 허용인 게시판인지 사용 여부 판단을 위해 controller 수정하기

 

 

=================================최종 ===================================
    @GetMapping("/post/{postNo}")
    public String read(@PathVariable("postNo") int postNo, Model model, HttpServletRequest request, HttpServletResponse response) {

        // ######### 게시글 상세정보 시작 ######### //
        PostVo post = this.postService.retrieveDetailBoard(postNo);

        // ######해당 게시판에서 댓글 사용 여부 판단######      <=====수정부분
        int useComment = this.boardService.retrieveBoard(post.getBoardNo()).getUseComment();
            model.addAttribute("useComment", useComment);

===============================================

 

==============처음 내가 짠 것=================

  @GetMapping("/post/{postNo}")
    public String read(@PathVariable("postNo"), int postNo, Model model, HttpServletRequest request, HttpServletResponse response) {

        // ######### 게시글 상세정보 시작 ######### //
        PostVo post = this.postService.retrieveDetailBoard(postNo);

        int useComment = this.boardService.retrieveBoard(int boardNo);

        if (useComment == 0){
            model.addAttribute("useComment", useComment);
        }else{
            useComment = 1;
            model.addAttribute("useComment", useComment);
        }
===================================================================

 

라고 적었었는데 내가 원하는건 usecomment를 갖고 오는거
        PostVo post = this.postService.retrieveDetailBoard(postNo);
=> 이 부분에서 해당 게시글에 대한 정보를 가져오는 거니까
 
        int useComment = this.boardService.retrieveBoard(int boardNo); =>         int useComment = this.boardService.retrieveBoard(post getboarNo()).getComment();
라고써야 위에 적혀있는 코드에서 게시글 정보를 조회하고 그 정보에서 usecomment값을 꺼내온다
그리고 위의 과정이 usecomment에 대해 정의만 하는 거라고 생각했었는데 값을 꺼내오는거니까(0인지 1인지, 사용여부) 그냥 model로 전달만 해줘도 된다
따라서

        if (useComment == 0){
            model.addAttribute("useComment", useComment);
        }else{
            useComment = 1;
            model.addAttribute("useComment", useComment);
        }
이 부분을 지우고 
 model.addAttribute("useComment", useComment); 모델부분만 놔두면 된다!

728x90