From 2a7eb42bbefa9a2fcb9bd99d124f3256411a8d7e Mon Sep 17 00:00:00 2001 From: tnals2384 Date: Tue, 1 Oct 2024 22:57:47 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20#202=20=EA=B2=8C=EC=8B=9C=EA=B8=80?= =?UTF-8?q?=20=EB=8B=A8=EA=B1=B4=20=EC=A1=B0=ED=9A=8C=20API=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/BoardController.java | 20 +++++++++---- .../domain/board/dto/BoardResponse.java | 2 +- .../domain/board/mapper/BoardMapper.java | 4 +-- .../board/repository/BoardRepository.java | 8 +++++ .../domain/board/service/BoardService.java | 2 ++ .../board/service/BoardServiceImpl.java | 30 +++++++++++++------ .../BoardCommentHeartRepository.java | 1 - 7 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/server/capple/domain/board/controller/BoardController.java b/src/main/java/com/server/capple/domain/board/controller/BoardController.java index 783cb6ea..31d412fe 100644 --- a/src/main/java/com/server/capple/domain/board/controller/BoardController.java +++ b/src/main/java/com/server/capple/domain/board/controller/BoardController.java @@ -34,7 +34,7 @@ public class BoardController { @ApiResponse(responseCode = "COMMON200", description = "성공"), }) @PostMapping() - private BaseResponse createBoard(@AuthMember Member member, + public BaseResponse createBoard(@AuthMember Member member, @RequestBody @Valid BoardRequest.BoardCreate request) { return BaseResponse.onSuccess(boardService.createBoard(member, request.getBoardType(), request.getContent())); } @@ -44,7 +44,7 @@ private BaseResponse createBoard(@AuthMember Member member, @ApiResponse(responseCode = "COMMON200", description = "성공"), }) @GetMapping("/redis") - private BaseResponse> getBoardsByBoardTypeWithRedis( + public BaseResponse> getBoardsByBoardTypeWithRedis( @AuthMember Member member, @RequestParam(name = "boardType", required = false) BoardType boardType, @Parameter(description = "Pull to Refresh 후 마지막 index") @@ -59,7 +59,7 @@ private BaseResponse> getBoardsByBoardTypeWithRedis( @ApiResponse(responseCode = "COMMON200", description = "성공"), }) @GetMapping() - private BaseResponse> getBoardsByBoardType( + public BaseResponse> getBoardsByBoardType( @AuthMember Member member, @RequestParam(name = "boardType", required = false) BoardType boardType, @Parameter(description = "Pull to Refresh 후 마지막 index") @@ -74,7 +74,7 @@ private BaseResponse> getBoardsByBoardType( @ApiResponse(responseCode = "COMMON200", description = "성공"), }) @GetMapping("/search") - private BaseResponse> searchBoardsByKeyword( + public BaseResponse> searchBoardsByKeyword( @AuthMember Member member, @RequestParam(name = "keyword") String keyword, @Parameter(description = "Pull to Refresh 후 마지막 index") @RequestParam(required = false) Long threshold, @@ -89,10 +89,20 @@ private BaseResponse> searchBoardsByKeyword( @ApiResponse(responseCode = "COMMON200", description = "성공"), }) @DeleteMapping("/{boardId}") - private BaseResponse deleteBoard(@AuthMember Member member, @PathVariable(name = "boardId") Long boardId) { + public BaseResponse deleteBoard(@AuthMember Member member, @PathVariable(name = "boardId") Long boardId) { return BaseResponse.onSuccess(boardService.deleteBoard(member, boardId)); } + @Operation(summary = "게시글 단건 조회 API", description = "게시글을 조회합니다.") + @ApiResponses(value = { + @ApiResponse(responseCode = "COMMON200", description = "성공"), + }) + @GetMapping("/{boardId}") + public BaseResponse getBoard(@AuthMember Member member, @PathVariable(value = "boardId") Long boardId) { + return BaseResponse.onSuccess(boardService.getBoard(member, boardId)); + } + + @Operation(summary = "게시글 좋아요/취소 API", description = " 게시글 좋아요/취소 API 입니다." + "pathVariable 으로 boardId를 주세요.") @PostMapping("/{boardId}/heart") diff --git a/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java b/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java index 7193957a..47c1e3f7 100644 --- a/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java +++ b/src/main/java/com/server/capple/domain/board/dto/BoardResponse.java @@ -25,7 +25,7 @@ public static class BoardInfo { private String content; private Integer heartCount; private Integer commentCount; - private LocalDateTime createAt; + private LocalDateTime createdAt; private Boolean isMine; private Boolean isReported; private Boolean isLiked; diff --git a/src/main/java/com/server/capple/domain/board/mapper/BoardMapper.java b/src/main/java/com/server/capple/domain/board/mapper/BoardMapper.java index 42dd52c8..7993c684 100644 --- a/src/main/java/com/server/capple/domain/board/mapper/BoardMapper.java +++ b/src/main/java/com/server/capple/domain/board/mapper/BoardMapper.java @@ -29,7 +29,7 @@ public BoardInfo toBoardInfo(Board board, Integer boardHeartsCount, Boolean isLi .content(board.getContent()) .heartCount(boardHeartsCount) .commentCount(board.getCommentCount()) - .createAt(board.getCreatedAt()) + .createdAt(board.getCreatedAt()) .isLiked(isLiked) .isMine(isMine) .isReported(board.getIsReport()) @@ -45,7 +45,7 @@ public BoardInfo toBoardInfo(Board board, Boolean isLiked, Boolean isMine) { .content(board.getContent()) .heartCount(board.getHeartCount()) .commentCount(board.getCommentCount()) - .createAt(board.getCreatedAt()) + .createdAt(board.getCreatedAt()) .isLiked(isLiked) .isMine(isMine) .isReported(board.getIsReport()) diff --git a/src/main/java/com/server/capple/domain/board/repository/BoardRepository.java b/src/main/java/com/server/capple/domain/board/repository/BoardRepository.java index e7664e41..01fe885b 100644 --- a/src/main/java/com/server/capple/domain/board/repository/BoardRepository.java +++ b/src/main/java/com/server/capple/domain/board/repository/BoardRepository.java @@ -28,6 +28,14 @@ public interface BoardRepository extends JpaRepository { Slice findBoardInfosByMemberAndKeywordAndIdIsLessThanEqual(Member member, String keyword, Long lastIndex, Pageable pageable); + @Query("SELECT b AS board, " + + "(CASE WHEN bh.isLiked = TRUE THEN TRUE ELSE FALSE END) AS isLike, " + + "(CASE WHEN b.writer = :member THEN TRUE ELSE FALSE END) AS isMine " + + "FROM Board b " + + "LEFT JOIN BoardHeart bh ON b = bh.board AND bh.member = :member " + + "WHERE b.id = :boardId") + BoardInfoInterface findBoardByMember(Member member, Long boardId); + //redis 성능 테스트용 @Query("SELECT DISTINCT b AS board, " + "(CASE WHEN b.writer = :member THEN TRUE ELSE FALSE END) AS isMine " + diff --git a/src/main/java/com/server/capple/domain/board/service/BoardService.java b/src/main/java/com/server/capple/domain/board/service/BoardService.java index e1d6dbee..a50eee4b 100644 --- a/src/main/java/com/server/capple/domain/board/service/BoardService.java +++ b/src/main/java/com/server/capple/domain/board/service/BoardService.java @@ -19,6 +19,8 @@ public interface BoardService { SliceResponse searchBoardsByKeyword(Member member, String keyword, Long lastIndex, Pageable pageable); + BoardInfo getBoard(Member member, Long boardId); + BoardId deleteBoard(Member member, Long boardId); ToggleBoardHeart toggleBoardHeart(Member member, Long boardId); diff --git a/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java b/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java index 899d9fa8..49d95175 100644 --- a/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java +++ b/src/main/java/com/server/capple/domain/board/service/BoardServiceImpl.java @@ -90,14 +90,26 @@ public SliceResponse getBoardsByBoardTypeWithRedis(Member member, Boa int heartCount = boardHeartRedisRepository.getBoardHeartsCount(sliceBoardInfo.getBoard().getId()); boolean isLiked = boardHeartRedisRepository.isMemberLikedBoard(member.getId(), sliceBoardInfo.getBoard().getId()); return boardMapper.toBoardInfo( - sliceBoardInfo.getBoard(), - heartCount, - isLiked, - sliceBoardInfo.getIsMine()); + sliceBoardInfo.getBoard(), + heartCount, + isLiked, + sliceBoardInfo.getIsMine()); }) .toList(), lastIndex.toString(), boardCountService.getBoardCount()); } + @Override + public BoardInfo getBoard(Member member, Long boardId) { + BoardInfoInterface boardInfo = boardRepository.findBoardByMember(member, boardId); + + return boardMapper.toBoardInfo( + boardInfo.getBoard(), + boardInfo.getIsLike(), + boardInfo.getIsMine() + ); + } + + @Override @Transactional public BoardId deleteBoard(Member member, Long boardId) { @@ -118,10 +130,10 @@ public ToggleBoardHeart toggleBoardHeart(Member member, Long boardId) { // 좋아요 눌렀는지 확인 //boardHeart에 없다면 새로 저장 BoardHeart boardHeart = boardHeartRepository.findByMemberAndBoard(member, board) - .orElseGet(() -> { - BoardHeart newHeart = boardHeartMapper.toBoardHeart(board, member); - return boardHeartRepository.save(newHeart); - }); + .orElseGet(() -> { + BoardHeart newHeart = boardHeartMapper.toBoardHeart(board, member); + return boardHeartRepository.save(newHeart); + }); boolean isLiked = boardHeart.toggleHeart(); board.setHeartCount(boardHeart.isLiked()); @@ -138,7 +150,7 @@ private void checkPermission(Member member, Board board) { @Override public Board findBoard(Long boardId) { return boardRepository.findById(boardId) - .orElseThrow(() -> new RestApiException(BoardErrorCode.BOARD_NOT_FOUND)); + .orElseThrow(() -> new RestApiException(BoardErrorCode.BOARD_NOT_FOUND)); } static class BoardCreatedEvent { diff --git a/src/main/java/com/server/capple/domain/boardComment/repository/BoardCommentHeartRepository.java b/src/main/java/com/server/capple/domain/boardComment/repository/BoardCommentHeartRepository.java index b869a6be..4463a644 100644 --- a/src/main/java/com/server/capple/domain/boardComment/repository/BoardCommentHeartRepository.java +++ b/src/main/java/com/server/capple/domain/boardComment/repository/BoardCommentHeartRepository.java @@ -9,5 +9,4 @@ public interface BoardCommentHeartRepository extends JpaRepository { Optional findByMemberAndBoardComment(Member member, BoardComment boardComment); - }