diff --git a/src/main/java/com/mpnp/baechelin/store/controller/StoreController.java b/src/main/java/com/mpnp/baechelin/store/controller/StoreController.java index 26d4c1a..6ff7438 100644 --- a/src/main/java/com/mpnp/baechelin/store/controller/StoreController.java +++ b/src/main/java/com/mpnp/baechelin/store/controller/StoreController.java @@ -1,16 +1,17 @@ package com.mpnp.baechelin.store.controller; import com.mpnp.baechelin.store.domain.Store; +import com.mpnp.baechelin.store.dto.StoreCardResponseDto; import com.mpnp.baechelin.store.dto.StoreResponseDto; import com.mpnp.baechelin.store.repository.StoreQueryRepository; import com.mpnp.baechelin.store.service.StoreService; import io.swagger.annotations.Api; -import io.swagger.annotations.ApiImplicitParam; -import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.security.core.userdetails.User; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -31,34 +32,34 @@ public class StoreController { @ApiOperation(value = "조건에 맞는 업장 목록을 반환하는 메소드") @GetMapping("/near") - public List getStoreInRange(@RequestParam(required = false) BigDecimal latStart, - @RequestParam(required = false) BigDecimal latEnd, - @RequestParam(required = false) BigDecimal lngStart, - @RequestParam(required = false) BigDecimal lngEnd, - @RequestParam(required = false) String category, - @RequestParam(required = false) List facility, - @PageableDefault Pageable pageable) { - List betweenLngLat = storeQueryRepository.findBetweenLngLat(latStart, latEnd, lngStart, lngEnd, category, facility, pageable); - return betweenLngLat.parallelStream().map(storeService::storeToResDto).collect(Collectors.toList());// 순서보장 + public List getStoreInRange(@RequestParam(required = false) BigDecimal latStart, + @RequestParam(required = false) BigDecimal latEnd, + @RequestParam(required = false) BigDecimal lngStart, + @RequestParam(required = false) BigDecimal lngEnd, + @RequestParam(required = false) String category, + @RequestParam(required = false) List facility, + @PageableDefault Pageable pageable, + @AuthenticationPrincipal User user) { + return storeService.getStoreInRange(latStart, latEnd, lngStart, lngEnd, category, facility, pageable, user==null?null:user.getUsername()); } @GetMapping("/point") - public List getStoreInRangeHighPoint(@RequestParam(required = false) BigDecimal lat, - @RequestParam(required = false) BigDecimal lng, - @RequestParam(required = false) String category, - @RequestParam(required = false) List facility, - @PageableDefault Pageable pageable) { - List betweenLngLat = storeQueryRepository.findStoreOrderByPoint(lat, lng, category, facility, pageable); - return betweenLngLat.parallelStream().map(storeService::storeToResDto).collect(Collectors.toList());// 순서보장 + public List getStoreInRangeHighPoint(@RequestParam(required = false) BigDecimal lat, + @RequestParam(required = false) BigDecimal lng, + @RequestParam(required = false) String category, + @RequestParam(required = false) List facility, + @PageableDefault Pageable pageable, + @AuthenticationPrincipal User user) { + return storeService.getStoreInRangeHighPoint(lat, lng, category, facility, pageable, user==null?null:user.getUsername()); } @GetMapping("/bookmark") - public List getStoreInRangeHighBookmark(@RequestParam(required = false) BigDecimal lat, - @RequestParam(required = false) BigDecimal lng, - @RequestParam(required = false) String category, - @RequestParam(required = false) List facility, - @RequestParam int limit) { - List betweenLngLat = storeQueryRepository.findStoreOrderByBookmark(lat, lng, category, facility, limit); - return betweenLngLat.parallelStream().map(storeService::storeToResDto).collect(Collectors.toList());// 순서보장 + public List getStoreInRangeHighBookmark(@RequestParam(required = false) BigDecimal lat, + @RequestParam(required = false) BigDecimal lng, + @RequestParam(required = false) String category, + @RequestParam(required = false) List facility, + @RequestParam int limit, + @AuthenticationPrincipal User user) { + return storeService.getStoreInRangeHighBookmark(lat,lng,category,facility,limit,user==null?null:user.getUsername()); } } diff --git a/src/main/java/com/mpnp/baechelin/store/dto/StoreCardDto.java b/src/main/java/com/mpnp/baechelin/store/dto/StoreCardResponseDto.java similarity index 79% rename from src/main/java/com/mpnp/baechelin/store/dto/StoreCardDto.java rename to src/main/java/com/mpnp/baechelin/store/dto/StoreCardResponseDto.java index 2328675..547e2e2 100644 --- a/src/main/java/com/mpnp/baechelin/store/dto/StoreCardDto.java +++ b/src/main/java/com/mpnp/baechelin/store/dto/StoreCardResponseDto.java @@ -1,13 +1,12 @@ package com.mpnp.baechelin.store.dto; import com.mpnp.baechelin.review.domain.Review; -import com.mpnp.baechelin.review.dto.ReviewImageResponseDto; -import com.mpnp.baechelin.review.dto.ReviewResponseDto; import com.mpnp.baechelin.store.domain.Store; import com.mpnp.baechelin.user.domain.User; import lombok.*; import lombok.extern.slf4j.Slf4j; +import javax.transaction.Transactional; import java.math.BigDecimal; import java.util.List; import java.util.stream.Collectors; @@ -18,7 +17,7 @@ @Setter @Builder @Slf4j -public class StoreCardDto implements Comparable { +public class StoreCardResponseDto implements Comparable { private int storeId; private String category; private String name; @@ -38,7 +37,7 @@ public class StoreCardDto implements Comparable { private double pointAvg = 0.0; @Override - public int compareTo(StoreCardDto sad) { + public int compareTo(StoreCardResponseDto sad) { if (this.pointAvg > sad.pointAvg) { return 1; } else if (this.pointAvg < sad.pointAvg) { @@ -48,7 +47,7 @@ public int compareTo(StoreCardDto sad) { } - public StoreCardDto(Store store, User user) { + public StoreCardResponseDto(Store store, boolean bookmark) { this.storeId = store.getId(); this.category = store.getCategory(); this.name = store.getName(); @@ -61,9 +60,10 @@ public StoreCardDto(Store store, User user) { this.phoneNumber = store.getPhoneNumber(); this.heightDifferent = store.getHeightDifferent(); this.approach = store.getApproach(); - this.storeImgList = store.getStoreImageList().parallelStream().map(StoreImgResponseDto::new).collect(Collectors.toList()); - this.pointAvg =Double.parseDouble(String.format(store.getReviewList().stream() + this.storeImgList = store.getStoreImageList().parallelStream() + .map(StoreImgResponseDto::new).collect(Collectors.toList()); + this.pointAvg = Double.parseDouble(String.format(store.getReviewList().stream() .collect(Collectors.averagingDouble(Review::getPoint)).toString(), 0.1f)); - this.bookmark = true; + this.bookmark = bookmark; } } diff --git a/src/main/java/com/mpnp/baechelin/store/repository/StoreQueryRepository.java b/src/main/java/com/mpnp/baechelin/store/repository/StoreQueryRepository.java index 7846453..6e7890a 100644 --- a/src/main/java/com/mpnp/baechelin/store/repository/StoreQueryRepository.java +++ b/src/main/java/com/mpnp/baechelin/store/repository/StoreQueryRepository.java @@ -1,7 +1,9 @@ package com.mpnp.baechelin.store.repository; +import com.mpnp.baechelin.review.domain.Review; import com.mpnp.baechelin.store.domain.Store; -import com.mpnp.baechelin.store.dto.StoreResponseDto; +import com.mpnp.baechelin.store.dto.StoreCardResponseDto; +import com.mpnp.baechelin.user.domain.User; import com.querydsl.core.BooleanBuilder; import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.core.types.dsl.StringPath; @@ -64,12 +66,11 @@ public List findBetweenLngLat(BigDecimal latStart, } //TODO 별점순 - 쿼리 결과로 산출된 리스트의 평균 구하기, 정렬, 페이징 - public List findStoreOrderByPoint(BigDecimal lat, - BigDecimal lng, - String category, - List facility, - Pageable pageable) { - + public List findStoreOrderByPoint(BigDecimal lat, + BigDecimal lng, + String category, + List facility, + Pageable pageable, User user) { BooleanBuilder builder = locTwoPointAndConditions(lat, lng, category, facility); @@ -77,11 +78,19 @@ public List findStoreOrderByPoint(BigDecimal lat, .where(builder) .fetch(); - List resultAvgList = resultList.stream().sorted() - .map(StoreResponseDto::new).collect(Collectors.toList()); + List resultAvgList = resultList.stream() + .map(store -> { + long count = user == null ? 0L : user.getBookmarkList().stream() + .filter(b -> b.getUserId() == user && b.getStoreId() == store).count(); + double avg = Double.parseDouble(String.format(String.valueOf(store.getReviewList().stream() + .collect(Collectors.averagingDouble(Review::getPoint))), "0.1f")); + StoreCardResponseDto storeCardResponseDto = new StoreCardResponseDto(store, count > 0); + storeCardResponseDto.setPointAvg(avg); + return storeCardResponseDto; + }).sorted().collect(Collectors.toList()); getStorePaged(resultAvgList, pageable); - + return resultAvgList; // return queryFactory.selectFrom(store) // .where(builder) @@ -169,7 +178,7 @@ private void getStorePaged(List storeResultList, Pageable pageable) { start = Math.max(start, pageStartIndex); end = Math.min(end, pageStartIndex + pageable.getPageSize() - 1); - storeResultList = storeResultList.subList(start, end); + storeResultList = storeResultList.subList(start, end); } } diff --git a/src/main/java/com/mpnp/baechelin/store/service/StoreService.java b/src/main/java/com/mpnp/baechelin/store/service/StoreService.java index df04e51..9d5861c 100644 --- a/src/main/java/com/mpnp/baechelin/store/service/StoreService.java +++ b/src/main/java/com/mpnp/baechelin/store/service/StoreService.java @@ -3,20 +3,31 @@ import com.mpnp.baechelin.review.domain.Review; import com.mpnp.baechelin.review.repository.ReviewRepository; import com.mpnp.baechelin.store.domain.Store; +import com.mpnp.baechelin.store.dto.StoreCardResponseDto; import com.mpnp.baechelin.store.dto.StoreResponseDto; +import com.mpnp.baechelin.store.repository.StoreQueryRepository; import com.mpnp.baechelin.store.repository.StoreRepository; +import com.mpnp.baechelin.user.domain.User; +import com.mpnp.baechelin.user.repository.UserRepository; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import javax.transaction.Transactional; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; @Service +@Transactional @RequiredArgsConstructor public class StoreService { private final StoreRepository storeRepository; private final ReviewRepository reviewRepository; + private final StoreQueryRepository storeQueryRepository; + private final UserRepository userRepository; public List getStoreList() { List storeList = storeRepository.findAll(); @@ -30,7 +41,7 @@ public List getStoreList() { return storeResponseList; } - public StoreResponseDto storeToResDto(Store store){ + public StoreResponseDto storeToResDto(Store store) { List reviewList = reviewRepository.findAllByStoreId(store); double totalPoint = 0; @@ -63,4 +74,29 @@ public StoreResponseDto storeToResDto(Store store){ .pointAvg(pointAvg) .build(); } -} + + public List getStoreInRange(BigDecimal latStart, BigDecimal latEnd, BigDecimal lngStart, BigDecimal lngEnd, String category, List facility, Pageable pageable, String socialId) { + User targetUser = userRepository.findBySocialId(socialId); + List betweenLngLat = storeQueryRepository.findBetweenLngLat(latStart, latEnd, lngStart, lngEnd, category, facility, pageable); + return betweenLngLat.parallelStream().map(store -> { + long count = targetUser.getBookmarkList().stream() + .filter(b -> b.getUserId() == targetUser && b.getStoreId() == store).count(); + return new StoreCardResponseDto(store, count > 0); + }).collect(Collectors.toList());// 순서보장 + } + + public List getStoreInRangeHighPoint(BigDecimal lat, BigDecimal lng, String category, List facility, Pageable pageable, String socialId) { + User targetUser = userRepository.findBySocialId(socialId); + return storeQueryRepository.findStoreOrderByPoint(lat, lng, category, facility, pageable, targetUser); + } + + public List getStoreInRangeHighBookmark(BigDecimal lat, BigDecimal lng, String category, List facility, int limit, String socialId) { + User targetUser = userRepository.findBySocialId(socialId); + List betweenLngLat = storeQueryRepository.findStoreOrderByBookmark(lat, lng, category, facility, limit); + return betweenLngLat.parallelStream().map(store -> { + long count = targetUser == null ? 0 : targetUser.getBookmarkList().stream() + .filter(b -> b.getUserId() == targetUser && b.getStoreId() == store).count(); + return new StoreCardResponseDto(store, count > 0); + }).collect(Collectors.toList());// 순서보장 + } +} \ No newline at end of file diff --git a/src/main/java/com/mpnp/baechelin/user/domain/User.java b/src/main/java/com/mpnp/baechelin/user/domain/User.java index 9a64e71..1f54a10 100644 --- a/src/main/java/com/mpnp/baechelin/user/domain/User.java +++ b/src/main/java/com/mpnp/baechelin/user/domain/User.java @@ -13,10 +13,12 @@ import lombok.Setter; import javax.persistence.*; +import java.util.ArrayList; import java.util.List; @Entity -@Getter @Setter +@Getter +@Setter @NoArgsConstructor public class User extends TimeStamped { @@ -54,13 +56,13 @@ public class User extends TimeStamped { // 연관관계 매핑 @OneToMany(mappedBy = "userId", cascade = CascadeType.ALL, orphanRemoval = true) - private List folderList; + private List folderList = new ArrayList<>(); @OneToMany(mappedBy = "userId", cascade = CascadeType.ALL, orphanRemoval = true) - private List bookmarkList; + private List bookmarkList = new ArrayList<>(); @OneToMany(mappedBy = "userId", cascade = CascadeType.ALL, orphanRemoval = true) - private List reviewList; + private List reviewList = new ArrayList<>(); @Builder public User(String socialId,