Skip to content

Commit

Permalink
#23 [Update] 거리 수정 및 쿼리 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
JSoi committed Jul 15, 2022
1 parent 24efe68 commit 69b0787
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static BooleanBuilder locAndConditions(BigDecimal latStart, BigDecimal la
public static BooleanBuilder locTwoPointAndConditions(BigDecimal latitude, BigDecimal longitude, String category, List<String> facility) {
BooleanBuilder builder = new BooleanBuilder();
if (latitude != null && longitude != null) {
BigDecimal[] location = getRange(latitude, longitude, 20);
BigDecimal[] location = getRange(latitude, longitude, 10);
builder = locationBuilder(location[0], location[1], location[2], location[3]);
}
return getBooleanBuilder(category, facility, builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

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

Expand All @@ -25,15 +26,17 @@ public class ReviewMainResponseDto {
private double point; //별점
private List<ReviewImageResponseDto> reviewImageUrlList; //리뷰 이미지 사진
private List<ReviewResponseDto.TagResponseDto> tagList;

public ReviewMainResponseDto(Review review, Store store, User user) {
this.storeId = store.getId();
this.userId = user.getId();
this.storeName = store.getName();
this.userName = user.getName();
this.userName = user.getEmail();
this.content = review.getContent();
this.point = review.getPoint();
this.reviewImageUrlList = review.getReviewImageList()
.parallelStream().map(ReviewImageResponseDto::new).collect(Collectors.toList());
this.tagList = review.getTagList().parallelStream().map(ReviewResponseDto.TagResponseDto::new).collect(Collectors.toList());
.stream().map(ReviewImageResponseDto::new).collect(Collectors.toList());
this.tagList = review.getTagList()
.stream().map(ReviewResponseDto.TagResponseDto::new).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ public List<Review> findRecentReviews(BigDecimal lat,
int limit) {
BooleanBuilder builder = new BooleanBuilder();
if (lat != null && lng != null) {
BigDecimal[] location = QuerydslLocation.getRange(lat, lng, 20);
BigDecimal[] location = QuerydslLocation.getRange(lat, lng, 10);
builder = QuerydslLocation.locationBuilder(location[0], location[1], location[2], location[3]);
}
// 위도 경도에 해당하는 가게를 찾음 -> 해당 댓글을 다 가져옴 -> 내림차순 정렬 -> limit
// TODO 쿼리문 개선하기
return queryFactory.selectFrom(review)
.leftJoin(review.storeId, store)
.on(review.storeId.id.eq(store.id))
.where(builder)
.orderBy(review.createdAt.desc())
.limit(limit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void reviewUpdate(ReviewRequestDto reviewRequestDto, String socialId, int
public List<ReviewMainResponseDto> getRecentReview(BigDecimal lat, BigDecimal lng, int limit) {
return reviewQueryRepository
.findRecentReviews(lat, lng, limit)
.parallelStream().map(review -> new ReviewMainResponseDto(review, review.getStoreId(), review.getUserId())).collect(Collectors.toList());
.stream().map(review -> new ReviewMainResponseDto(review, review.getStoreId(), review.getUserId())).collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,19 @@ public Page<Store> findBetweenLngLat(BigDecimal latStart,

List<Store> storeList = queryFactory.selectFrom(store)
.where(builder)
.orderBy()
.limit(pageable.getPageSize())
.offset(pageable.getOffset())
.fetch();
// 가까운순으로 정렬하기
if (latEnd != null && latStart != null && lngStart != null && lngEnd != null) {
BigDecimal nowLat = (latStart.add(latEnd)).divide(new BigDecimal("2"), 22, RoundingMode.HALF_UP);
BigDecimal nowLng = (lngStart.add(lngEnd)).divide(new BigDecimal("2"), 22, RoundingMode.HALF_UP);
storeList.sort((thisStore, newStore) -> {
BigDecimal thisDiff = nowLat.subtract(thisStore.getLatitude()).abs().add(nowLng.subtract(thisStore.getLongitude()).abs());
BigDecimal newDiff = nowLat.subtract(newStore.getLatitude()).abs().add(nowLng.subtract(newStore.getLongitude()).abs());
return thisDiff.compareTo(newDiff);
});
}
int fetchCount = queryFactory.selectFrom(store).where(builder).fetch().size();
return new PageImpl<>(storeList, pageable, fetchCount);
}
Expand All @@ -76,7 +85,7 @@ public Page<Store> findStoreOrderByPoint(BigDecimal lat,
BigDecimal lng,
String category,
List<String> facility,
Pageable pageable, User user) {
Pageable pageable) {

BooleanBuilder builder = locTwoPointAndConditions(lat, lng, category, facility);
// 직접 DTO를 조작
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/mpnp/baechelin/store/service/StoreService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.mpnp.baechelin.store.service;

import com.mpnp.baechelin.bookmark.domain.Bookmark;
import com.mpnp.baechelin.bookmark.repository.BookmarkRepository;
import com.mpnp.baechelin.review.domain.Review;
import com.mpnp.baechelin.review.dto.ReviewResponseDto;
import com.mpnp.baechelin.review.repository.ReviewRepository;
import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.store.dto.StoreCardResponseDto;
Expand Down Expand Up @@ -86,15 +84,15 @@ public StorePagedResponseDto getStoreInRange(BigDecimal latStart, BigDecimal lat
User targetUser = socialId == null ? null : userRepository.findBySocialId(socialId);
Page<Store> betweenLngLat = storeQueryRepository.findBetweenLngLat(latStart, latEnd, lngStart, lngEnd, category, facility, pageable);
// store 가져와서 dto 매핑
return getStoreCardPagedResponseDtos(targetUser, betweenLngLat);
return getStoreCardPagedResponseDto(targetUser, betweenLngLat);
}

// public List<StoreCardResponseDto> getStoreInRangeHighPoint(BigDecimal lat, BigDecimal lng, String
public StorePagedResponseDto getStoreInRangeHighPoint(BigDecimal lat, BigDecimal lng, String
category, List<String> facility, Pageable pageable, String socialId) {
User targetUser = socialId == null ? null : userRepository.findBySocialId(socialId);
Page<Store> resultList = storeQueryRepository.findStoreOrderByPoint(lat, lng, category, facility, pageable, targetUser);
return getStoreCardPagedResponseDtos(targetUser, resultList);
Page<Store> resultList = storeQueryRepository.findStoreOrderByPoint(lat, lng, category, facility, pageable);
return getStoreCardPagedResponseDto(targetUser, resultList);
}

public List<StoreCardResponseDto> getStoreInRangeHighBookmark(BigDecimal lat, BigDecimal lng, String
Expand All @@ -104,7 +102,7 @@ public List<StoreCardResponseDto> getStoreInRangeHighBookmark(BigDecimal lat, Bi
return getStoreCardResponseDtos(targetUser, highBookmarkResultList);
}

private StorePagedResponseDto getStoreCardPagedResponseDtos(User targetUser, Page<Store> resultStoreList) {
private StorePagedResponseDto getStoreCardPagedResponseDto(User targetUser, Page<Store> resultStoreList) {
List<StoreCardResponseDto> mappingResult = new ArrayList<>();
if (targetUser == null) {
for (Store store : resultStoreList) {
Expand Down

0 comments on commit 69b0787

Please sign in to comment.