From 9df012d9f262695db8501d31970191e6940f2fc2 Mon Sep 17 00:00:00 2001 From: dawoon08 Date: Mon, 21 Nov 2022 20:06:05 +0900 Subject: [PATCH] #31 Feat : post GET paging --- .../demo/src/board/BoardController.java | 9 +++++++-- .../com/example/demo/src/board/BoardDao.java | 10 ++++++---- .../example/demo/src/board/BoardProvider.java | 7 ++++--- .../demo/src/user/model/PostUserReq.java | 1 - .../demo/src/user/model/PostUserRes.java | 6 +++--- .../com/example/demo/utils/Pagination.java | 19 +++++++++++++++++++ 6 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 SpringBootTemplate/src/main/java/com/example/demo/utils/Pagination.java diff --git a/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardController.java b/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardController.java index bf562bc..43e5104 100644 --- a/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardController.java +++ b/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardController.java @@ -4,6 +4,7 @@ import com.example.demo.config.BaseResponse; import com.example.demo.src.board.model.*; import com.example.demo.utils.JwtService; +import com.example.demo.utils.Pagination; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -57,10 +58,14 @@ public BaseResponse uploadBoard(@RequestBody PostBoardReq postBoar */ @ResponseBody @GetMapping("") - public BaseResponse> getPosts(@RequestParam(required = false) String writer){ + public BaseResponse> getPosts(@RequestParam(required = false) String writer, + @RequestParam(required = false) int page){ try{ + Pagination pagination = new Pagination(); + pagination.setPage(page); + if(writer == null){ - List getPostsRes = boardProvider.getPosts(); + List getPostsRes = boardProvider.getPosts(pagination); return new BaseResponse<>(getPostsRes); } List getPostRes = boardProvider.getPostByWriter(writer); diff --git a/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardDao.java b/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardDao.java index 30a45a3..5e96484 100644 --- a/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardDao.java +++ b/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardDao.java @@ -3,6 +3,7 @@ import com.example.demo.src.board.model.GetPostRes; import com.example.demo.src.board.model.PatchPostReq; import com.example.demo.src.board.model.PostBoardReq; +import com.example.demo.utils.Pagination; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @@ -28,15 +29,16 @@ public int uploadBoard(PostBoardReq postBoardReq) { return this.jdbcTemplate.queryForObject(lastInsertIdQuery, int.class); // 해당 쿼리문의 결과 마지막으로 삽인된 유저의 userIdx번호를 반환한다. } - public List getPosts() { - String getPostsQuery = "select * from Posts"; + public List getPosts(Pagination pagination) { + String getPostsQuery = "select * from Posts limit ? offset ?"; + Object[] getPostsParams = new Object[]{pagination.getLimit(), pagination.getOffSet()}; return this.jdbcTemplate.query(getPostsQuery, (rs, rowNum) -> new GetPostRes( rs.getInt("postIdx"), rs.getString("title"), rs.getString("writer"), - rs.getString("content") - )); + rs.getString("content")), + getPostsParams); } public List getPostsByWriter(String writer) { diff --git a/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardProvider.java b/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardProvider.java index 1231c89..d9b619e 100644 --- a/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardProvider.java +++ b/SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardProvider.java @@ -2,8 +2,8 @@ import com.example.demo.config.BaseException; import com.example.demo.src.board.model.GetPostRes; -import com.example.demo.src.user.model.GetUserRes; import com.example.demo.utils.JwtService; +import com.example.demo.utils.Pagination; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -26,9 +26,9 @@ public BoardProvider(BoardDao boardDao, JwtService jwtService) { this.jwtService = jwtService; } - public List getPosts() throws BaseException { + public List getPosts(Pagination pagination) throws BaseException { try { - List getPostRes = boardDao.getPosts(); + List getPostRes = boardDao.getPosts(pagination); return getPostRes; } catch (Exception exception) { throw new BaseException(DATABASE_ERROR); @@ -37,6 +37,7 @@ public List getPosts() throws BaseException { public List getPostByWriter(String writer) throws BaseException { try { + List getPostRes = boardDao.getPostsByWriter(writer); return getPostRes; } catch (Exception exception) { diff --git a/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserReq.java b/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserReq.java index 69b0ab7..fe30cf3 100644 --- a/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserReq.java +++ b/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserReq.java @@ -10,5 +10,4 @@ public class PostUserReq { private String email; private String password; private String nickname; - private String point; } diff --git a/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserRes.java b/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserRes.java index 98b7dee..48d24a0 100644 --- a/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserRes.java +++ b/SpringBootTemplate/src/main/java/com/example/demo/src/user/model/PostUserRes.java @@ -13,9 +13,9 @@ */ public class PostUserRes { private int userIdx; - + // 해당 부분은 7주차 - JWT 수업 후 주석해제 및 대체해주세요! + private String jwt; public PostUserRes(String jwt, int userIdx) { } -// 해당 부분은 7주차 - JWT 수업 후 주석해제 및 대체해주세요! - private String jwt; + } diff --git a/SpringBootTemplate/src/main/java/com/example/demo/utils/Pagination.java b/SpringBootTemplate/src/main/java/com/example/demo/utils/Pagination.java new file mode 100644 index 0000000..d2404f6 --- /dev/null +++ b/SpringBootTemplate/src/main/java/com/example/demo/utils/Pagination.java @@ -0,0 +1,19 @@ +package com.example.demo.utils; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class Pagination { + private int page; // 페이지 번호 + private int limit = 3; //페이지당 출력할 데이터 개수 + + public int getOffSet() { + return (page - 1) * limit; + } +}