[스프링 부트 쇼핑몰 프로젝트 with JPA] 17-2. 게시판 - Controller

2023. 10. 16. 20:15·Web & Android/스프링 부트 쇼핑몰 프로젝트 with JPA
이 내용은 스프링 부트 쇼핑몰 프로젝트 with JPA 책을 학습한 내용입니다.

Controller

  • BoardController

      import com.example.walkingmate_back.board.dto.BoardRequestDTO;
      import com.example.walkingmate_back.board.dto.BoardResponseDTO;
      import com.example.walkingmate_back.board.dto.BoardUpdateDTO;
      import com.example.walkingmate_back.board.entity.Board;
      import com.example.walkingmate_back.board.service.BoardService;
      import com.example.walkingmate_back.main.response.ResponseMessage;
      import com.example.walkingmate_back.main.response.DefaultRes;
      import com.example.walkingmate_back.main.response.StatusEnum;
      import org.springframework.http.HttpStatus;
      import org.springframework.http.ResponseEntity;
      import org.springframework.security.core.Authentication;
      import org.springframework.web.bind.annotation.*;
      import java.util.List;
      import java.util.Optional;
    
      /**
       *    게시글 등록, 수정, 삭제, 단일 조회, 전체 조회
       *
       *   @version          1.00 / 2023.08.04
       *   @author           전우진
       */
    
      //@Controller
      @RestController
      @RequestMapping("/board")
      public class BoardController {
    
          private final BoardService boardService;
    
          public BoardController(BoardService boardService) {
              this.boardService = boardService;       
          }
    
          // 게시글 작성
          @PostMapping("/save")
          public ResponseEntity<DefaultRes<BoardResponseDTO>> saveBoard(@RequestBody BoardRequestDTO boardRequestDTO, Authentication authentication){
              BoardResponseDTO boardResponseDTO = boardService.saveBoard(boardRequestDTO, authentication.getName());
    
              if(boardResponseDTO != null)
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.OK, ResponseMessage.WRITE_BOARD, boardResponseDTO), HttpStatus.OK);
              else
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_USER, null), HttpStatus.OK);
          }
    
          // 게시글 수정
          @PutMapping("/{id}")
          public ResponseEntity<DefaultRes<BoardResponseDTO>> updateBoard(@PathVariable Long id, @RequestBody BoardUpdateDTO boardUpdateDTO, Authentication authentication) {
              Board board = boardService.FindBoard(id);
              if(board == null) return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_BOARD, null), HttpStatus.OK);
    
              BoardResponseDTO boardResponseDTO = boardService.updateBoard(board, boardUpdateDTO, authentication.getName());
    
              if(boardResponseDTO != null)
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.OK, ResponseMessage.UPDATE_BOARD, boardResponseDTO), HttpStatus.OK);
              else
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_USER, null), HttpStatus.OK);
          }
    
          // 게시글 삭제
          @DeleteMapping("/{id}")
          public ResponseEntity<DefaultRes<BoardResponseDTO>> deleteBoard(@PathVariable Long id, Authentication authentication) {
              Board board = boardService.FindBoard(id);
              if(board == null) return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_BOARD, null), HttpStatus.OK);
    
              BoardResponseDTO boardResponseDTO = boardService.deleteBoard(board, authentication.getName());
    
              if(boardResponseDTO != null)
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.OK, ResponseMessage.DELETE_BOARD, boardResponseDTO), HttpStatus.OK);
              else
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_USER, null), HttpStatus.OK);
          }
    
          // 단일 게시글 조회 - 댓글 포함
          @GetMapping("/{id}")
          public ResponseEntity<DefaultRes<BoardResponseDTO>> SpecificationBoard(@PathVariable Long id, Authentication authentication) {
              BoardResponseDTO boardResponseDTO = boardService.getBoard(id, authentication.getName());
    
              if(boardResponseDTO != null)
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.OK, ResponseMessage.READ_SUCCESS, boardResponseDTO), HttpStatus.OK);
              else
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_BOARD, null), HttpStatus.OK);
          }
    
          // 게시글 전체 조회 - 댓글 포함
          @GetMapping({"/list", "/list/{page}"})
          public ResponseEntity<DefaultRes<List<BoardResponseDTO>>> listBoard(@PathVariable Optional<Integer> page, Authentication authentication) {
              int pageNumber = page.orElse(1);
    
              List<BoardResponseDTO> boardResponseDTO = boardService.getAllBoard(pageNumber, authentication.getName());
    
              if(boardResponseDTO != null)
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.OK, ResponseMessage.READ_SUCCESS, boardResponseDTO), HttpStatus.OK);
              else
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_BOARD, null), HttpStatus.OK);
          }
    
      }
  • BoardCommetController

      import com.example.walkingmate_back.board.dto.*;
      import com.example.walkingmate_back.board.entity.BoardComment;
      import com.example.walkingmate_back.board.service.BoardCommentService;
      import com.example.walkingmate_back.main.response.DefaultRes;
      import com.example.walkingmate_back.main.response.ResponseMessage;
      import com.example.walkingmate_back.main.response.StatusEnum;
      import com.example.walkingmate_back.user.entity.UserEntity;
      import com.example.walkingmate_back.user.service.UserService;
      import org.springframework.http.HttpStatus;
      import org.springframework.http.ResponseEntity;
      import org.springframework.security.core.Authentication;
      import org.springframework.web.bind.annotation.*;
    
      /**
       *    댓글 등록, 수정, 삭제 - 대댓글 포함
       *
       *   @version          1.00 / 2023.08.07
       *   @author           전우진
       */
    
      //@Controller
      @RestController
      @RequestMapping("/board/comments")
      public class BoardCommentController {
    
          private final BoardCommentService boardCommentService;
          private final UserService userService;
    
          public BoardCommentController(BoardCommentService boardCommentService, UserService userService) {
              this.boardCommentService = boardCommentService;
              this.userService = userService;
          }
    
          // 댓글 저장
          @PostMapping("/save/{id}")
          public ResponseEntity<DefaultRes<BoardCommentResponseDTO>> saveComment(@PathVariable Long id, @RequestBody BoardCommentRequestDTO boardCommentRequestDTO, Authentication authentication) {
              UserEntity user = userService.FindUser(authentication.getName());
    
              if(user == null) return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_USER, null), HttpStatus.OK);
    
              BoardCommentResponseDTO boardCommentResponseDTO = boardCommentService.saveCommemt(boardCommentRequestDTO, user, id);
    
              if(boardCommentResponseDTO != null)
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.OK, ResponseMessage.WRITE_BOARDCOMMENT, boardCommentResponseDTO), HttpStatus.OK);
              else
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_BOARD, null), HttpStatus.OK);
          }
    
          // 댓글 수정
          @PutMapping("/{id}")
          public ResponseEntity<DefaultRes<BoardCommentResponseDTO>> updateComment(@PathVariable Long id, @RequestBody BoardCommentUpdateDTO boardCommentUpdateDTO, Authentication authentication) {
              BoardComment boardComment = boardCommentService.FindBoardComment(id);
              if(boardComment == null) return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_BOARDCOMMENT, null), HttpStatus.OK);
    
              BoardCommentResponseDTO boardCommentResponseDTO = boardCommentService.updateComment(boardComment, boardCommentUpdateDTO, authentication.getName());
    
              if(boardCommentResponseDTO != null)
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.OK, ResponseMessage.UPDATE_BOARDCOMMENT, boardCommentResponseDTO), HttpStatus.OK);
              else
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_USER, null), HttpStatus.OK);
          }
    
          // 댓글 삭제
          @DeleteMapping("/{id}")
          public ResponseEntity<DefaultRes<BoardCommentResponseDTO>> deleteComment(@PathVariable Long id, Authentication authentication) {
              BoardComment boardComment = boardCommentService.FindBoardComment(id);
              if(boardComment == null) return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_BOARDCOMMENT, null), HttpStatus.OK);
    
              BoardCommentResponseDTO boardCommentResponseDTO = boardCommentService.deleteComment(boardComment, authentication.getName());
    
              if(boardCommentResponseDTO != null)
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.OK, ResponseMessage.DELETE_BOARDCOMMENT, boardCommentResponseDTO), HttpStatus.OK);
              else
                  return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_BOARDCOMMENT, null), HttpStatus.OK);
          }
      }
  • *RecommemdController

    import com.example.walkingmate_back.board.dto.BoardCommentResponseDTO;
    import com.example.walkingmate_back.board.dto.BoardResponseDTO;
    import com.example.walkingmate_back.board.service.RecommendService;
    import com.example.walkingmate_back.main.response.DefaultRes;
    import com.example.walkingmate_back.main.response.ResponseMessage;
    import com.example.walkingmate_back.main.response.StatusEnum;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.security.core.Authentication;
    import org.springframework.web.bind.annotation.*;
    

