이 내용은 스프링 부트 쇼핑몰 프로젝트 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 |