-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from 9oormthon-univ/develop
feat: 카테고리 별 가게 목록 조회 구현
- Loading branch information
Showing
9 changed files
with
261 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/jangburich/domain/store/domain/dto/condition/StoreSearchCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.jangburich.domain.store.domain.dto.condition; | ||
|
||
public record StoreSearchCondition( | ||
Double lat, | ||
Double lon | ||
) { | ||
} |
20 changes: 20 additions & 0 deletions
20
...n/java/com/jangburich/domain/store/domain/dto/condition/StoreSearchConditionWithType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.jangburich.domain.store.domain.dto.condition; | ||
|
||
import com.querydsl.core.annotations.QueryProjection; | ||
|
||
public record StoreSearchConditionWithType( | ||
Boolean isReservable, | ||
Boolean isOperational, | ||
Boolean isPrepaid, | ||
Boolean isPostpaid | ||
) { | ||
|
||
@QueryProjection | ||
public StoreSearchConditionWithType(Boolean isReservable, Boolean isOperational, Boolean isPrepaid, | ||
Boolean isPostpaid) { | ||
this.isReservable = isReservable; | ||
this.isOperational = isOperational; | ||
this.isPrepaid = isPrepaid; | ||
this.isPostpaid = isPostpaid; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/jangburich/domain/store/domain/dto/response/SearchStoresResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.jangburich.domain.store.domain.dto.response; | ||
|
||
import com.jangburich.domain.store.domain.Category; | ||
import com.querydsl.core.annotations.QueryProjection; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record SearchStoresResponse( | ||
Long storeId, | ||
String name, | ||
Boolean isFavorite, | ||
Category category, | ||
Double distance, | ||
String businessStatus, | ||
String closeTime, | ||
String phoneNumber, | ||
String imageUrl | ||
) { | ||
|
||
@QueryProjection | ||
public SearchStoresResponse(Long storeId, String name, Boolean isFavorite, Category category, Double distance, | ||
String businessStatus, String closeTime, String phoneNumber, String imageUrl) { | ||
this.storeId = storeId; | ||
this.name = name; | ||
this.isFavorite = isFavorite; | ||
this.category = category; | ||
this.distance = distance; | ||
this.businessStatus = businessStatus; | ||
this.closeTime = closeTime; | ||
this.phoneNumber = phoneNumber; | ||
this.imageUrl = imageUrl; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/jangburich/domain/store/domain/repository/StoreQueryDslRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.jangburich.domain.store.domain.repository; | ||
|
||
import com.jangburich.domain.store.domain.Category; | ||
import com.jangburich.domain.store.domain.dto.condition.StoreSearchCondition; | ||
import com.jangburich.domain.store.domain.dto.condition.StoreSearchConditionWithType; | ||
import com.jangburich.domain.store.domain.dto.response.SearchStoresResponse; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface StoreQueryDslRepository { | ||
Page<SearchStoresResponse> findStoresByCategory(Long userId, Integer searchRadius, Category category, StoreSearchCondition storeSearchCondition, Pageable pageable); | ||
|
||
Page<SearchStoresResponse> findStores(Long userId, String keyword, StoreSearchConditionWithType storeSearchConditionWithType, Pageable pageable); | ||
} |
117 changes: 117 additions & 0 deletions
117
src/main/java/com/jangburich/domain/store/domain/repository/StoreQueryDslRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.jangburich.domain.store.domain.repository; | ||
|
||
import static com.jangburich.domain.store.domain.QStore.store; | ||
|
||
import com.jangburich.domain.store.domain.Category; | ||
import com.jangburich.domain.store.domain.dto.condition.StoreSearchCondition; | ||
import com.jangburich.domain.store.domain.dto.condition.StoreSearchConditionWithType; | ||
import com.jangburich.domain.store.domain.dto.response.QSearchStoresResponse; | ||
import com.jangburich.domain.store.domain.dto.response.SearchStoresResponse; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.core.types.dsl.Expressions; | ||
import com.querydsl.jpa.impl.JPAQuery; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import java.time.LocalTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.support.PageableExecutionUtils; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@RequiredArgsConstructor | ||
@Repository | ||
public class StoreQueryDslRepositoryImpl implements StoreQueryDslRepository { | ||
|
||
private static final int RADIUS_OF_EARTH_KM = 6371; | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public Page<SearchStoresResponse> findStoresByCategory(Long userId, Integer searchRadius, Category category, | ||
StoreSearchCondition storeSearchCondition, Pageable pageable) { | ||
double myCurrentLat = storeSearchCondition.lat(); | ||
double myCurrentLon = storeSearchCondition.lon(); | ||
|
||
List<SearchStoresResponse> results = queryFactory | ||
.select(new QSearchStoresResponse(store.id, store.name, Expressions.FALSE, store.category, | ||
Expressions.constant(1.0), Expressions.constant("open"), | ||
store.closeTime.stringValue(), store.contactNumber, store.representativeImage)) | ||
.from(store) | ||
.where( | ||
store.category.eq(category), | ||
withinSearchRadius(myCurrentLat, myCurrentLon, searchRadius, store.latitude, store.longitude) | ||
) | ||
.orderBy(store.id.desc()) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
|
||
JPAQuery<Long> countQuery = queryFactory | ||
.select(store.count()) | ||
.from(store) | ||
.where( | ||
store.category.eq(category), | ||
withinSearchRadius(myCurrentLat, myCurrentLon, searchRadius, store.latitude, store.longitude) | ||
); | ||
|
||
return PageableExecutionUtils.getPage(results, pageable, () -> countQuery.fetch().size()); | ||
} | ||
|
||
@Override | ||
public Page<SearchStoresResponse> findStores(Long userId, String keyword, | ||
StoreSearchConditionWithType storeSearchConditionWithType, | ||
Pageable pageable) { | ||
Boolean reservable = storeSearchConditionWithType.isReservable(); | ||
Boolean operational = storeSearchConditionWithType.isOperational(); | ||
Boolean prepaid = storeSearchConditionWithType.isPrepaid(); | ||
Boolean postpaid = storeSearchConditionWithType.isPostpaid(); | ||
|
||
List<SearchStoresResponse> results = queryFactory | ||
.select(new QSearchStoresResponse(store.id, store.name, Expressions.FALSE, store.category, | ||
Expressions.constant(1.0), Expressions.constant("open"), | ||
store.closeTime.stringValue(), store.contactNumber, store.representativeImage)) | ||
.from(store) | ||
.where( | ||
store.reservationAvailable.eq(reservable), | ||
store.name.contains(keyword) | ||
) | ||
.orderBy(store.id.desc()) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
|
||
JPAQuery<Long> countQuery = queryFactory | ||
.select(store.count()) | ||
.from(store) | ||
.where( | ||
store.reservationAvailable.eq(reservable), | ||
store.name.contains(keyword) | ||
); | ||
|
||
return PageableExecutionUtils.getPage(results, pageable, () -> countQuery.fetch().size()); | ||
} | ||
|
||
// TO DO | ||
private BooleanExpression isStoreCurrentlyOpen(String openTime, String closeTime) { | ||
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm"); | ||
|
||
String currentTimeStr = LocalTime.now().format(timeFormatter); | ||
LocalTime currentTime = LocalTime.parse(currentTimeStr, timeFormatter); | ||
|
||
LocalTime open = LocalTime.parse(openTime, timeFormatter); | ||
LocalTime close = LocalTime.parse(closeTime, timeFormatter); | ||
|
||
return Expressions.booleanTemplate("{0} >= {1} and {0} < {2}", currentTime, openTime, closeTime); | ||
} | ||
|
||
private BooleanExpression withinSearchRadius(double userLat, double userLng, int searchRadius, | ||
com.querydsl.core.types.dsl.NumberPath<Double> storeLat, | ||
com.querydsl.core.types.dsl.NumberPath<Double> storeLng) { | ||
return Expressions.numberTemplate(Double.class, | ||
"({0} * acos(cos(radians({1})) * cos(radians({2})) * cos(radians({3}) - radians({4})) + sin(radians({1})) * sin(radians({2}))))", | ||
RADIUS_OF_EARTH_KM, userLat, storeLat, storeLng, userLng) | ||
.loe(searchRadius); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/com/jangburich/domain/store/exception/ResourceNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.jangburich.domain.store.exception; | ||
|
||
public class ResourceNotFoundException extends RuntimeException { | ||
|
||
public ResourceNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |