File tree Expand file tree Collapse file tree 4 files changed +38
-20
lines changed
src/main/java/kr/kro/photoliner/domain/photo Expand file tree Collapse file tree 4 files changed +38
-20
lines changed Original file line number Diff line number Diff line change 1212import kr .kro .photoliner .domain .photo .service .PhotoService ;
1313import kr .kro .photoliner .domain .photo .service .PhotoUploadService ;
1414import lombok .RequiredArgsConstructor ;
15+ import org .springframework .data .domain .Pageable ;
16+ import org .springframework .data .domain .Sort ;
17+ import org .springframework .data .web .PageableDefault ;
1518import org .springframework .http .HttpStatus ;
1619import org .springframework .http .MediaType ;
1720import 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" )
Original file line number Diff line number Diff line change 44import java .util .List ;
55import java .util .Optional ;
66import kr .kro .photoliner .domain .photo .model .Photo ;
7- import kr .kro .photoliner .domain .photo .model .Photos ;
87import org .locationtech .jts .geom .Point ;
8+ import org .springframework .data .domain .Page ;
99
1010public 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 ,
Original file line number Diff line number Diff line change 44import kr .kro .photoliner .domain .photo .model .Photo ;
55import kr .kro .photoliner .domain .photo .model .Photos ;
66import org .locationtech .jts .geom .Point ;
7+ import org .springframework .data .domain .Page ;
78import org .springframework .data .domain .Pageable ;
89import org .springframework .data .jpa .repository .JpaRepository ;
910import org .springframework .data .jpa .repository .Query ;
1011
1112public 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
Original file line number Diff line number Diff line change 1313import org .locationtech .jts .geom .Coordinate ;
1414import org .locationtech .jts .geom .GeometryFactory ;
1515import org .locationtech .jts .geom .Point ;
16- import org .springframework .data .domain .PageRequest ;
1716import org .springframework .data .domain .Pageable ;
18- import org .springframework .data .domain .Sort ;
1917import org .springframework .stereotype .Service ;
2018import 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 )
You can’t perform that action at this time.
0 commit comments