Skip to content

Commit

Permalink
#23, #18 [Add, Refactor] QueryDSL로 필터링, 페이징 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
JSoi committed Jul 11, 2022
1 parent 2393c59 commit 2cd89f8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ public List<StoreResponseDto> giveStoreInRange(@RequestParam BigDecimal latStart
@RequestParam BigDecimal latEnd,
@RequestParam BigDecimal lngStart,
@RequestParam BigDecimal lngEnd,
@RequestParam(required = false) String category,
@RequestParam(required = false) List<String> facility,
// sort 기준 정하기
//@PageableDefault(sort = {""}, direction = Sort.Direction.DESC) Pageable pageable){
@PageableDefault Pageable pageable) {
List<Store> betweenLngLat = storeQueryRepository.findBetweenLngLat(latStart, latEnd, lngStart, lngEnd, pageable);
return betweenLngLat.stream().map(storeService::storeToResDto).collect(Collectors.toList());
List<Store> betweenLngLat = storeQueryRepository.findBetweenLngLat(latStart, latEnd, lngStart, lngEnd, category, facility, pageable);
return betweenLngLat.parallelStream().map(storeService::storeToResDto).collect(Collectors.toList());// 순서보장
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package com.mpnp.baechelin.store.controller;

import com.mpnp.baechelin.store.domain.QStore;
import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.tag.domain.QTag;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.CaseBuilder;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.StringPath;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport;
Expand All @@ -9,6 +17,7 @@
import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import static com.mpnp.baechelin.store.domain.QStore.store;

Expand All @@ -17,24 +26,66 @@
public class StoreQueryRepository extends QuerydslRepositorySupport {
private final JPAQueryFactory queryFactory;

public StoreQueryRepository(JPAQueryFactory queryFactory) {
public StoreQueryRepository(JPAQueryFactory queryFactory) {
super(Store.class);
this.queryFactory = queryFactory;
}

// TODO 카테고리, 시설 추가하기
public List<Store> findBetweenLngLat(BigDecimal latStart,
BigDecimal latEnd,
BigDecimal lngStart,
BigDecimal lngEnd,
String category,
List<String> facility,
Pageable pageable) {

BooleanBuilder builder = new BooleanBuilder();
builder.and(store.latitude.goe(latStart));
builder.and(store.latitude.loe(latEnd));
builder.and(store.longitude.goe(lngStart));
builder.and(store.longitude.loe(lngEnd));
builder.and(category == null ? null : store.category.eq(category));
if (facility != null && facility.size() > 0) {
for (String fac : facility) {
builder.and(facilityTF(fac));
}
}

List<Store> storeList = queryFactory.selectFrom(store)
.where(store.latitude.goe(latStart),
store.latitude.loe(latEnd),
store.longitude.goe(lngStart),
store.longitude.loe(lngEnd))
.where(builder)
.limit(pageable.getPageSize())
.offset(pageable.getOffset())
.fetch();

return storeList;
}

private BooleanExpression facilityTF(String facility) {
if (facility == null || facility.isEmpty()) return null;
return givePath(facility).eq("Y");
}

private StringPath givePath(String dbFacility) {
if (dbFacility.equals("elevator"))
return store.elevator;
if (dbFacility.equals("heightDifferent"))
return store.heightDifferent;
if (dbFacility.equals("parking"))
return store.parking;
if (dbFacility.equals("approach"))
return store.approach;
else
return store.toilet;
}



//TODO 주변밥집

//TODO 별점순

//TODO 실시간 맛집

//TODO 북마크순
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mpnp.baechelin.api.model;
package com.mpnp.baechelin.store.domain;

import java.util.Arrays;

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/mpnp/baechelin/store/domain/Facility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.mpnp.baechelin.store.domain;

public enum Facility {
}

0 comments on commit 2cd89f8

Please sign in to comment.