Skip to content

Commit

Permalink
✨ feat: 마이페이지 커뮤니티 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
jinho7 committed Aug 17, 2024
1 parent 0fa256d commit 072328d
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,40 @@ public ApiResponse<BoardResponseDTO.BoardListDTO> getBoardList(

@Operation(summary = "게시글 상세 조회", description = "지정된 ID의 게시글 상세 정보를 조회합니다.")
@GetMapping("/{boardId}")
public ApiResponse<BoardResponseDTO.BoardDetailDTO> getBoardDetail(@PathVariable("boardId") Long boardId,
public ApiResponse<BoardResponseDTO.BoardDTO> getBoardDetail(@PathVariable("boardId") Long boardId,
@AuthenticatedMember Member member) {
return ApiResponse.onSuccess(boardQueryService.getBoardDetail(boardId, member));
}

@Operation(summary = "내가 쓴 게시글 조회", description = "사용자가 작성한 게시글 목록을 최신순으로 커서 기반 페이지네이션으로 조회합니다.")
@GetMapping("/my-posts")
public ApiResponse<BoardResponseDTO.BoardListDTO> getMyPosts(
@RequestParam(defaultValue = "0") Long cursor,
@RequestParam(defaultValue = "10") Integer limit,
@AuthenticatedMember Member member) {
return ApiResponse.onSuccess(boardQueryService.getMyPosts(cursor, limit, member));
}

@Operation(summary = "내가 댓글 단 게시글 조회", description = "사용자가 댓글을 작성한 게시글 목록을 최신순으로 커서 기반 페이지네이션으로 조회합니다.")
@GetMapping("/my-comments")
public ApiResponse<BoardResponseDTO.BoardListDTO> getMyCommentedPosts(
@RequestParam(defaultValue = "0") Long cursor,
@RequestParam(defaultValue = "10") Integer limit,
@AuthenticatedMember Member member) {
return ApiResponse.onSuccess(boardQueryService.getMyCommentedPosts(cursor, limit, member));
}

@Operation(summary = "내가 좋아요 한 게시글 조회", description = "사용자가 좋아요를 누른 게시글 목록을 최신순으로 커서 기반 페이지네이션으로 조회합니다.")
@GetMapping("/my-likes")
public ApiResponse<BoardResponseDTO.BoardListDTO> getMyLikedPosts(
@RequestParam(defaultValue = "0") Long cursor,
@RequestParam(defaultValue = "10") Integer limit,
@AuthenticatedMember Member member) {
return ApiResponse.onSuccess(boardQueryService.getMyLikedPosts(cursor, limit, member));
}

// CommandService
@Operation(summary = "이미지 업로드", description = "게시글에 첨부할 이미지를 업로드합니다.")
@Operation(summary = "커뮤니티 게시글 이미지 업로드", description = "게시글에 첨부할 이미지를 업로드합니다.")
@PostMapping(value = "/images", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ApiResponse<BoardResponseDTO.BoardImgDTO> uploadBoardImages(
@RequestPart("images") List<MultipartFile> images) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ApiResponse<CommentResponseDTO.CommentsListDTO> getComments(
}

// CommandService
@Operation(summary = "이미지 업로드", description = "댓글에 첨부할 이미지를 업로드합니다.")
@Operation(summary = "커뮤니티 댓글 이미지 업로드", description = "댓글에 첨부할 이미지를 업로드합니다.")
@PostMapping(value = "/comments/images", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ApiResponse<CommentResponseDTO.CommentImgDTO> uploadCommentImages(
@RequestPart("images") List<MultipartFile> images) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,6 @@ public static BoardListDTO of(List<Board> boards, Long nextCursor, boolean hasNe
}
}

@Getter
@Builder
public static class BoardDetailDTO {
private BoardDTO board;

public static BoardDetailDTO of(Board board, Member currentMember) {
return BoardDetailDTO.builder()
.board(BoardDTO.from(board, currentMember.getId()))
.build();
}
}

@Getter
@Builder
public static class BoardStatusDTO {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,13 @@ public interface BoardRepository extends JpaRepository<Board, Long> {
@Query("SELECT b FROM Board b WHERE b.category = :category AND b.id < :cursor ORDER BY b.id DESC")
List<Board> findByCategoryOrderByLatestWithCursor(@Param("category") Category category, @Param("cursor") Long cursor, Pageable pageable);

default List<Board> findAllOrderByLikesWithCursor(Long cursor, Integer limit) {
return findAllOrderByLikesWithCursor(cursor, PageRequest.of(0, limit));
}
@Query("SELECT b FROM Board b WHERE b.member.id = :memberId AND b.id < :cursor ORDER BY b.id DESC")
List<Board> findMyPosts(@Param("memberId") Long memberId, @Param("cursor") Long cursor, Pageable pageable);

default List<Board> findAllOrderByLatestWithCursor(Long cursor, Integer limit) {
return findAllOrderByLatestWithCursor(cursor, PageRequest.of(0, limit));
}
@Query("SELECT DISTINCT b FROM Board b JOIN b.comments c WHERE c.member.id = :memberId AND b.id < :cursor ORDER BY b.id DESC")
List<Board> findMyCommentedPosts(@Param("memberId") Long memberId, @Param("cursor") Long cursor, Pageable pageable);

default List<Board> findByCategoryOrderByLikesWithCursor(Category category, Long cursor, Integer limit) {
return findByCategoryOrderByLikesWithCursor(category, cursor, PageRequest.of(0, limit));
}

default List<Board> findByCategoryOrderByLatestWithCursor(Category category, Long cursor, Integer limit) {
return findByCategoryOrderByLatestWithCursor(category, cursor, PageRequest.of(0, limit));
}
@Query("SELECT b FROM Board b JOIN BoardLike bl ON b.id = bl.board.id WHERE bl.member.id = :memberId AND b.id < :cursor ORDER BY b.id DESC")
List<Board> findMyLikedPosts(@Param("memberId") Long memberId, @Param("cursor") Long cursor, Pageable pageable);
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ BoardResponseDTO.BoardListDTO getBoardList(Category category,
SortType sortType,
Member member);

BoardResponseDTO.BoardDetailDTO getBoardDetail(Long boardId, Member member);
BoardResponseDTO.BoardDTO getBoardDetail(Long boardId, Member member);

BoardResponseDTO.BoardListDTO getMyPosts(Long cursor, Integer limit, Member member);

BoardResponseDTO.BoardListDTO getMyCommentedPosts(Long cursor, Integer limit, Member member);

BoardResponseDTO.BoardListDTO getMyLikedPosts(Long cursor, Integer limit, Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.example.template.domain.board.repository.BoardRepository;
import com.example.template.domain.member.entity.Member;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -27,35 +28,66 @@ public BoardResponseDTO.BoardListDTO getBoardList(Category category,
Integer limit,
SortType sortType,
Member member) {
// 첫 페이지 로딩 시 매우 큰 ID 값 사용
if (cursor == 0) {
cursor = Long.MAX_VALUE;
}

List<Board> boards;
// 전체 조회
if (category == null) {
boards = sortType == SortType.LIKES
? boardRepository.findAllOrderByLikesWithCursor(cursor, limit)
: boardRepository.findAllOrderByLatestWithCursor(cursor, limit);
}
// 카테고리별 조회
else {
boards = sortType == SortType.LIKES
? boardRepository.findByCategoryOrderByLikesWithCursor(category, cursor, limit)
: boardRepository.findByCategoryOrderByLatestWithCursor(category, cursor, limit);
}

Long nextCursor = boards.isEmpty() ? null : boards.get(boards.size() - 1).getId();
boolean hasNext = boards.size() == limit;

return BoardResponseDTO.BoardListDTO.of(boards, nextCursor, hasNext, member.getId());
cursor = initializeCursor(cursor);
List<Board> boards = fetchBoards(category, cursor, limit, sortType);
return createBoardListDTO(boards, limit, member.getId());
}

@Override
public BoardResponseDTO.BoardDetailDTO getBoardDetail(Long boardId, Member member) {
public BoardResponseDTO.BoardDTO getBoardDetail(Long boardId, Member member) {
Board board = boardRepository.findById(boardId)
.orElseThrow(() -> new BoardException(BoardErrorCode.BOARD_NOT_FOUND));
return BoardResponseDTO.BoardDetailDTO.of(board, member);
return BoardResponseDTO.BoardDTO.from(board, member.getId());
}

public BoardResponseDTO.BoardListDTO getMyPosts(Long cursor, Integer limit, Member member) {
cursor = initializeCursor(cursor);
PageRequest pageRequest = PageRequest.of(0, limit + 1);
List<Board> boards = boardRepository.findMyPosts(member.getId(), cursor, pageRequest);
return createBoardListDTO(boards, limit, member.getId());
}

public BoardResponseDTO.BoardListDTO getMyCommentedPosts(Long cursor, Integer limit, Member member) {
cursor = initializeCursor(cursor);
PageRequest pageRequest = PageRequest.of(0, limit + 1);
List<Board> boards = boardRepository.findMyCommentedPosts(member.getId(), cursor, pageRequest);
return createBoardListDTO(boards, limit, member.getId());
}

public BoardResponseDTO.BoardListDTO getMyLikedPosts(Long cursor, Integer limit, Member member) {
cursor = initializeCursor(cursor);
PageRequest pageRequest = PageRequest.of(0, limit + 1);
List<Board> boards = boardRepository.findMyLikedPosts(member.getId(), cursor, pageRequest);
return createBoardListDTO(boards, limit, member.getId());
}

private Long initializeCursor(Long cursor) {
return (cursor == 0) ? Long.MAX_VALUE : cursor;
}

private List<Board> fetchBoards(Category category, Long cursor, Integer limit, SortType sortType) {
PageRequest pageRequest = PageRequest.of(0, limit + 1);
if (category == null) {
if (sortType == SortType.LIKES) {
return boardRepository.findAllOrderByLikesWithCursor(cursor, pageRequest);
} else {
return boardRepository.findAllOrderByLatestWithCursor(cursor, pageRequest);
}
} else {
if (sortType == SortType.LIKES) {
return boardRepository.findByCategoryOrderByLikesWithCursor(category, cursor, pageRequest);
} else {
return boardRepository.findByCategoryOrderByLatestWithCursor(category, cursor, pageRequest);
}
}
}

private BoardResponseDTO.BoardListDTO createBoardListDTO(List<Board> boards, Integer limit, Long memberId) {
boolean hasNext = boards.size() > limit;
if (hasNext) {
boards = boards.subList(0, limit);
}
Long nextCursor = hasNext ? boards.get(boards.size() - 1).getId() : null;
return BoardResponseDTO.BoardListDTO.of(boards, nextCursor, hasNext, memberId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public ApiResponse<MemberResponseDTO.LoginResultDTO> loginOrSignupByKakao(@Valid
return ApiResponse.onSuccess(kakaoService.loginOrSignupByKakao(requestDTO));
}


@Operation(summary = "로그아웃")
@PostMapping("/logout")
public ApiResponse<String> logout(HttpServletRequest request) {
Expand Down

0 comments on commit 072328d

Please sign in to comment.