Skip to content

Commit

Permalink
feat: #128 카테고리별 게시글 조회 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
kyxxgsoo committed Aug 19, 2024
1 parent fb80dc8 commit 7ee47d1
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
import com.server.capple.config.security.AuthMember;
import com.server.capple.domain.board.dto.BoardRequest;
import com.server.capple.domain.board.dto.BoardResponse;
import com.server.capple.domain.board.entity.BoardType;
import com.server.capple.domain.board.service.BoardService;
import com.server.capple.domain.member.entity.Member;
import com.server.capple.domain.question.dto.response.QuestionResponse;
import com.server.capple.global.common.BaseResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

@Tag(name = "게시판 API", description = "게시판 관련 API")
Expand All @@ -34,4 +39,17 @@ private BaseResponse<BoardResponse.BoardCreate> createBoard(
return BaseResponse.onSuccess(boardService.createBoard(member, request.getBoardType(), request.getContent()));
}

@Operation(summary = "카테고리별 게시판 조회 API", description = "카테고리별 게시판을 생성합니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
})
@GetMapping()
private BaseResponse<BoardResponse.BoardsGetByBoardType> getBoardsByBoardType(
@RequestParam(name = "boardType", required = false) BoardType boardType
// TODO: 페이징 프론트 이슈로 추후 구현
// @PageableDefault(sort = "created_at", direction = Sort.Direction.DESC) @Parameter(hidden = true) Pageable pageable
) {
return BaseResponse.onSuccess(boardService.getBoardsByBoardType(boardType));
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.server.capple.domain.board.dto;

import com.server.capple.domain.member.entity.Member;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class BoardResponse {

@Getter
Expand All @@ -14,4 +19,24 @@ public class BoardResponse {
public static class BoardCreate {
private Long boardId;
}

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class BoardsGetByBoardType {
private List<BoardsGetByBoardTypeBoardInfo> boards = new ArrayList<>();
}

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class BoardsGetByBoardTypeBoardInfo {
private Long writerId;
private String content;
private Integer heartCount;
private Integer commentCount;
private LocalDateTime createAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.server.capple.domain.member.entity.Member;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class BoardMapper {

Expand All @@ -33,4 +35,24 @@ public BoardResponse.BoardCreate toBoardCreate(
.boardId(board.getId())
.build();
}

public BoardResponse.BoardsGetByBoardType toBoardsGetByBoardType(
List<BoardResponse.BoardsGetByBoardTypeBoardInfo> boards
) {
return BoardResponse.BoardsGetByBoardType.builder()
.boards(boards)
.build();
}

public BoardResponse.BoardsGetByBoardTypeBoardInfo toBoardsGetByBoardTypeBoardInfo(
Board board
) {
return BoardResponse.BoardsGetByBoardTypeBoardInfo.builder()
.writerId(board.getMember().getId())
.content(board.getContent())
.heartCount(board.getHeartCount())
.commentCount(board.getCommentCount())
.createAt(board.getCreatedAt())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.server.capple.domain.board.repository;

import com.server.capple.domain.board.entity.Board;
import com.server.capple.domain.board.entity.BoardType;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface BoardRepository extends JpaRepository<Board, Long> {

List<Board> findBoardsByBoardType(BoardType boardType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

public interface BoardService {
BoardResponse.BoardCreate createBoard(Member member, BoardType boardType, String content);

BoardResponse.BoardsGetByBoardType getBoardsByBoardType(BoardType boardType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
Expand All @@ -30,4 +33,22 @@ public BoardResponse.BoardCreate createBoard(Member member, BoardType boardType,
}
return boardMapper.toBoardCreate(board);
}

@Override
public BoardResponse.BoardsGetByBoardType getBoardsByBoardType(BoardType boardType) {
List<Board> boards = new ArrayList<>();
if (boardType == null) {
boards = boardRepository.findAll();
} else if (boardType == BoardType.FREEBOARD) {
boards = boardRepository.findBoardsByBoardType(BoardType.FREEBOARD);
} else if (boardType == BoardType.HOTBOARD) {
boards = boardRepository.findBoardsByBoardType(BoardType.HOTBOARD);
} else {
throw new RestApiException(BoardErrorCode.BOARD_BAD_REQUEST);
}
return boardMapper.toBoardsGetByBoardType(boards.stream()
.map(boardMapper::toBoardsGetByBoardTypeBoardInfo)
.toList()
);
}
}

0 comments on commit 7ee47d1

Please sign in to comment.