diff --git a/server/src/main/java/com/yogit/server/board/controller/BoardController.java b/server/src/main/java/com/yogit/server/board/controller/BoardController.java index 28a9b45..8a72e0a 100644 --- a/server/src/main/java/com/yogit/server/board/controller/BoardController.java +++ b/server/src/main/java/com/yogit/server/board/controller/BoardController.java @@ -3,6 +3,7 @@ import com.yogit.server.board.dto.request.CreateBoardReq; import com.yogit.server.board.dto.request.DeleteBoardReq; +import com.yogit.server.board.dto.request.GetAllBoardsReq; import com.yogit.server.board.dto.request.PatchBoardReq; import com.yogit.server.board.dto.response.BoardRes; import com.yogit.server.board.service.BoardService; @@ -16,6 +17,8 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import java.util.List; + @Slf4j @RestController @RequiredArgsConstructor // private final DI의존주입 @@ -31,6 +34,9 @@ public class BoardController { @ApiOperation(value = "게시글 등록", notes = "그룹 게시글에 필요한 정보를 입력받아 게시글 생성.") @ApiResponses({ @ApiResponse(code= 201, message = "요청에 성공하였습니다."), + @ApiResponse(code= 404, message = "존재하지 않는 유저입니다."), + @ApiResponse(code= 404, message = "존재하지 않는 City아이디입니다."), + @ApiResponse(code= 404, message = "존재하지 않는 Category아이디입니다."), @ApiResponse(code = 4000 , message = "서버 오류입니다.") }) @PostMapping("") @@ -45,6 +51,11 @@ public ApplicationResponse registerBoard(@RequestBody @Validated Creat @ApiOperation(value = "게시글 수정", notes = "그룹 게시글에 필요한 정보를 입력받아 게시글 수정.") @ApiResponses({ @ApiResponse(code= 201, message = "요청에 성공하였습니다."), + @ApiResponse(code= 404, message = "존재하지 않는 유저입니다."), + @ApiResponse(code= 404, message = "존재하지 않는 City아이디입니다."), + @ApiResponse(code= 404, message = "존재하지 않는 Category아이디입니다."), + @ApiResponse(code= 404, message = "존재하지 않는 Board아이디입니다."), + @ApiResponse(code= 404, message = "요청한 유저가 호스트가 아닙니다."), @ApiResponse(code = 4000 , message = "서버 오류입니다.") }) @PatchMapping("") @@ -59,6 +70,7 @@ public ApplicationResponse updateBoard(@RequestBody @Validated PatchBo @ApiOperation(value = "게시글 삭제", notes = "그룹 게시글 삭제 요청.") @ApiResponses({ @ApiResponse(code= 201, message = "요청에 성공하였습니다."), + @ApiResponse(code= 404, message = "존재하지 않는 유저입니다."), @ApiResponse(code= 404, message = "존재하지 않는 Board아이디입니다."), @ApiResponse(code= 404, message = "요청한 유저가 호스트가 아닙니다."), @ApiResponse(code = 4000 , message = "서버 오류입니다.") @@ -67,4 +79,19 @@ public ApplicationResponse updateBoard(@RequestBody @Validated PatchBo public ApplicationResponse deleteBoard(@RequestBody @Validated DeleteBoardReq deleteBoardReq){ return boardService.deleteBoard(deleteBoardReq); } + + /** + * 게시글 전체 조회 + * @author 토마스 + */ + @ApiOperation(value = "게시글 전체 조회", notes = "그룹 게시글 전체조회 요청.") + @ApiResponses({ + @ApiResponse(code= 201, message = "요청에 성공하였습니다."), + @ApiResponse(code= 404, message = "존재하지 않는 유저입니다."), + @ApiResponse(code = 4000 , message = "서버 오류입니다.") + }) + @PostMapping("/get") + public ApplicationResponse> findAllBoards(@RequestBody @Validated GetAllBoardsReq getAllBoardsReq){ + return boardService.findAllBoards(getAllBoardsReq); + } } diff --git a/server/src/main/java/com/yogit/server/board/dto/request/GetAllBoardsReq.java b/server/src/main/java/com/yogit/server/board/dto/request/GetAllBoardsReq.java new file mode 100644 index 0000000..310c1d6 --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/dto/request/GetAllBoardsReq.java @@ -0,0 +1,23 @@ +package com.yogit.server.board.dto.request; + +import io.swagger.annotations.ApiModelProperty; +import io.swagger.annotations.ApiParam; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class GetAllBoardsReq { + + @ApiModelProperty(example = "1") + @ApiParam(value = "Board ID", required = true) + private Long boardId; + + @ApiModelProperty(example = "1") + @ApiParam(value = "유저 ID", required = true) + private Long userId; + + @ApiModelProperty(example = "1") + @ApiParam(value = "페이징 조회 페이지", required = true) + private int cursor; +} diff --git a/server/src/main/java/com/yogit/server/board/repository/BoardRepository.java b/server/src/main/java/com/yogit/server/board/repository/BoardRepository.java index 3fecb72..cffaeb6 100644 --- a/server/src/main/java/com/yogit/server/board/repository/BoardRepository.java +++ b/server/src/main/java/com/yogit/server/board/repository/BoardRepository.java @@ -1,6 +1,8 @@ package com.yogit.server.board.repository; import com.yogit.server.board.entity.Board; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -11,4 +13,7 @@ public interface BoardRepository extends JpaRepository { @Query("select b from Board b where b.id = :boardId and b.status = 'ACTIVE'") Optional findBoardById(@Param("boardId") Long boardId); + + @Query("select b from Board b where b.status = 'ACTIVE'") + Slice findAllBoards(Pageable pageable); } diff --git a/server/src/main/java/com/yogit/server/board/service/BoardService.java b/server/src/main/java/com/yogit/server/board/service/BoardService.java index f928fd5..ed0f585 100644 --- a/server/src/main/java/com/yogit/server/board/service/BoardService.java +++ b/server/src/main/java/com/yogit/server/board/service/BoardService.java @@ -2,10 +2,13 @@ import com.yogit.server.board.dto.request.CreateBoardReq; import com.yogit.server.board.dto.request.DeleteBoardReq; +import com.yogit.server.board.dto.request.GetAllBoardsReq; import com.yogit.server.board.dto.request.PatchBoardReq; import com.yogit.server.board.dto.response.BoardRes; import com.yogit.server.global.dto.ApplicationResponse; +import java.util.List; + public interface BoardService { ApplicationResponse createBoard(CreateBoardReq createBoardReq); @@ -13,4 +16,6 @@ public interface BoardService { ApplicationResponse updateBoard(PatchBoardReq patchBoardReq); ApplicationResponse deleteBoard(DeleteBoardReq deleteBoardReq); + + ApplicationResponse> findAllBoards(GetAllBoardsReq getAllBoardsReq); } diff --git a/server/src/main/java/com/yogit/server/board/service/BoardServiceImpl.java b/server/src/main/java/com/yogit/server/board/service/BoardServiceImpl.java index a412a3b..d5b34ca 100644 --- a/server/src/main/java/com/yogit/server/board/service/BoardServiceImpl.java +++ b/server/src/main/java/com/yogit/server/board/service/BoardServiceImpl.java @@ -2,6 +2,7 @@ import com.yogit.server.board.dto.request.CreateBoardReq; import com.yogit.server.board.dto.request.DeleteBoardReq; +import com.yogit.server.board.dto.request.GetAllBoardsReq; import com.yogit.server.board.dto.request.PatchBoardReq; import com.yogit.server.board.dto.response.BoardRes; import com.yogit.server.board.entity.Board; @@ -20,11 +21,14 @@ import com.yogit.server.user.repository.CityRepository; import com.yogit.server.user.repository.UserRepository; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Slice; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.bind.annotation.RequestBody; -import javax.sound.midi.Patch; +import java.util.List; +import java.util.stream.Collectors; @Service @Transactional(readOnly = true) @@ -36,6 +40,9 @@ public class BoardServiceImpl implements BoardService{ private final CityRepository cityRepository; private final CategoryRepository categoryRepository; + private static final int PAGING_SIZE = 10; + private static final String PAGING_STANDARD = "date"; + @Transactional(readOnly = false) @Override public ApplicationResponse createBoard(CreateBoardReq dto){ @@ -112,4 +119,22 @@ public ApplicationResponse deleteBoard(DeleteBoardReq dto){ return ApplicationResponse.ok(boardRes); } + + @Transactional(readOnly = false) + @Override + public ApplicationResponse> findAllBoards(GetAllBoardsReq dto){ + int cursor = dto.getCursor(); + + User user = userRepository.findById(dto.getUserId()) + .orElseThrow(() -> new NotFoundUserException()); + + PageRequest pageRequest = PageRequest.of(cursor, PAGING_SIZE, Sort.by(Sort.Direction.ASC, PAGING_STANDARD )); + + Slice boards = boardRepository.findAllBoards(pageRequest); + List boardsRes = boards.stream() + .map(board -> BoardRes.toDto(board)) + .collect(Collectors.toList()); + return ApplicationResponse.ok(boardsRes); + } + }