Skip to content

Commit e40c943

Browse files
committed
refactor: SliceResponse로 대체
1 parent fcc9244 commit e40c943

5 files changed

Lines changed: 33 additions & 20 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example.solidconnection.common.dto;
2+
3+
import org.springframework.data.domain.Slice;
4+
5+
import java.util.List;
6+
7+
public record SliceResponse<T>(
8+
List<T> content,
9+
int nextPageNumber
10+
) {
11+
12+
private static final int NO_NEXT_PAGE = -1;
13+
private static final int BASE_NUMBER = 1; // 1-based
14+
15+
public static <T, R> SliceResponse<R> of(Slice<T> slice, List<R> content) {
16+
int nextPageNumber = slice.hasNext()
17+
? slice.getNumber() + BASE_NUMBER + 1
18+
: NO_NEXT_PAGE;
19+
return new SliceResponse<>(content, nextPageNumber);
20+
}
21+
}

src/main/java/com/example/solidconnection/mentor/controller/MentorController.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.example.solidconnection.mentor.controller;
22

3+
import com.example.solidconnection.common.dto.SliceResponse;
34
import com.example.solidconnection.common.resolver.AuthorizedUser;
45
import com.example.solidconnection.mentor.dto.MentorDetailResponse;
5-
import com.example.solidconnection.mentor.dto.MentorPreviewsResponse;
6+
import com.example.solidconnection.mentor.dto.MentorPreviewResponse;
67
import com.example.solidconnection.mentor.service.MentorQueryService;
78
import com.example.solidconnection.siteuser.domain.SiteUser;
89
import lombok.RequiredArgsConstructor;
@@ -34,12 +35,12 @@ public ResponseEntity<MentorDetailResponse> getMentorDetails(
3435
}
3536

3637
@GetMapping
37-
public ResponseEntity<MentorPreviewsResponse> getMentorPreviews(
38+
public ResponseEntity<SliceResponse<MentorPreviewResponse>> getMentorPreviews(
3839
@AuthorizedUser SiteUser siteUser,
3940
@RequestParam("region") String region,
4041
@PageableDefault(size = 3, sort = "menteeCount", direction = DESC) Pageable pageable
4142
) {
42-
MentorPreviewsResponse response = mentorQueryService.getMentorPreviews(region, siteUser, pageable);
43+
SliceResponse<MentorPreviewResponse> response = mentorQueryService.getMentorPreviews(region, siteUser, pageable);
4344
return ResponseEntity.ok(response);
4445
}
4546
}

src/main/java/com/example/solidconnection/mentor/dto/MentorPreviewsResponse.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/java/com/example/solidconnection/mentor/service/MentorQueryService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.example.solidconnection.mentor.service;
22

3+
import com.example.solidconnection.common.dto.SliceResponse;
34
import com.example.solidconnection.common.exception.CustomException;
45
import com.example.solidconnection.mentor.domain.Mentor;
56
import com.example.solidconnection.mentor.dto.MentorDetailResponse;
67
import com.example.solidconnection.mentor.dto.MentorPreviewResponse;
7-
import com.example.solidconnection.mentor.dto.MentorPreviewsResponse;
88
import com.example.solidconnection.mentor.repository.MentorBatchQueryRepository;
99
import com.example.solidconnection.mentor.repository.MentorRepository;
1010
import com.example.solidconnection.mentor.repository.MentoringRepository;
@@ -45,14 +45,14 @@ public MentorDetailResponse getMentorDetails(long mentorId, SiteUser currentUser
4545
}
4646

