Skip to content

Commit

Permalink
지역별 수거함 검색 기능의 데이터 조회 로직을 QueryDSL로 마이그레이션 (#128)
Browse files Browse the repository at this point in the history
* refactor: repository 클래스들 패키지 위치 변경

* refactor: searchBySigunguNm 메서드의 데이터 조회 로직을 QueryDSL로 마이그레이션

* refactor: searchByDongNm 메서드의 데이터 조회 로직을 QueryDSL로 마이그레이션
  • Loading branch information
Hanjaemo authored Aug 9, 2024
1 parent 7d104a4 commit 62501d0
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package contest.collectingbox.module.collectingbox.application;

import contest.collectingbox.module.collectingbox.domain.CollectingBox;
import contest.collectingbox.module.collectingbox.domain.Tags;
import contest.collectingbox.module.collectingbox.domain.repository.CollectingBoxRepository;
import contest.collectingbox.module.collectingbox.dto.CollectingBoxDetailResponse;
import contest.collectingbox.module.collectingbox.dto.CollectingBoxResponse;
import contest.collectingbox.module.location.domain.DongInfo;
import contest.collectingbox.module.location.domain.DongInfoRepository;
import contest.collectingbox.module.location.domain.GeoPoint;
import contest.collectingbox.module.location.domain.repository.DongInfoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -55,18 +52,10 @@ public List<CollectingBoxResponse> searchCollectingBoxes(final String query, fin
}

private List<CollectingBoxResponse> searchBySigunguNm(String query, Tags tags) {
return dongInfoRepository.findAllBySigunguNm(query).stream()
.flatMap(dongInfo ->
collectingBoxRepository.findAllByDongInfoAndTags(dongInfo, tags.getTags()).stream())
.map(CollectingBoxResponse::fromEntity)
.collect(Collectors.toList());
return collectingBoxRepository.searchBySigunguNm(query, tags);
}

private List<CollectingBoxResponse> searchByDongNm(String dongNm, Tags tags) {
DongInfo dongInfo = dongInfoRepository.findByDongNm(dongNm);
List<CollectingBox> boxes = collectingBoxRepository.findAllByDongInfoAndTags(dongInfo, tags.getTags());
return boxes.stream()
.map(CollectingBoxResponse::fromEntity)
.collect(Collectors.toList());
private List<CollectingBoxResponse> searchByDongNm(String query, Tags tags) {
return collectingBoxRepository.searchByDongNm(query, tags);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ public interface CollectingBoxRepositoryCustom {
CollectingBoxDetailResponse findDetailById(Long id);

List<CollectingBoxResponse> findAllWithinArea(GeoPoint center, int radius, Tags tags);

List<CollectingBoxResponse> searchBySigunguNm(String query, Tags tags);

List<CollectingBoxResponse> searchByDongNm(String query, Tags tags);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import contest.collectingbox.global.exception.CollectingBoxException;
import contest.collectingbox.global.utils.GeometryUtil;
Expand All @@ -20,6 +21,7 @@

import static contest.collectingbox.global.exception.ErrorCode.NOT_FOUND_COLLECTING_BOX;
import static contest.collectingbox.module.collectingbox.domain.QCollectingBox.collectingBox;
import static contest.collectingbox.module.location.domain.QDongInfo.dongInfo;
import static contest.collectingbox.module.location.domain.QLocation.location;
import static contest.collectingbox.module.review.domain.QReview.review;

Expand Down Expand Up @@ -87,4 +89,46 @@ public CollectingBoxDetailResponse findDetailById(Long id) {

return response;
}

@Override
public List<CollectingBoxResponse> searchBySigunguNm(String query, Tags tags) {
return queryFactory
.select(new QCollectingBoxResponse(
collectingBox.id,
Expressions.stringTemplate("function('st_longitude', {0})", location.point)
.castToNum(double.class),
Expressions.stringTemplate("function('st_latitude', {0})", location.point)
.castToNum(double.class),
collectingBox.tag
))
.from(collectingBox)
.join(collectingBox.location, location)
.where(location.dongInfo.dongCd.in(
JPAExpressions
.select(dongInfo.dongCd)
.from(dongInfo)
.where(dongInfo.sigunguNm.eq(query))))
.fetch();
}

@Override
public List<CollectingBoxResponse> searchByDongNm(String query, Tags tags) {
return queryFactory
.select(new QCollectingBoxResponse(
collectingBox.id,
Expressions.stringTemplate("function('st_longitude', {0})", location.point)
.castToNum(double.class),
Expressions.stringTemplate("function('st_latitude', {0})", location.point)
.castToNum(double.class),
collectingBox.tag
))
.from(collectingBox)
.join(collectingBox.location, location)
.where(location.dongInfo.dongCd.in(
JPAExpressions
.select(dongInfo.dongCd)
.from(dongInfo)
.where(dongInfo.dongNm.eq(query))))
.fetch();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package contest.collectingbox.module.location.domain.repository;

import contest.collectingbox.module.location.domain.DongInfo;
import org.springframework.data.jpa.repository.JpaRepository;

public interface DongInfoRepository extends JpaRepository<DongInfo, Long> {
DongInfo findBySigunguNmAndDongNm(String sigunguNm, String dongNm);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package contest.collectingbox.module.location.domain;
package contest.collectingbox.module.location.domain.repository;

import contest.collectingbox.module.location.domain.Location;
import org.springframework.data.jpa.repository.JpaRepository;

public interface LocationRepository extends JpaRepository<Location, Long> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import contest.collectingbox.global.utils.GeometryUtil;
import contest.collectingbox.module.collectingbox.domain.CollectingBox;
import contest.collectingbox.module.collectingbox.domain.Tag;
import contest.collectingbox.module.location.domain.DongInfoRepository;
import contest.collectingbox.module.location.domain.repository.DongInfoRepository;
import contest.collectingbox.module.location.domain.Location;
import lombok.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.opencsv.exceptions.CsvValidationException;
import contest.collectingbox.module.collectingbox.domain.repository.CollectingBoxRepository;
import contest.collectingbox.module.collectingbox.domain.Tag;
import contest.collectingbox.module.location.domain.DongInfoRepository;
import contest.collectingbox.module.location.domain.repository.DongInfoRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
Expand Down

0 comments on commit 62501d0

Please sign in to comment.