Skip to content

Commit b2e9d67

Browse files
committed
fix: 페이지 값, 응답 내 currentPage 1로 시작하도록 수정 및 중복 코드 제거 #32
1 parent 849d406 commit b2e9d67

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

src/main/java/org/dfbf/soundlink/domain/emotionRecord/controller/EmotionRecordController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public ResponseResult getEmotionRecordsWithoutMine(
4545
@RequestParam(required = false) String spotifyId,
4646
@Parameter(description = "감정 필터(추가 가능)")
4747
@RequestParam(required = false) List<String> emotions,
48-
@RequestParam(defaultValue = "0") int page,
48+
@RequestParam(defaultValue = "1") int page,
4949
@RequestParam(defaultValue = "10") int size) {
5050
return emotionRecordService.getEmotionRecordsExcludingUserIdByFilters(userId, emotions, spotifyId, page, size);
5151
}
@@ -57,7 +57,7 @@ public ResponseResult getEmotionRecordsWithoutMine(
5757
)
5858
public ResponseResult getAllEmotionRecords(
5959
@RequestParam("tag") String loginId,
60-
@RequestParam(defaultValue = "0") int page,
60+
@RequestParam(defaultValue = "1") int page,
6161
@RequestParam(defaultValue = "10") int size) {
6262
return emotionRecordService.getEmotionRecordsByLoginId(loginId, page, size);
6363
}

src/main/java/org/dfbf/soundlink/domain/emotionRecord/dto/response/EmotionRecordPageResponseDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public record EmotionRecordPageResponseDTO<T>(
1313
public static <T> EmotionRecordPageResponseDTO<T> fromPage(Page<?> recordsPage, List<T> dtoList) {
1414
return new EmotionRecordPageResponseDTO<>(
1515
dtoList,
16-
recordsPage.getNumber(),
16+
recordsPage.getNumber() + 1, // 현재 페이지 번호를 1부터 시작하도록 변경
1717
recordsPage.getTotalPages(),
1818
recordsPage.getTotalElements()
1919
);

src/main/java/org/dfbf/soundlink/domain/emotionRecord/service/EmotionRecordService.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)