Skip to content

Commit

Permalink
refactor: 상황에 맞는 페이지네이션 검증
Browse files Browse the repository at this point in the history
  • Loading branch information
yungic committed Jul 3, 2024
1 parent bda293d commit 9130a36
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.elice.ustory.domain.bookmark.dto.BookmarkResponse;
import com.elice.ustory.domain.paper.entity.Paper;
import com.elice.ustory.global.exception.dto.ErrorResponse;
import com.elice.ustory.global.exception.model.ValidationException;
import com.elice.ustory.global.jwt.JwtAuthorization;
import com.elice.ustory.global.Validation.PageableValidation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -32,6 +32,7 @@
@RequiredArgsConstructor
public class BookmarkController {

private final PageableValidation pageableValidation;
private final BookmarkService bookmarkService;

@Operation(summary = "Create bookmark API", description = "북마크를 지정한다.")
Expand Down Expand Up @@ -65,11 +66,7 @@ public ResponseEntity<List<BookmarkListResponse>> getBookmarkedPapersByUserId(
@RequestParam(name = "page", defaultValue = "1") int page,
@RequestParam(name = "size", defaultValue = "20") int size) {

if (page < 1) {
throw new ValidationException("페이지는 1 이상이어야 합니다.");
} else if (size < 1){
throw new ValidationException("사이즈는 1 이상이어야 합니다.");
}
pageableValidation.pageValidate(page, size);

List<Paper> papers = bookmarkService.getBookmarksByUser(userId, page, size);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.elice.ustory.domain.diary.entity.DiaryCategory;
import com.elice.ustory.domain.diary.service.DiaryService;
import com.elice.ustory.global.exception.dto.ErrorResponse;
import com.elice.ustory.global.exception.model.ValidationException;
import com.elice.ustory.global.jwt.JwtAuthorization;
import com.elice.ustory.global.Validation.PageableValidation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -15,7 +15,6 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -29,6 +28,8 @@
@RequestMapping("/diary")
@RequiredArgsConstructor
public class DiaryController {

private final PageableValidation pageableValidation;
private final DiaryService diaryService;

@Operation(summary = "Create Diary API", description = "다이어리 생성 및 링크 테이블에 등록")
Expand Down Expand Up @@ -67,12 +68,8 @@ public ResponseEntity<List<DiaryListResponse>> getDiaries(
@RequestParam(name = "diaryCategory", required = false) DiaryCategory diaryCategory,
@RequestParam(name = "requestTime") LocalDateTime requestTime,
@RequestParam(name = "searchWord", required = false) String searchWord) {
if(page<1){
throw new ValidationException("페이지는 1 이상이어야 합니다.");
}else if(size<1){
throw new ValidationException("사이즈는 1 이상이어야 합니다.");
}
Pageable pageable = PageRequest.of(page - 1, size);

Pageable pageable = pageableValidation.madePageable(page, size);

List<DiaryListResponse> userDiaries = diaryService.getUserDiaries(userId, pageable, diaryCategory, requestTime, searchWord);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.elice.ustory.domain.friend.controller;

import com.elice.ustory.domain.bookmark.dto.BookmarkListResponse;
import com.elice.ustory.domain.friend.dto.FriendRequestDto;
import com.elice.ustory.domain.friend.dto.FriendRequestListDTO;
import com.elice.ustory.domain.friend.dto.UserFriendDTO;
Expand All @@ -9,6 +8,7 @@
import com.elice.ustory.global.exception.dto.ErrorResponse;
import com.elice.ustory.global.exception.model.ValidationException;
import com.elice.ustory.global.jwt.JwtAuthorization;
import com.elice.ustory.global.Validation.PageableValidation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -17,7 +17,6 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -31,9 +30,11 @@
@RequestMapping("/friend")
public class FriendController {

private PageableValidation pageableValidation;
private FriendService friendService;

public FriendController(FriendService friendService) {
public FriendController(FriendService friendService, PageableValidation codeutils) {
this.pageableValidation = codeutils;
this.friendService = friendService;
}

Expand All @@ -59,12 +60,7 @@ public ResponseEntity<List<UserFriendDTO>> getFriends(
@RequestParam(name = "size", defaultValue = "10") int size,
@RequestParam(name = "requestTime") LocalDateTime requestTime) {

if (page < 1) {
throw new ValidationException("페이지는 1 이상이어야 합니다.");
} else if (size < 1){
throw new ValidationException("사이즈는 1 이상이어야 합니다.");
}
Pageable pageable = PageRequest.of(page - 1, size);
Pageable pageable = pageableValidation.madePageable(page, size);

List<UserFriendDTO> friends = friendService.getFriends(userId, nickname, requestTime, pageable);

Expand Down Expand Up @@ -113,12 +109,8 @@ public ResponseEntity<List<FriendRequestListDTO>> getFriendRequests(@JwtAuthoriz
@RequestParam(name = "page", defaultValue = "1") int page,
@RequestParam(name = "size", defaultValue = "10") int size,
@RequestParam(name = "requestTime") LocalDateTime requestTime) {
if (page < 1) {
throw new ValidationException("페이지는 1 이상이어야 합니다.");
} else if (size < 1){
throw new ValidationException("사이즈는 1 이상이어야 합니다.");
}
Pageable pageable = PageRequest.of(page - 1, size);

Pageable pageable = pageableValidation.madePageable(page, size);

List<FriendRequestListDTO> friendRequests = friendService.getFriendRequests(userId, requestTime, pageable);
return ResponseEntity.ok(friendRequests);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.elice.ustory.domain.like.dto.LikeResponse;
import com.elice.ustory.domain.paper.entity.Paper;
import com.elice.ustory.global.exception.dto.ErrorResponse;
import com.elice.ustory.global.exception.model.ValidationException;
import com.elice.ustory.global.jwt.JwtAuthorization;
import com.elice.ustory.global.Validation.PageableValidation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -32,6 +32,7 @@
@RequiredArgsConstructor
public class LikeController {

private final PageableValidation pageableValidation;
private final LikeService likeService;

@Operation(summary = "Create Like API", description = "좋아요로 지정한다.")
Expand Down Expand Up @@ -65,11 +66,7 @@ public ResponseEntity<List<LikeListResponse>> getLikedPapersByUserId(
@RequestParam(name = "page", defaultValue = "1") int page,
@RequestParam(name = "size", defaultValue = "20") int size) {

if (page < 1) {
throw new ValidationException("페이지는 1 이상이어야 합니다.");
} else if (size < 1){
throw new ValidationException("사이즈는 1 이상이어야 합니다.");
}
pageableValidation.pageValidate(page, size);

List<Paper> papers = likeService.getLikesByUser(userId, page, size);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@EntityListeners(AuditingEntityListener.class)
@IdClass(LikeId.class)
@Table(name = "like")
@Table(name = "`like`")
public class Like extends BaseEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.elice.ustory.domain.notice.dto.NoticeDeleteRequest;
import com.elice.ustory.domain.notice.dto.NoticeResponse;
import com.elice.ustory.global.exception.dto.ErrorResponse;
import com.elice.ustory.global.exception.model.ValidationException;
import com.elice.ustory.global.jwt.JwtAuthorization;
import com.elice.ustory.domain.notice.service.NoticeService;
import com.elice.ustory.global.Validation.PageableValidation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -15,7 +15,6 @@
import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.validation.Valid;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -29,10 +28,11 @@
@RequestMapping("/notices")
public class NoticeController {


private PageableValidation pageableValidation;
private NoticeService noticeService;

public NoticeController(NoticeService noticeService) {
public NoticeController(NoticeService noticeService, PageableValidation pageableValidation) {
this.pageableValidation = pageableValidation;
this.noticeService = noticeService;
}

Expand All @@ -56,13 +56,8 @@ public ResponseEntity<List<NoticeResponse>> getAllNoticesByUserId(@JwtAuthorizat
@RequestParam(name = "page", defaultValue = "1") int page,
@RequestParam(name = "size", defaultValue = "10") int size,
@RequestParam(name = "requestTime") LocalDateTime requestTime) {
if (page < 1) {
throw new ValidationException("페이지는 1 이상이어야 합니다.");
} else if (size < 1){
throw new ValidationException("사이즈는 1 이상이어야 합니다.");
}
Pageable pageable = pageableValidation.madePageable(page, size);

Pageable pageable = PageRequest.of(page - 1, size);
List<NoticeResponse> notices = noticeService.getAllNoticesByUserId(userId, requestTime, pageable);

return ResponseEntity.ok(notices);
Expand Down
15 changes: 4 additions & 11 deletions src/main/java/com/elice/ustory/domain/paper/PaperController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import com.elice.ustory.domain.paper.entity.Paper;
import com.elice.ustory.domain.paper.service.PaperService;
import com.elice.ustory.global.exception.dto.ErrorResponse;
import com.elice.ustory.global.exception.model.ValidationException;
import com.elice.ustory.global.jwt.JwtAuthorization;
import com.elice.ustory.global.Validation.PageableValidation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
Expand Down Expand Up @@ -46,6 +46,7 @@
@RequiredArgsConstructor
public class PaperController {

private final PageableValidation pageableValidation;
private final PaperService paperService;
private final BookmarkService bookmarkService;

Expand Down Expand Up @@ -136,11 +137,7 @@ public ResponseEntity<List<PaperListResponse>> getPapersByUser(@JwtAuthorization
@RequestParam(name = "size", defaultValue = "20") int size,
@RequestParam(name = "requestTime") LocalDateTime requestTime) {

if (page < 1) {
throw new ValidationException("페이지는 1 이상이어야 합니다.");
} else if (size < 1){
throw new ValidationException("사이즈는 1 이상이어야 합니다.");
}
pageableValidation.pageValidate(page, size);

List<Paper> papers = paperService.getPapersByWriterId(userId, page, size, requestTime);

Expand Down Expand Up @@ -169,11 +166,7 @@ public ResponseEntity<List<PaperListResponse>> getPapersByDiary(
@RequestParam(name = "endDate", required = false) @DateTimeFormat(pattern = "yyyy/MM/dd") LocalDate endDate
) {

if (page < 1) {
throw new ValidationException("페이지는 1 이상이어야 합니다.");
} else if (size < 1){
throw new ValidationException("사이즈는 1 이상이어야 합니다.");
}
pageableValidation.pageValidate(page, size);

List<Paper> papers = paperService.getPapersByDiaryId(diaryId, page, size, startDate, endDate, requestTime);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.elice.ustory.global.Validation;

import com.elice.ustory.global.exception.model.ValidationException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

@Component
public class PageableValidation {

private static final String AT_LEAST_PAGE = "페이지는 1 이상이어야 합니다.";
private static final String AT_LEAST_SIZE = "사이즈는 1 이상이어야 합니다.";


/**
*
* @param page
* @param size
* 페이지네이션에서 페이지와 사이즈가 1 이상이여야 함을 검증하는 메서드
* Pageable 까지 만들어서 넘기는 버전, 그냥 검증한 하는 버전 두 가지 버전의 메서드를 구현
* 하나로 만들면 좋았겠다. 하지만 서비스단까지 바꿔야 하는 관계로 패스
*/
public void pageValidate(int page, int size) {
if (page < 1) {
throw new ValidationException(AT_LEAST_PAGE);
} else if (size < 1) {
throw new ValidationException(AT_LEAST_SIZE);
}
}

public Pageable madePageable(int page, int size) {

if (page < 1) {
throw new ValidationException(AT_LEAST_PAGE);
} else if (size < 1) {
throw new ValidationException(AT_LEAST_SIZE);
}

return PageRequest.of(page - 1, size);
}

}

0 comments on commit 9130a36

Please sign in to comment.