Skip to content

Commit 849d929

Browse files
chanrhanchanrhan
andauthored
fix: 지도 기반 마커 조회 API 요청 시 NPE 오류 해결 (#45)
* refactor: 지도 마커 조회 API 비즈니스 로직 수정(view 사용 안함) * chore: drop view 마이그레이션 파일 추가 * fix: pageable 오류 수정 * fix: get api에 requestbody 제거 * fix: 위도/경도 반대로 출력하고 있던 오류 수정 --------- Co-authored-by: chanrhan <km1104rs@naver.com>
1 parent a64c2eb commit 849d929

19 files changed

Lines changed: 270 additions & 220 deletions

src/main/java/kr/kro/photoliner/domain/album/controller/AlbumController.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import kr.kro.photoliner.domain.album.dto.request.AlbumDeleteRequest;
66
import kr.kro.photoliner.domain.album.dto.request.AlbumItemCreateRequest;
77
import kr.kro.photoliner.domain.album.dto.request.AlbumItemDeleteRequest;
8+
import kr.kro.photoliner.domain.album.dto.request.AlbumPhotoMarkersRequest;
89
import kr.kro.photoliner.domain.album.dto.request.AlbumTitleUpdateRequest;
910
import kr.kro.photoliner.domain.album.dto.response.AlbumCreateResponse;
1011
import kr.kro.photoliner.domain.album.dto.response.AlbumPhotoItemsResponse;
12+
import kr.kro.photoliner.domain.album.dto.response.AlbumPhotoMarkersResponse;
1113
import kr.kro.photoliner.domain.album.dto.response.AlbumsResponse;
1214
import kr.kro.photoliner.domain.album.service.AlbumService;
1315
import lombok.RequiredArgsConstructor;
@@ -69,7 +71,7 @@ public ResponseEntity<Void> deletePhoto(
6971
@GetMapping("/{albumId}/photos")
7072
public ResponseEntity<AlbumPhotoItemsResponse> getAlbumItems(
7173
@PathVariable Long albumId,
72-
@PageableDefault(sort = "capturedDt", direction = Sort.Direction.DESC) Pageable pageable
74+
@PageableDefault Pageable pageable
7375
) {
7476
return ResponseEntity.ok(albumService.getAlbumPhotoItems(albumId, pageable));
7577
}
@@ -91,4 +93,12 @@ public ResponseEntity<Void> deleteAlbumItems(
9193
albumService.deleteAlbumItems(albumId, request);
9294
return ResponseEntity.noContent().build();
9395
}
96+
97+
@GetMapping("/{albumId}/markers")
98+
public ResponseEntity<AlbumPhotoMarkersResponse> getAlbumPhotoMarkers(
99+
@PathVariable Long albumId,
100+
@Valid AlbumPhotoMarkersRequest request
101+
) {
102+
return ResponseEntity.ok(albumService.getAlbumPhotoMarkers(albumId, request));
103+
}
94104
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package kr.kro.photoliner.domain.album.dto;
2+
3+
import java.time.LocalDateTime;
4+
import java.util.Objects;
5+
import org.locationtech.jts.geom.Point;
6+
7+
public record AlbumPhotoItem(
8+
Long id,
9+
Long photoId,
10+
String fileName,
11+
String filePath,
12+
String thumbnailPath,
13+
LocalDateTime capturedDt,
14+
Point location
15+
) {
16+
17+
public Double getLatitude() {
18+
if (Objects.isNull(location)) {
19+
return null;
20+
}
21+
return location.getY();
22+
}
23+
24+
public Double getLongitude() {
25+
if (Objects.isNull(location)) {
26+
return null;
27+
}
28+
return location.getX();
29+
}
30+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package kr.kro.photoliner.domain.album.dto;
2+
3+
import java.util.List;
4+
5+
public record AlbumPhotoItems(
6+
List<AlbumPhotoItem> photoItems
7+
) {
8+
9+
public int count() {
10+
return photoItems.size();
11+
}
12+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package kr.kro.photoliner.domain.album.dto.request;
2+
3+
import jakarta.validation.constraints.Max;
4+
import jakarta.validation.constraints.Min;
5+
import org.locationtech.jts.geom.Coordinate;
6+
7+
public record AlbumPhotoMarkersRequest(
8+
@Min(0) @Max(90)
9+
double swLat,
10+
11+
@Min(0) @Max(180)
12+
double swLng,
13+
14+
@Min(0) @Max(90)
15+
double neLat,
16+
17+
@Min(0) @Max(180)
18+
double neLng
19+
) {
20+
public Coordinate getSouthWestCoordinate() {
21+
return new Coordinate(swLng, swLat);
22+
}
23+
24+
public Coordinate getNorthEastCoordinate() {
25+
return new Coordinate(neLng, neLat);
26+
}
27+
}

src/main/java/kr/kro/photoliner/domain/album/dto/response/AlbumPhotoItemsResponse.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import java.time.LocalDateTime;
44
import java.util.List;
5-
import kr.kro.photoliner.domain.album.model.view.AlbumPhotoView;
5+
import kr.kro.photoliner.domain.album.dto.AlbumPhotoItem;
66
import org.springframework.data.domain.Page;
77

88
public record AlbumPhotoItemsResponse(
99
List<InnerAlbumPhotoItem> items
1010
) {
1111

12-
public static AlbumPhotoItemsResponse from(Page<AlbumPhotoView> albumPhotoViews) {
12+
public static AlbumPhotoItemsResponse from(Page<AlbumPhotoItem> albumPhotoViews) {
1313
return new AlbumPhotoItemsResponse(
1414
albumPhotoViews.stream()
1515
.map(InnerAlbumPhotoItem::from)
@@ -26,14 +26,14 @@ public record InnerAlbumPhotoItem(
2626
LocalDateTime capturedDt
2727
) {
2828

29-
public static InnerAlbumPhotoItem from(AlbumPhotoView albumPhotoView) {
29+
public static InnerAlbumPhotoItem from(AlbumPhotoItem albumPhotoItem) {
3030
return new InnerAlbumPhotoItem(
31-
albumPhotoView.getId(),
32-
albumPhotoView.getPhotoId(),
33-
albumPhotoView.getFileName(),
34-
albumPhotoView.getFilePath(),
35-
albumPhotoView.getThumbnailPath(),
36-
albumPhotoView.getCapturedDt()
31+
albumPhotoItem.id(),
32+
albumPhotoItem.photoId(),
33+
albumPhotoItem.fileName(),
34+
albumPhotoItem.filePath(),
35+
albumPhotoItem.thumbnailPath(),
36+
albumPhotoItem.capturedDt()
3737
);
3838
}
3939
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package kr.kro.photoliner.domain.album.dto.response;
2+
3+
import java.time.LocalDateTime;
4+
import java.util.List;
5+
import kr.kro.photoliner.domain.album.dto.AlbumPhotoItem;
6+
import kr.kro.photoliner.domain.album.dto.AlbumPhotoItems;
7+
8+
public record AlbumPhotoMarkersResponse(
9+
Integer count,
10+
List<InnerAlbumPhotoMarker> albumPhotoMarkers
11+
) {
12+
13+
public static AlbumPhotoMarkersResponse from(AlbumPhotoItems albumPhotoItems) {
14+
return new AlbumPhotoMarkersResponse(
15+
albumPhotoItems.count(),
16+
albumPhotoItems.photoItems().stream()
17+
.map(InnerAlbumPhotoMarker::from)
18+
.toList()
19+
);
20+
}
21+
22+
public record InnerAlbumPhotoMarker(
23+
Long id,
24+
Long photoId,
25+
String filePath,
26+
String thumbnailPath,
27+
LocalDateTime capturedDt,
28+
Double lat,
29+
Double lng
30+
) {
31+
32+
public static InnerAlbumPhotoMarker from(AlbumPhotoItem albumPhotoItem) {
33+
return new InnerAlbumPhotoMarker(
34+
albumPhotoItem.id(),
35+
albumPhotoItem.photoId(),
36+
albumPhotoItem.filePath(),
37+
albumPhotoItem.thumbnailPath(),
38+
albumPhotoItem.capturedDt(),
39+
albumPhotoItem.getLatitude(),
40+
albumPhotoItem.getLongitude()
41+
);
42+
}
43+
}
44+
}

src/main/java/kr/kro/photoliner/domain/album/model/view/AlbumPhotoView.java

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

src/main/java/kr/kro/photoliner/domain/album/model/view/AlbumPhotoViews.java

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,54 @@
11
package kr.kro.photoliner.domain.album.repository;
22

33
import java.util.List;
4-
import kr.kro.photoliner.domain.album.model.view.AlbumPhotoView;
5-
import kr.kro.photoliner.domain.album.model.view.AlbumPhotoViews;
4+
import kr.kro.photoliner.domain.album.dto.AlbumPhotoItem;
5+
import kr.kro.photoliner.domain.album.dto.AlbumPhotoItems;
6+
import kr.kro.photoliner.domain.album.model.PhotoItem;
67
import org.locationtech.jts.geom.Point;
78
import org.springframework.data.domain.Page;
89
import org.springframework.data.domain.Pageable;
910
import org.springframework.data.jpa.repository.JpaRepository;
1011
import org.springframework.data.jpa.repository.Query;
1112

12-
public interface AlbumPhotoRepository extends JpaRepository<AlbumPhotoView, Long> {
13-
Page<AlbumPhotoView> findByAlbumId(Long albumId, Pageable pageable);
13+
public interface AlbumPhotoRepository extends JpaRepository<PhotoItem, Long> {
14+
@Query("""
15+
select new kr.kro.photoliner.domain.album.dto.AlbumPhotoItem(
16+
pi.id,
17+
pi.photoId,
18+
p.fileName,
19+
p.filePath,
20+
p.thumbnailPath,
21+
p.capturedDt,
22+
p.location
23+
)
24+
from PhotoItem pi
25+
inner join Photo p on p.id = pi.photoId
26+
where pi.album.id = :albumId
27+
""")
28+
Page<AlbumPhotoItem> findByAlbumId(Long albumId, Pageable pageable);
1429

1530
@Query("""
16-
select apv
17-
from AlbumPhotoView apv
18-
where apv.userId = :userId
19-
and function('st_x', apv.location) between function('st_x', :sw) and function('st_x', :ne)
20-
and function('st_y', apv.location) between function('st_y', :sw) and function('st_y', :ne)
21-
order by apv.capturedDt desc
31+
select new kr.kro.photoliner.domain.album.dto.AlbumPhotoItem(
32+
pi.id,
33+
pi.photoId,
34+
p.fileName,
35+
p.filePath,
36+
p.thumbnailPath,
37+
p.capturedDt,
38+
p.location
39+
)
40+
from PhotoItem pi
41+
inner join Photo p on p.id = pi.photoId
42+
where pi.album.id = :albumId
43+
and function('st_x', p.location) between function('st_x', :sw) and function('st_x', :ne)
44+
and function('st_y', p.location) between function('st_y', :sw) and function('st_y', :ne)
45+
order by p.capturedDt
2246
""")
23-
List<AlbumPhotoView> findByUserIdInBox(Long userId, Point sw, Point ne);
47+
List<AlbumPhotoItem> findByAlbumIdInBox(Long albumId, Point sw, Point ne);
2448

25-
default AlbumPhotoViews getByUserIdInBox(Long userId, Point sw, Point ne) {
26-
return new AlbumPhotoViews(
27-
findByUserIdInBox(userId, sw, ne)
49+
default AlbumPhotoItems getByAlbumIdInBox(Long albumId, Point sw, Point ne) {
50+
return new AlbumPhotoItems(
51+
findByAlbumIdInBox(albumId, sw, ne)
2852
);
2953
}
3054
}

0 commit comments

Comments
 (0)