Skip to content

Commit

Permalink
UMC-EWHA#31 Feat : post GET paging
Browse files Browse the repository at this point in the history
  • Loading branch information
dawoon08 committed Nov 21, 2022
1 parent 1900cc8 commit 9df012d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,10 +58,14 @@ public BaseResponse<PostBoardRes> uploadBoard(@RequestBody PostBoardReq postBoar
*/
@ResponseBody
@GetMapping("")
public BaseResponse<List<GetPostRes>> getPosts(@RequestParam(required = false) String writer){
public BaseResponse<List<GetPostRes>> getPosts(@RequestParam(required = false) String writer,
@RequestParam(required = false) int page){
try{
Pagination pagination = new Pagination();
pagination.setPage(page);

if(writer == null){
List<GetPostRes> getPostsRes = boardProvider.getPosts();
List<GetPostRes> getPostsRes = boardProvider.getPosts(pagination);
return new BaseResponse<>(getPostsRes);
}
List<GetPostRes> getPostRes = boardProvider.getPostByWriter(writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,15 +29,16 @@ public int uploadBoard(PostBoardReq postBoardReq) {
return this.jdbcTemplate.queryForObject(lastInsertIdQuery, int.class); // 해당 쿼리문의 결과 마지막으로 삽인된 유저의 userIdx번호를 반환한다.
}

public List<GetPostRes> getPosts() {
String getPostsQuery = "select * from Posts";
public List<GetPostRes> 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<GetPostRes> getPostsByWriter(String writer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,9 +26,9 @@ public BoardProvider(BoardDao boardDao, JwtService jwtService) {
this.jwtService = jwtService;
}

public List<GetPostRes> getPosts() throws BaseException {
public List<GetPostRes> getPosts(Pagination pagination) throws BaseException {
try {
List<GetPostRes> getPostRes = boardDao.getPosts();
List<GetPostRes> getPostRes = boardDao.getPosts(pagination);
return getPostRes;
} catch (Exception exception) {
throw new BaseException(DATABASE_ERROR);
Expand All @@ -37,6 +37,7 @@ public List<GetPostRes> getPosts() throws BaseException {

public List<GetPostRes> getPostByWriter(String writer) throws BaseException {
try {

List<GetPostRes> getPostRes = boardDao.getPostsByWriter(writer);
return getPostRes;
} catch (Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ public class PostUserReq {
private String email;
private String password;
private String nickname;
private String point;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 9df012d

Please sign in to comment.