diff --git a/src/main/java/com/elice/ustory/domain/bookmark/BookmarkController.java b/src/main/java/com/elice/ustory/domain/bookmark/BookmarkController.java index 2a60e30d..3d21e406 100644 --- a/src/main/java/com/elice/ustory/domain/bookmark/BookmarkController.java +++ b/src/main/java/com/elice/ustory/domain/bookmark/BookmarkController.java @@ -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; @@ -32,6 +32,7 @@ @RequiredArgsConstructor public class BookmarkController { + private final PageableValidation pageableValidation; private final BookmarkService bookmarkService; @Operation(summary = "Create bookmark API", description = "북마크를 지정한다.") @@ -65,11 +66,7 @@ public ResponseEntity> 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 papers = bookmarkService.getBookmarksByUser(userId, page, size); diff --git a/src/main/java/com/elice/ustory/domain/diary/controller/DiaryController.java b/src/main/java/com/elice/ustory/domain/diary/controller/DiaryController.java index e8da3be6..2c77d35c 100644 --- a/src/main/java/com/elice/ustory/domain/diary/controller/DiaryController.java +++ b/src/main/java/com/elice/ustory/domain/diary/controller/DiaryController.java @@ -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; @@ -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; @@ -29,6 +28,8 @@ @RequestMapping("/diary") @RequiredArgsConstructor public class DiaryController { + + private final PageableValidation pageableValidation; private final DiaryService diaryService; @Operation(summary = "Create Diary API", description = "다이어리 생성 및 링크 테이블에 등록") @@ -67,12 +68,8 @@ public ResponseEntity> 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 userDiaries = diaryService.getUserDiaries(userId, pageable, diaryCategory, requestTime, searchWord); diff --git a/src/main/java/com/elice/ustory/domain/friend/controller/FriendController.java b/src/main/java/com/elice/ustory/domain/friend/controller/FriendController.java index b53936f4..3dbd0abc 100644 --- a/src/main/java/com/elice/ustory/domain/friend/controller/FriendController.java +++ b/src/main/java/com/elice/ustory/domain/friend/controller/FriendController.java @@ -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; @@ -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; @@ -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.*; @@ -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; } @@ -59,12 +60,7 @@ public ResponseEntity> 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 friends = friendService.getFriends(userId, nickname, requestTime, pageable); @@ -113,12 +109,8 @@ public ResponseEntity> 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 friendRequests = friendService.getFriendRequests(userId, requestTime, pageable); return ResponseEntity.ok(friendRequests); diff --git a/src/main/java/com/elice/ustory/domain/like/LikeController.java b/src/main/java/com/elice/ustory/domain/like/LikeController.java index 9e62240f..ac69e770 100644 --- a/src/main/java/com/elice/ustory/domain/like/LikeController.java +++ b/src/main/java/com/elice/ustory/domain/like/LikeController.java @@ -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; @@ -32,6 +32,7 @@ @RequiredArgsConstructor public class LikeController { + private final PageableValidation pageableValidation; private final LikeService likeService; @Operation(summary = "Create Like API", description = "좋아요로 지정한다.") @@ -65,11 +66,7 @@ public ResponseEntity> 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 papers = likeService.getLikesByUser(userId, page, size); diff --git a/src/main/java/com/elice/ustory/domain/like/entity/Like.java b/src/main/java/com/elice/ustory/domain/like/entity/Like.java index adef709c..f9be00d4 100644 --- a/src/main/java/com/elice/ustory/domain/like/entity/Like.java +++ b/src/main/java/com/elice/ustory/domain/like/entity/Like.java @@ -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 diff --git a/src/main/java/com/elice/ustory/domain/notice/controller/NoticeController.java b/src/main/java/com/elice/ustory/domain/notice/controller/NoticeController.java index 05076f42..0bf63948 100644 --- a/src/main/java/com/elice/ustory/domain/notice/controller/NoticeController.java +++ b/src/main/java/com/elice/ustory/domain/notice/controller/NoticeController.java @@ -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; @@ -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.*; @@ -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; } @@ -56,13 +56,8 @@ public ResponseEntity> 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 notices = noticeService.getAllNoticesByUserId(userId, requestTime, pageable); return ResponseEntity.ok(notices); diff --git a/src/main/java/com/elice/ustory/domain/paper/PaperController.java b/src/main/java/com/elice/ustory/domain/paper/PaperController.java index decf0772..69660a38 100644 --- a/src/main/java/com/elice/ustory/domain/paper/PaperController.java +++ b/src/main/java/com/elice/ustory/domain/paper/PaperController.java @@ -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; @@ -46,6 +46,7 @@ @RequiredArgsConstructor public class PaperController { + private final PageableValidation pageableValidation; private final PaperService paperService; private final BookmarkService bookmarkService; @@ -136,11 +137,7 @@ public ResponseEntity> 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 papers = paperService.getPapersByWriterId(userId, page, size, requestTime); @@ -169,11 +166,7 @@ public ResponseEntity> 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 papers = paperService.getPapersByDiaryId(diaryId, page, size, startDate, endDate, requestTime); diff --git a/src/main/java/com/elice/ustory/global/Validation/PageableValidation.java b/src/main/java/com/elice/ustory/global/Validation/PageableValidation.java new file mode 100644 index 00000000..f96408ce --- /dev/null +++ b/src/main/java/com/elice/ustory/global/Validation/PageableValidation.java @@ -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); + } + +} +