Skip to content

Commit

Permalink
Merge pull request #203 from Team-Capple/feat/#202/getBoardAPI
Browse files Browse the repository at this point in the history
[FEAT] 게시글 단건 조회 API 구현
  • Loading branch information
jaewonLeeKOR authored Oct 1, 2024
2 parents 0470af0 + 2a7eb42 commit b4f4718
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class BoardController {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
})
@PostMapping()
private BaseResponse<BoardId> createBoard(@AuthMember Member member,
public BaseResponse<BoardId> createBoard(@AuthMember Member member,
@RequestBody @Valid BoardRequest.BoardCreate request) {
return BaseResponse.onSuccess(boardService.createBoard(member, request.getBoardType(), request.getContent()));
}
Expand All @@ -44,7 +44,7 @@ private BaseResponse<BoardId> createBoard(@AuthMember Member member,
@ApiResponse(responseCode = "COMMON200", description = "성공"),
})
@GetMapping("/redis")
private BaseResponse<SliceResponse<BoardInfo>> getBoardsByBoardTypeWithRedis(
public BaseResponse<SliceResponse<BoardInfo>> getBoardsByBoardTypeWithRedis(
@AuthMember Member member,
@RequestParam(name = "boardType", required = false) BoardType boardType,
@Parameter(description = "Pull to Refresh 후 마지막 index")
Expand All @@ -59,7 +59,7 @@ private BaseResponse<SliceResponse<BoardInfo>> getBoardsByBoardTypeWithRedis(
@ApiResponse(responseCode = "COMMON200", description = "성공"),
})
@GetMapping()
private BaseResponse<SliceResponse<BoardInfo>> getBoardsByBoardType(
public BaseResponse<SliceResponse<BoardInfo>> getBoardsByBoardType(
@AuthMember Member member,
@RequestParam(name = "boardType", required = false) BoardType boardType,
@Parameter(description = "Pull to Refresh 후 마지막 index")
Expand All @@ -74,7 +74,7 @@ private BaseResponse<SliceResponse<BoardInfo>> getBoardsByBoardType(
@ApiResponse(responseCode = "COMMON200", description = "성공"),
})
@GetMapping("/search")
private BaseResponse<SliceResponse<BoardInfo>> searchBoardsByKeyword(
public BaseResponse<SliceResponse<BoardInfo>> searchBoardsByKeyword(
@AuthMember Member member, @RequestParam(name = "keyword") String keyword,
@Parameter(description = "Pull to Refresh 후 마지막 index")
@RequestParam(required = false) Long threshold,
Expand All @@ -89,10 +89,20 @@ private BaseResponse<SliceResponse<BoardInfo>> searchBoardsByKeyword(
@ApiResponse(responseCode = "COMMON200", description = "성공"),
})
@DeleteMapping("/{boardId}")
private BaseResponse<BoardId> deleteBoard(@AuthMember Member member, @PathVariable(name = "boardId") Long boardId) {
public BaseResponse<BoardId> 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<BoardInfo> 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public interface BoardRepository extends JpaRepository<Board, Long> {
Slice<BoardInfoInterface> 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 " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public interface BoardService {

SliceResponse<BoardInfo> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,26 @@ public SliceResponse<BoardInfo> 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) {
Expand All @@ -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());
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@

public interface BoardCommentHeartRepository extends JpaRepository<BoardCommentHeart,Long> {
Optional<BoardCommentHeart> findByMemberAndBoardComment(Member member, BoardComment boardComment);

}

0 comments on commit b4f4718

Please sign in to comment.