/**

  • 게시글 좋아요, 댓글 좋아요 저장
  • @version 1.00 / 2023.08.30
  • @author 전우진
  • /

//@Controller
@RestController
@RequestMapping("/board/recommend")
public class RecommendController {

private final RecommendService recommendService;

public RecommendController(RecommendService recommendService) {
    this.recommendService = recommendService;
}

// 게시글 좋아요 저장
@PostMapping("/save/{id}")
public ResponseEntity<DefaultRes<BoardResponseDTO>> saveRecommend(@PathVariable Long id, Authentication authentication) {

    BoardResponseDTO boardResponseDTO = recommendService.saveRecommend(id, authentication.getName());

    if(boardResponseDTO != null)
        return new ResponseEntity<>(DefaultRes.res(StatusEnum.OK, ResponseMessage.WRITE_RECOMMEND, boardResponseDTO), HttpStatus.OK);
    else
        return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_BOARD, null), HttpStatus.OK);
}

// 댓글 좋아요 저장
@PostMapping("/comment/save/{id}")
public ResponseEntity<DefaultRes<BoardCommentResponseDTO>> saveRecommendComment(@PathVariable Long id, Authentication authentication) {

    BoardCommentResponseDTO boardCommentResponseDTO = recommendService.saveRecommendComment(id, authentication.getName());

    if(boardCommentResponseDTO != null)
        return new ResponseEntity<>(DefaultRes.res(StatusEnum.OK, ResponseMessage.WRITE_RECOMMEND, boardCommentResponseDTO), HttpStatus.OK);
    else
        return new ResponseEntity<>(DefaultRes.res(StatusEnum.BAD_REQUEST, ResponseMessage.NOT_FOUND_BOARDCOMMENT, null), HttpStatus.OK);
}

}
```

저작자표시 비영리 변경금지 (새창열림)

'Web & Android > 스프링 부트 쇼핑몰 프로젝트 with JPA' 카테고리의 다른 글

[스프링 부트 쇼핑몰 프로젝트 with JPA] 17-4. 게시판 - Repository  (0) 2023.10.16
[스프링 부트 쇼핑몰 프로젝트 with JPA] 17-3. 게시판 - Service  (0) 2023.10.16
[스프링 부트 쇼핑몰 프로젝트 with JPA] 17-1. 게시판 - Entity  (1) 2023.10.16
[스프링 부트 쇼핑몰 프로젝트 with JPA] 16.장바구니 상품 삭제 & 주문  (0) 2023.10.16
[스프링 부트 쇼핑몰 프로젝트 with JPA] 15. 장바구니 조회  (1) 2023.10.16
'Web & Android/스프링 부트 쇼핑몰 프로젝트 with JPA' 카테고리의 다른 글
  • [스프링 부트 쇼핑몰 프로젝트 with JPA] 17-4. 게시판 - Repository
  • [스프링 부트 쇼핑몰 프로젝트 with JPA] 17-3. 게시판 - Service
  • [스프링 부트 쇼핑몰 프로젝트 with JPA] 17-1. 게시판 - Entity
  • [스프링 부트 쇼핑몰 프로젝트 with JPA] 16.장바구니 상품 삭제 & 주문
woojin._.
woojin._.
여러가지 개발을 해보며 발생하는 이야기들에 대한 블로그입니다:)
  • woojin._.
    Jin's Dev Story
    woojin._.
  • 전체
    오늘
    어제
    • 분류 전체보기 (829)
      • Tools (25)
        • eGovFrame (3)
        • GeoServer (3)
        • QGIS (2)
        • LabelImg (2)
        • Git (6)
        • GitHub (1)
        • Eclipse (7)
        • Visual Studio (1)
      • Web & Android (121)
        • SpringBoot (37)
        • Three.js (2)
        • Spring Data JPA (9)
        • 스프링 부트 쇼핑몰 프로젝트 with JPA (25)
        • Thymeleaf (4)
        • Spring Security (15)
        • Flutter (29)
      • Programming Language (61)
        • JAVA (27)
        • JavaScript (14)
        • Dart (2)
        • Python (15)
        • PHP (3)
      • Database (43)
        • PostgreSQL (32)
        • MYSQL (7)
        • Oracle (3)
        • MSSQL (1)
      • SERVER (17)
        • TCP_IP (3)
        • 리눅스 (7)
        • AWS (7)
      • Coding Test (445)
        • 백준[JAVA] (108)
        • 프로그래머스[JAVA] (260)
        • 알고리즘 고득점 Kit[JAVA] (3)
        • SQL 고득점 Kit[ORACLE] (74)
      • CS 지식 (49)
        • [자료구조] (14)
        • [네트워크] (12)
        • [데이터베이스] (10)
        • [알고리즘] (9)
        • [운영체제] (4)
      • 기타 (6)
      • 자격증 & 공부 (62)
        • 정보처리기사 (2)
        • SQLD (6)
        • 네트워크관리사 2급 (5)
        • 리눅스마스터 1급 (44)
        • 리눅스마스터 2급 (1)
        • ISTQB (3)
        • 시스템보안 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 인기 글

  • 태그

    자바
    리눅스마스터
    Spring Security
    programmers
    DB
    시큐리티
    프로그래머스
    Oracle
    스프링부트
    springboot
    backjoon
    스프링
    Flutter
    리눅스
    데이터
    Java
    플러터
    리눅스마스터 1급
    python
    CS지식
    baekjoon
    데이터베이스
    CS
    스프링 부트 쇼핑몰 프로젝트 with JPA
    JPA
    Linux
    백준
    spring
    pcce 기출문제
    postgresql
  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
woojin._.
[스프링 부트 쇼핑몰 프로젝트 with JPA] 17-2. 게시판 - Controller
상단으로

티스토리툴바