Skip to content

Commit aa95e77

Browse files
committed
refactor: QueryDSL 사용 하여 코드 가독성 개선
1 parent abaa796 commit aa95e77

File tree

9 files changed

+116
-66
lines changed

9 files changed

+116
-66
lines changed

src/main/java/com/example/log4u/domain/map/dto/response/AreaClusterProjection.java

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package com.example.log4u.domain.map.dto.response;
22

3+
import com.querydsl.core.annotations.QueryProjection;
4+
35
public record DiaryClusterResponseDto(
46
String areaName,
57
Long areaId,
68
Double lat,
79
Double lon,
810
Long diaryCount
911
) {
10-
public static DiaryClusterResponseDto of(AreaClusterProjection proj) {
11-
return new DiaryClusterResponseDto(
12-
proj.getName(),
13-
proj.getId(),
14-
proj.getLat(),
15-
proj.getLon(),
16-
proj.getDiaryCount()
17-
);
12+
13+
@QueryProjection
14+
public DiaryClusterResponseDto {
1815
}
16+
1917
}
Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,20 @@
11
package com.example.log4u.domain.map.repository;
22

3-
import java.util.List;
43
import java.util.Optional;
54

65
import org.springframework.data.jpa.repository.JpaRepository;
76
import org.springframework.data.jpa.repository.Query;
87
import org.springframework.data.repository.query.Param;
98
import org.springframework.stereotype.Repository;
109

11-
import com.example.log4u.domain.map.dto.response.AreaClusterProjection;
1210
import com.example.log4u.domain.map.entity.SidoAreas;
1311

1412
@Repository
15-
public interface SidoAreasRepository extends JpaRepository<SidoAreas, Long> {
16-
17-
@Query(value = """
18-
SELECT s.id AS id,
19-
s.name AS name,
20-
s.lat AS lat,
21-
s.lon AS lon,
22-
COALESCE(c.diary_count, 0) AS diaryCount
23-
FROM sido_areas s
24-
LEFT JOIN sido_areas_diary_count c ON s.id = c.id
25-
WHERE s.lat BETWEEN :south AND :north
26-
AND s.lon BETWEEN :west AND :east
27-
""", nativeQuery = true)
28-
List<AreaClusterProjection> findSidoAreaClusters(
29-
@Param("south") double south,
30-
@Param("north") double north,
31-
@Param("west") double west,
32-
@Param("east") double east
33-
);
13+
public interface SidoAreasRepository extends JpaRepository<SidoAreas, Long>,SidoAreasRepositoryCustom {
3414

3515
@Query("""
3616
SELECT s FROM SidoAreas s
3717
WHERE ST_Contains(s.geom, ST_SetSRID(ST_Point(:lon, :lat), 4326)) = true
3818
""")
3919
Optional<SidoAreas> findRegionByLatLon(@Param("lat") Double lat, @Param("lon") Double lon);
40-
4120
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.log4u.domain.map.repository;
2+
3+
import java.util.List;
4+
5+
import com.example.log4u.domain.map.dto.response.DiaryClusterResponseDto;
6+
7+
public interface SidoAreasRepositoryCustom {
8+
List<DiaryClusterResponseDto> findSidoAreaClusters(double south, double north, double west, double east);
9+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.example.log4u.domain.map.repository;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Qualifier;
6+
import org.springframework.stereotype.Repository;
7+
8+
import com.example.log4u.domain.map.dto.response.DiaryClusterResponseDto;
9+
import com.example.log4u.domain.map.dto.response.QDiaryClusterResponseDto;
10+
import com.example.log4u.domain.map.entity.QSidoAreas;
11+
import com.example.log4u.domain.map.entity.QSidoAreasDiaryCount;
12+
import com.querydsl.jpa.impl.JPAQueryFactory;
13+
14+
@Repository
15+
public class SidoAreasRepositoryImpl implements SidoAreasRepositoryCustom {
16+
17+
private final JPAQueryFactory queryFactory;
18+
19+
public SidoAreasRepositoryImpl(@Qualifier("postgresJPAQueryFactory") JPAQueryFactory queryFactory) {
20+
this.queryFactory = queryFactory;
21+
}
22+
23+
@Override
24+
public List<DiaryClusterResponseDto> findSidoAreaClusters(double south, double north, double west, double east) {
25+
QSidoAreas s = QSidoAreas.sidoAreas;
26+
QSidoAreasDiaryCount c = QSidoAreasDiaryCount.sidoAreasDiaryCount;
27+
28+
return queryFactory
29+
.select(new QDiaryClusterResponseDto(
30+
s.name,
31+
s.id,
32+
s.lat,
33+
s.lon,
34+
c.diaryCount.coalesce(0L)
35+
))
36+
.from(s)
37+
.leftJoin(c).on(s.id.eq(c.id))
38+
.where(
39+
s.lat.between(south, north),
40+
s.lon.between(west, east)
41+
)
42+
.fetch();
43+
}
44+
}

src/main/java/com/example/log4u/domain/map/repository/SiggAreasRepository.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,16 @@
11
package com.example.log4u.domain.map.repository;
22

3-
import java.util.List;
43
import java.util.Optional;
54

65
import org.springframework.data.jpa.repository.JpaRepository;
76
import org.springframework.data.jpa.repository.Query;
87
import org.springframework.data.repository.query.Param;
98
import org.springframework.stereotype.Repository;
109

11-
import com.example.log4u.domain.map.dto.response.AreaClusterProjection;
1210
import com.example.log4u.domain.map.entity.SiggAreas;
1311

1412
@Repository
15-
public interface SiggAreasRepository extends JpaRepository<SiggAreas, Long> {
16-
17-
@Query(value = """
18-
SELECT s.gid AS id,
19-
s.sgg_nm AS name,
20-
s.lat AS lat,
21-
s.lon AS lon,
22-
COALESCE(c.diary_count, 0) AS diaryCount
23-
FROM sigg_areas s
24-
LEFT JOIN sigg_areas_diary_count c ON s.gid = c.id
25-
WHERE s.lat BETWEEN :south AND :north
26-
AND s.lon BETWEEN :west AND :east
27-
""", nativeQuery = true)
28-
List<AreaClusterProjection> findSiggAreaClusters(
29-
@Param("south") double south,
30-
@Param("north") double north,
31-
@Param("west") double west,
32-
@Param("east") double east
33-
);
13+
public interface SiggAreasRepository extends JpaRepository<SiggAreas, Long>, SiggAreasRepositoryCustom {
3414

3515
@Query("""
3616
SELECT r FROM SiggAreas r
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.log4u.domain.map.repository;
2+
3+
import java.util.List;
4+
5+
import com.example.log4u.domain.map.dto.response.DiaryClusterResponseDto;
6+
7+
public interface SiggAreasRepositoryCustom {
8+
List<DiaryClusterResponseDto> findSiggAreaClusters(double south, double north, double west, double east);
9+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.example.log4u.domain.map.repository;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Qualifier;
6+
import org.springframework.stereotype.Repository;
7+
8+
import com.example.log4u.domain.map.dto.response.DiaryClusterResponseDto;
9+
import com.example.log4u.domain.map.dto.response.QDiaryClusterResponseDto;
10+
import com.example.log4u.domain.map.entity.QSiggAreas;
11+
import com.example.log4u.domain.map.entity.QSiggAreasDiaryCount;
12+
import com.querydsl.jpa.impl.JPAQueryFactory;
13+
14+
@Repository
15+
public class SiggAreasRepositoryImpl implements SiggAreasRepositoryCustom {
16+
17+
private final JPAQueryFactory queryFactory;
18+
19+
public SiggAreasRepositoryImpl(@Qualifier("postgresJPAQueryFactory") JPAQueryFactory queryFactory) {
20+
this.queryFactory = queryFactory;
21+
}
22+
23+
@Override
24+
public List<DiaryClusterResponseDto> findSiggAreaClusters(double south, double north, double west, double east) {
25+
QSiggAreas s = QSiggAreas.siggAreas;
26+
QSiggAreasDiaryCount c = QSiggAreasDiaryCount.siggAreasDiaryCount;
27+
28+
return queryFactory
29+
.select(new QDiaryClusterResponseDto(
30+
s.sggName,
31+
s.gid,
32+
s.lat,
33+
s.lon,
34+
c.diaryCount.coalesce(0L)
35+
))
36+
.from(s)
37+
.leftJoin(c).on(s.gid.eq(c.id))
38+
.where(
39+
s.lat.between(south, north),
40+
s.lon.between(west, east)
41+
)
42+
.fetch();
43+
}
44+
}

src/main/java/com/example/log4u/domain/map/service/MapService.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,11 @@ public List<DiaryClusterResponseDto> getDiaryClusters(double south, double north
3434
}
3535

3636
private List<DiaryClusterResponseDto> getSidoAreasClusters(double south, double north, double west, double east) {
37-
return sidoAreasRepository.findSidoAreaClusters(south, north, west, east).stream()
38-
.map(DiaryClusterResponseDto::of)
39-
.toList();
37+
return sidoAreasRepository.findSidoAreaClusters(south, north, west, east);
4038
}
4139

4240
private List<DiaryClusterResponseDto> getSiggAreasClusters(double south, double north, double west, double east) {
43-
return siggAreasRepository.findSiggAreaClusters(south, north, west, east).stream()
44-
.map(DiaryClusterResponseDto::of)
45-
.toList();
41+
return siggAreasRepository.findSiggAreaClusters(south, north, west, east);
4642
}
4743

4844
public void increaseRegionDiaryCount(Double lat, Double lon) {

0 commit comments

Comments
 (0)