From 190f56a4cdcff03a5635d5b0ee616af14db4b146 Mon Sep 17 00:00:00 2001 From: Chris Park Date: Sat, 23 Dec 2023 17:43:46 +0900 Subject: [PATCH] feat: Add extra information on OpenSearch result --- .../domain/search/SearchServiceImpl.java | 33 ++++++++++++++++--- .../domain/search/dto/SearchResultDto.java | 9 +++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/depromeet/breadmapbackend/domain/search/SearchServiceImpl.java b/src/main/java/com/depromeet/breadmapbackend/domain/search/SearchServiceImpl.java index c30e4c1d..4311f279 100644 --- a/src/main/java/com/depromeet/breadmapbackend/domain/search/SearchServiceImpl.java +++ b/src/main/java/com/depromeet/breadmapbackend/domain/search/SearchServiceImpl.java @@ -1,6 +1,10 @@ package com.depromeet.breadmapbackend.domain.search; +import com.depromeet.breadmapbackend.domain.bakery.Bakery; import com.depromeet.breadmapbackend.domain.bakery.BakeryRepository; +import com.depromeet.breadmapbackend.domain.flag.FlagBakery; +import com.depromeet.breadmapbackend.domain.flag.FlagBakeryRepository; +import com.depromeet.breadmapbackend.domain.flag.FlagRepository; import com.depromeet.breadmapbackend.domain.review.Review; import com.depromeet.breadmapbackend.domain.review.ReviewQueryRepository; import com.depromeet.breadmapbackend.domain.review.ReviewService; @@ -36,6 +40,8 @@ public class SearchServiceImpl implements SearchService { private final UserRepository userRepository; private final SubwayStationRepository subwayStationRepository; private final ReviewQueryRepository reviewQueryRepository; + private final FlagBakeryRepository flagBakeryRepository; + private final FlagRepository flagRepository; private final ReviewService reviewService; private final SearchLogService searchLogService; @@ -60,7 +66,7 @@ public List searchDatabase(String oAuthId, String keyword, Double lat @Override public SearchResultResponse searchEngine(String oAuthId, String keyword, Double userLat, Double userLng, SearchType searchType) { - userRepository.findByOAuthId(oAuthId).orElseThrow(() -> new DaedongException(DaedongStatus.USER_NOT_FOUND)); + User user = userRepository.findByOAuthId(oAuthId).orElseThrow(() -> new DaedongException(DaedongStatus.USER_NOT_FOUND)); searchLogService.saveRecentSearchLog(oAuthId, keyword); SearchResultResponse.SearchResultResponseBuilder builder = SearchResultResponse.builder(); @@ -84,8 +90,10 @@ public SearchResultResponse searchEngine(String oAuthId, String keyword, Double searchResults.addAll(getSearchEngineDtoList(document.getHits(), userLat, userLng)); } - List bakeriesReviews = reviewQueryRepository.getBakeriesReview(SearchEngineDto.extractBakeryIdList(searchResults)); - List searchResultDtos = mergeSearchEngineAndReview(searchResults, bakeriesReviews); + List bakeryIds = SearchEngineDto.extractBakeryIdList(searchResults); + List bakeriesReviews = reviewQueryRepository.getBakeriesReview(bakeryIds); + + List searchResultDtos = this.mergeSearchEngineAndAdditionalInfo(searchResults, user.getId(), bakeriesReviews); resultSortBySearchType(searchType, searchResultDtos); @@ -94,12 +102,28 @@ public SearchResultResponse searchEngine(String oAuthId, String keyword, Double .build(); } - private static List mergeSearchEngineAndReview(List searchResults, List bakeriesReviews) { + private List mergeSearchEngineAndAdditionalInfo(List searchResults, Long userId, List bakeriesReviews) { List searchResultDtos = new ArrayList<>(); for (SearchEngineDto searchResultDto : searchResults) { SearchResultDto.SearchResultDtoBuilder searchResultDtoBuilder = SearchResultDto.builder(); + + Optional bakeryOptional = bakeryRepository.findById(searchResultDto.getBakeryId()); + if(bakeryOptional.isPresent()) { + searchResultDtoBuilder + .latitude(bakeryOptional.get().getLatitude()) + .longitude(bakeryOptional.get().getLongitude()) + .bakeryImageUrl(bakeryOptional.get().getImages()); + + searchResultDtoBuilder.flagCount(flagBakeryRepository.countFlagNum(bakeryOptional.get())); + + Optional flagBakeryOptional = flagBakeryRepository.findByBakeryAndUserId(bakeryOptional.get(), userId); + if(flagBakeryOptional.isPresent()) { + searchResultDtoBuilder.flagColor(flagBakeryOptional.get().getFlag().getColor().getCode()); + } + } + searchResultDtoBuilder .bakeryId(searchResultDto.getBakeryId()) .bakeryName(searchResultDto.getBakeryName()) @@ -107,6 +131,7 @@ private static List mergeSearchEngineAndReview(List bakeryImageUrl; + + private Integer flagCount; + private String flagColor; }