Skip to content

Commit

Permalink
#23 [Update&Fix] Dto로 필터링 된 리스트 정렬 및 리스트 가져오기
Browse files Browse the repository at this point in the history
페이징 오류 수정 예정
  • Loading branch information
JSoi committed Jul 14, 2022
1 parent 26659f6 commit 8594f50
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -31,34 +32,34 @@ public class StoreController {

@ApiOperation(value = "조건에 맞는 업장 목록을 반환하는 메소드")
@GetMapping("/near")
public List<StoreResponseDto> 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<String> facility,
@PageableDefault Pageable pageable) {
List<Store> betweenLngLat = storeQueryRepository.findBetweenLngLat(latStart, latEnd, lngStart, lngEnd, category, facility, pageable);
return betweenLngLat.parallelStream().map(storeService::storeToResDto).collect(Collectors.toList());// 순서보장
public List<StoreCardResponseDto> 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<String> 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<StoreResponseDto> getStoreInRangeHighPoint(@RequestParam(required = false) BigDecimal lat,
@RequestParam(required = false) BigDecimal lng,
@RequestParam(required = false) String category,
@RequestParam(required = false) List<String> facility,
@PageableDefault Pageable pageable) {
List<Store> betweenLngLat = storeQueryRepository.findStoreOrderByPoint(lat, lng, category, facility, pageable);
return betweenLngLat.parallelStream().map(storeService::storeToResDto).collect(Collectors.toList());// 순서보장
public List<StoreCardResponseDto> getStoreInRangeHighPoint(@RequestParam(required = false) BigDecimal lat,
@RequestParam(required = false) BigDecimal lng,
@RequestParam(required = false) String category,
@RequestParam(required = false) List<String> facility,
@PageableDefault Pageable pageable,
@AuthenticationPrincipal User user) {
return storeService.getStoreInRangeHighPoint(lat, lng, category, facility, pageable, user==null?null:user.getUsername());
}

@GetMapping("/bookmark")
public List<StoreResponseDto> getStoreInRangeHighBookmark(@RequestParam(required = false) BigDecimal lat,
@RequestParam(required = false) BigDecimal lng,
@RequestParam(required = false) String category,
@RequestParam(required = false) List<String> facility,
@RequestParam int limit) {
List<Store> betweenLngLat = storeQueryRepository.findStoreOrderByBookmark(lat, lng, category, facility, limit);
return betweenLngLat.parallelStream().map(storeService::storeToResDto).collect(Collectors.toList());// 순서보장
public List<StoreCardResponseDto> getStoreInRangeHighBookmark(@RequestParam(required = false) BigDecimal lat,
@RequestParam(required = false) BigDecimal lng,
@RequestParam(required = false) String category,
@RequestParam(required = false) List<String> facility,
@RequestParam int limit,
@AuthenticationPrincipal User user) {
return storeService.getStoreInRangeHighBookmark(lat,lng,category,facility,limit,user==null?null:user.getUsername());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,7 +17,7 @@
@Setter
@Builder
@Slf4j
public class StoreCardDto implements Comparable<StoreCardDto> {
public class StoreCardResponseDto implements Comparable<StoreCardResponseDto> {
private int storeId;
private String category;
private String name;
Expand All @@ -38,7 +37,7 @@ public class StoreCardDto implements Comparable<StoreCardDto> {
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) {
Expand All @@ -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();
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -64,24 +66,31 @@ public List<Store> findBetweenLngLat(BigDecimal latStart,
}

//TODO 별점순 - 쿼리 결과로 산출된 리스트의 평균 구하기, 정렬, 페이징
public List<Store> findStoreOrderByPoint(BigDecimal lat,
BigDecimal lng,
String category,
List<String> facility,
Pageable pageable) {

public List<StoreCardResponseDto> findStoreOrderByPoint(BigDecimal lat,
BigDecimal lng,
String category,
List<String> facility,
Pageable pageable, User user) {

BooleanBuilder builder = locTwoPointAndConditions(lat, lng, category, facility);

List<Store> resultList = queryFactory.selectFrom(store)
.where(builder)
.fetch();

List<StoreResponseDto> resultAvgList = resultList.stream().sorted()
.map(StoreResponseDto::new).collect(Collectors.toList());
List<StoreCardResponseDto> 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)
Expand Down Expand Up @@ -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);
}

}
40 changes: 38 additions & 2 deletions src/main/java/com/mpnp/baechelin/store/service/StoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<StoreResponseDto> getStoreList() {
List<Store> storeList = storeRepository.findAll();
Expand All @@ -30,7 +41,7 @@ public List<StoreResponseDto> getStoreList() {
return storeResponseList;
}

public StoreResponseDto storeToResDto(Store store){
public StoreResponseDto storeToResDto(Store store) {
List<Review> reviewList = reviewRepository.findAllByStoreId(store);

double totalPoint = 0;
Expand Down Expand Up @@ -63,4 +74,29 @@ public StoreResponseDto storeToResDto(Store store){
.pointAvg(pointAvg)
.build();
}
}

public List<StoreCardResponseDto> getStoreInRange(BigDecimal latStart, BigDecimal latEnd, BigDecimal lngStart, BigDecimal lngEnd, String category, List<String> facility, Pageable pageable, String socialId) {
User targetUser = userRepository.findBySocialId(socialId);
List<Store> 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<StoreCardResponseDto> getStoreInRangeHighPoint(BigDecimal lat, BigDecimal lng, String category, List<String> facility, Pageable pageable, String socialId) {
User targetUser = userRepository.findBySocialId(socialId);
return storeQueryRepository.findStoreOrderByPoint(lat, lng, category, facility, pageable, targetUser);
}

public List<StoreCardResponseDto> getStoreInRangeHighBookmark(BigDecimal lat, BigDecimal lng, String category, List<String> facility, int limit, String socialId) {
User targetUser = userRepository.findBySocialId(socialId);
List<Store> 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());// 순서보장
}
}
10 changes: 6 additions & 4 deletions src/main/java/com/mpnp/baechelin/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -54,13 +56,13 @@ public class User extends TimeStamped {

// 연관관계 매핑
@OneToMany(mappedBy = "userId", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Folder> folderList;
private List<Folder> folderList = new ArrayList<>();

@OneToMany(mappedBy = "userId", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Bookmark> bookmarkList;
private List<Bookmark> bookmarkList = new ArrayList<>();

@OneToMany(mappedBy = "userId", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Review> reviewList;
private List<Review> reviewList = new ArrayList<>();

@Builder
public User(String socialId,
Expand Down

0 comments on commit 8594f50

Please sign in to comment.