Skip to content

Commit

Permalink
#9 feat: Board전체조회 API구현, 페이징 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
xhaktmchl committed Oct 10, 2022
1 parent 0a44ef5 commit cab378c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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의존주입
Expand All @@ -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("")
Expand All @@ -45,6 +51,11 @@ public ApplicationResponse<BoardRes> 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("")
Expand All @@ -59,6 +70,7 @@ public ApplicationResponse<BoardRes> 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 = "서버 오류입니다.")
Expand All @@ -67,4 +79,19 @@ public ApplicationResponse<BoardRes> updateBoard(@RequestBody @Validated PatchBo
public ApplicationResponse<BoardRes> 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<List<BoardRes>> findAllBoards(@RequestBody @Validated GetAllBoardsReq getAllBoardsReq){
return boardService.findAllBoards(getAllBoardsReq);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,4 +13,7 @@ public interface BoardRepository extends JpaRepository<Board, Long> {

@Query("select b from Board b where b.id = :boardId and b.status = 'ACTIVE'")
Optional<Board> findBoardById(@Param("boardId") Long boardId);

@Query("select b from Board b where b.status = 'ACTIVE'")
Slice<Board> findAllBoards(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

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<BoardRes> createBoard(CreateBoardReq createBoardReq);

ApplicationResponse<BoardRes> updateBoard(PatchBoardReq patchBoardReq);

ApplicationResponse<BoardRes> deleteBoard(DeleteBoardReq deleteBoardReq);

ApplicationResponse<List<BoardRes>> findAllBoards(GetAllBoardsReq getAllBoardsReq);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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<BoardRes> createBoard(CreateBoardReq dto){
Expand Down Expand Up @@ -112,4 +119,22 @@ public ApplicationResponse<BoardRes> deleteBoard(DeleteBoardReq dto){
return ApplicationResponse.ok(boardRes);
}


@Transactional(readOnly = false)
@Override
public ApplicationResponse<List<BoardRes>> 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<Board> boards = boardRepository.findAllBoards(pageRequest);
List<BoardRes> boardsRes = boards.stream()
.map(board -> BoardRes.toDto(board))
.collect(Collectors.toList());
return ApplicationResponse.ok(boardsRes);
}

}

0 comments on commit cab378c

Please sign in to comment.