@@ -73,13 +73,13 @@ public ResponseResult saveEmotionRecordWithMusic(Long userId, EmotionRecordReque
7373
7474 @ Transactional (readOnly = true )
7575 public ResponseResult getEmotionRecordsByLoginId (String userTag , int page , int size ) {
76- Pageable pageable ;
77-
78- try {
79- pageable = PageRequest .of (page , size , Sort .by ("createdAt" ).descending ());
80- } catch (IllegalArgumentException e ) {
81- return new ResponseResult (ErrorCode .INVALID_PAGE_REQUEST , "페이지 요청 값이 잘못되었습니다." );
76+ ResponseResult pageValidationResult = validateAndCreatePageable (page , size );
77+ if (pageValidationResult .getCode () != 200 /*SUCCESS*/ ) {
78+ return pageValidationResult ;
8279 }
80+
81+ Pageable pageable = (Pageable ) pageValidationResult .getData ();
82+
8383 try {
8484 Page <EmotionRecord > recordsPage = emotionRecordRepository .findByLoginId (userTag , pageable );
8585
@@ -97,15 +97,14 @@ public ResponseResult getEmotionRecordsByLoginId(String userTag, int page, int s
9797 }
9898
9999 public ResponseResult getEmotionRecordsExcludingUserIdByFilters (Long userId , List <String > emotionList , String spotifyId , int page , int size ) {
100- Pageable pageable ;
101100 List <Emotions > emotionEnums = null ;
102-
103- try {
104- pageable = PageRequest .of (page , size , Sort .by ("createdAt" ).descending ());
105- } catch (IllegalArgumentException e ) {
106- return new ResponseResult (ErrorCode .INVALID_PAGE_REQUEST , "페이지 요청 값이 잘못되었습니다." );
101+ ResponseResult pageValidationResult = validateAndCreatePageable (page , size );
102+ if (pageValidationResult .getCode () != 200 /*SUCCESS*/ ) {
103+ return pageValidationResult ;
107104 }
108105
106+ Pageable pageable = (Pageable ) pageValidationResult .getData ();
107+
109108 if (emotionList != null && !emotionList .isEmpty ()) {
110109 try {
111110 emotionEnums = emotionList .stream ()
@@ -202,4 +201,18 @@ public ResponseResult deleteEmotionRecord(Long recordId) {
202201 return new ResponseResult (ErrorCode .INTERNAL_SERVER_ERROR , e .getMessage ());
203202 }
204203 }
204+
205+ // 페이지네이션 처리 및 관련 예외처리
206+ private ResponseResult validateAndCreatePageable (int page , int size ) {
207+ if (page < 1 ) {
208+ return new ResponseResult (ErrorCode .INVALID_PAGE_REQUEST , "페이지 번호는 1 이상이어야 합니다." );
209+ }
210+ try {
211+ Pageable pageable = PageRequest .of (page - 1 , size , Sort .by ("createdAt" ).descending ());
212+ return new ResponseResult (ErrorCode .SUCCESS , pageable );
213+ } catch (IllegalArgumentException e ) {
214+ return new ResponseResult (ErrorCode .INVALID_PAGE_REQUEST , "페이지 요청 값이 잘못되었습니다." );
215+ }
216+ }
217+
205218}
0 commit comments