4747
@Transactional(readOnly = true)
48-
public MentorPreviewsResponse getMentorPreviews(String region, SiteUser siteUser, Pageable pageable) { // todo: 멘토의 '인증' 작업 후 region 필터링 추가
48+
public SliceResponse<MentorPreviewResponse> getMentorPreviews(String region, SiteUser siteUser, Pageable pageable) { // todo: 멘토의 '인증' 작업 후 region 필터링 추가
4949
Slice<Mentor> mentorPage = mentorRepository.findBy(pageable);
5050
List<Mentor> mentors = mentorPage.toList();
5151

5252
List<MentorPreviewResponse> content = getContent(mentors, siteUser);
5353
int pageNumber = getPageNumber(mentorPage);
5454

55-
return new MentorPreviewsResponse(content, pageNumber);
55+
return new SliceResponse<>(content, pageNumber);
5656
}
5757

5858
private List<MentorPreviewResponse> getContent(List<Mentor> mentors, SiteUser siteUser) {

src/test/java/com/example/solidconnection/mentor/service/MentorQueryServiceTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.example.solidconnection.mentor.service;
22

3+
import com.example.solidconnection.common.dto.SliceResponse;
34
import com.example.solidconnection.common.exception.CustomException;
45
import com.example.solidconnection.common.exception.ErrorCode;
56
import com.example.solidconnection.mentor.domain.Channel;
67
import com.example.solidconnection.mentor.domain.Mentor;
78
import com.example.solidconnection.mentor.dto.ChannelResponse;
89
import com.example.solidconnection.mentor.dto.MentorDetailResponse;
910
import com.example.solidconnection.mentor.dto.MentorPreviewResponse;
10-
import com.example.solidconnection.mentor.dto.MentorPreviewsResponse;
1111
import com.example.solidconnection.mentor.fixture.ChannelFixture;
1212
import com.example.solidconnection.mentor.fixture.MentorFixture;
1313
import com.example.solidconnection.mentor.fixture.MentoringFixture;
@@ -136,7 +136,7 @@ void setUp() {
136136
Channel channel2 = channelFixture.채널(2, mentor2);
137137

138138
// when
139-
MentorPreviewsResponse response = mentorQueryService.getMentorPreviews(region, currentUser, PageRequest.of(0, 10));
139+
SliceResponse<MentorPreviewResponse> response = mentorQueryService.getMentorPreviews(region, currentUser, PageRequest.of(0, 10));
140140

141141
// then
142142
Map<Long, MentorPreviewResponse> mentorPreviewMap = response.content().stream()
@@ -160,7 +160,7 @@ void setUp() {
160160
mentoringFixture.대기중_멘토링(mentor1.getId(), currentUser.getId());
161161

162162
// when
163-
MentorPreviewsResponse response = mentorQueryService.getMentorPreviews(region, currentUser, PageRequest.of(0, 10));
163+
SliceResponse<MentorPreviewResponse> response = mentorQueryService.getMentorPreviews(region, currentUser, PageRequest.of(0, 10));
164164

165165
// then
166166
Map<Long, MentorPreviewResponse> mentorPreviewMap = response.content().stream()
@@ -174,7 +174,7 @@ void setUp() {
174174
@Test
175175
void 다음_페이지_번호를_응답한다() {
176176
// given
177-
MentorPreviewsResponse response = mentorQueryService.getMentorPreviews(region, currentUser, PageRequest.of(0, 1));
177+
SliceResponse<MentorPreviewResponse> response = mentorQueryService.getMentorPreviews(region, currentUser, PageRequest.of(0, 1));
178178

179179
// then
180180
assertThat(response.nextPageNumber()).isEqualTo(2);
@@ -183,7 +183,7 @@ void setUp() {
183183
@Test
184184
void 다음_페이지가_없으면_페이지_없음을_의미하는_값을_응답한다() {
185185
// given
186-
MentorPreviewsResponse response = mentorQueryService.getMentorPreviews(region, currentUser, PageRequest.of(0, 10));
186+
SliceResponse<MentorPreviewResponse> response = mentorQueryService.getMentorPreviews(region, currentUser, PageRequest.of(0, 10));
187187

188188
// then
189189
assertThat(response.nextPageNumber()).isEqualTo(NO_NEXT_PAGE_NUMBER);

0 commit comments

Comments
 (0)