Skip to content

Commit bed244a

Browse files
authored
feat: 페이지 메타데이터 응답 추가 (#33)
1 parent d0f4086 commit bed244a

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

src/main/java/kr/kro/photoliner/domain/photo/controller/PhotoController.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import kr.kro.photoliner.domain.photo.service.PhotoService;
1313
import kr.kro.photoliner.domain.photo.service.PhotoUploadService;
1414
import lombok.RequiredArgsConstructor;
15+
import org.springframework.data.domain.Pageable;
16+
import org.springframework.data.domain.Sort;
17+
import org.springframework.data.web.PageableDefault;
1518
import org.springframework.http.HttpStatus;
1619
import org.springframework.http.MediaType;
1720
import org.springframework.http.ResponseEntity;
@@ -37,9 +40,10 @@ public class PhotoController {
3740

3841
@GetMapping
3942
public ResponseEntity<PhotosResponse> getPhotoList(
40-
@RequestParam Long userId
43+
@RequestParam Long userId,
44+
@PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable
4145
) {
42-
return ResponseEntity.ok(photoService.getPhotoList(userId));
46+
return ResponseEntity.ok(photoService.getPhotoList(userId, pageable));
4347
}
4448

4549
@GetMapping("/markers")

src/main/java/kr/kro/photoliner/domain/photo/dto/response/PhotosResponse.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,44 @@
44
import java.util.List;
55
import java.util.Optional;
66
import kr.kro.photoliner.domain.photo.model.Photo;
7-
import kr.kro.photoliner.domain.photo.model.Photos;
87
import org.locationtech.jts.geom.Point;
8+
import org.springframework.data.domain.Page;
99

1010
public record PhotosResponse(
11-
Integer count,
12-
List<InnerPhotoResponse> photos
11+
List<InnerPhotoResponse> photos,
12+
InnerPageInfo pageInfo
1313
) {
1414

15-
public static PhotosResponse from(Photos photos) {
15+
public static PhotosResponse from(Page<Photo> photoPage) {
1616
return new PhotosResponse(
17-
photos.count(),
18-
photos.photos().stream()
17+
photoPage.getContent().stream()
1918
.map(InnerPhotoResponse::from)
20-
.toList()
19+
.toList(),
20+
InnerPageInfo.from(photoPage)
2121
);
2222
}
2323

24+
public record InnerPageInfo(
25+
long totalElements,
26+
int totalPages,
27+
int currentPage,
28+
int size,
29+
boolean hasNext,
30+
boolean hasPrevious
31+
) {
32+
33+
public static InnerPageInfo from(Page<Photo> page) {
34+
return new InnerPageInfo(
35+
page.getTotalElements(),
36+
page.getTotalPages(),
37+
page.getNumber(),
38+
page.getSize(),
39+
page.hasNext(),
40+
page.hasPrevious()
41+
);
42+
}
43+
}
44+
2445
public record InnerPhotoResponse(
2546
Long id,
2647
String filePath,

src/main/java/kr/kro/photoliner/domain/photo/repository/PhotoRepository.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,18 @@
44
import kr.kro.photoliner.domain.photo.model.Photo;
55
import kr.kro.photoliner.domain.photo.model.Photos;
66
import org.locationtech.jts.geom.Point;
7+
import org.springframework.data.domain.Page;
78
import org.springframework.data.domain.Pageable;
89
import org.springframework.data.jpa.repository.JpaRepository;
910
import org.springframework.data.jpa.repository.Query;
1011

1112
public interface PhotoRepository extends JpaRepository<Photo, Long> {
1213

13-
List<Photo> findByUserId(
14+
Page<Photo> findByUserId(
1415
Long userId,
1516
Pageable pageable
1617
);
1718

18-
default Photos findPhotosByUserId(Long userId, Pageable pageable) {
19-
return new Photos(findByUserId(userId, pageable));
20-
}
21-
2219
@Query("""
2320
select p
2421
from Photo p

src/main/java/kr/kro/photoliner/domain/photo/service/PhotoService.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
import org.locationtech.jts.geom.Coordinate;
1414
import org.locationtech.jts.geom.GeometryFactory;
1515
import org.locationtech.jts.geom.Point;
16-
import org.springframework.data.domain.PageRequest;
1716
import org.springframework.data.domain.Pageable;
18-
import org.springframework.data.domain.Sort;
1917
import org.springframework.stereotype.Service;
2018
import org.springframework.transaction.annotation.Transactional;
2119

@@ -27,10 +25,8 @@ public class PhotoService {
2725
private final GeometryFactory geometryFactory;
2826

2927
@Transactional(readOnly = true)
30-
public PhotosResponse getPhotoList(Long userId) {
31-
Pageable pageable = PageRequest.of(0, 10, Sort.by("createdAt").descending());
32-
Photos photos = photoRepository.findPhotosByUserId(userId, pageable);
33-
return PhotosResponse.from(photos);
28+
public PhotosResponse getPhotoList(Long userId, Pageable pageable) {
29+
return PhotosResponse.from(photoRepository.findByUserId(userId, pageable));
3430
}
3531

3632
@Transactional(readOnly = true)

0 commit comments

Comments
 (0)