Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add extra information on OpenSearch result #388

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -60,7 +66,7 @@ public List<SearchDto> 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();
Expand All @@ -84,8 +90,10 @@ public SearchResultResponse searchEngine(String oAuthId, String keyword, Double
searchResults.addAll(getSearchEngineDtoList(document.getHits(), userLat, userLng));
}

List<BakeryReviewScoreDto> bakeriesReviews = reviewQueryRepository.getBakeriesReview(SearchEngineDto.extractBakeryIdList(searchResults));
List<SearchResultDto> searchResultDtos = mergeSearchEngineAndReview(searchResults, bakeriesReviews);
List<Long> bakeryIds = SearchEngineDto.extractBakeryIdList(searchResults);
List<BakeryReviewScoreDto> bakeriesReviews = reviewQueryRepository.getBakeriesReview(bakeryIds);

List<SearchResultDto> searchResultDtos = this.mergeSearchEngineAndAdditionalInfo(searchResults, user.getId(), bakeriesReviews);

resultSortBySearchType(searchType, searchResultDtos);

Expand All @@ -94,19 +102,36 @@ public SearchResultResponse searchEngine(String oAuthId, String keyword, Double
.build();
}

private static List<SearchResultDto> mergeSearchEngineAndReview(List<SearchEngineDto> searchResults, List<BakeryReviewScoreDto> bakeriesReviews) {
private List<SearchResultDto> mergeSearchEngineAndAdditionalInfo(List<SearchEngineDto> searchResults, Long userId, List<BakeryReviewScoreDto> bakeriesReviews) {

List<SearchResultDto> searchResultDtos = new ArrayList<>();

for (SearchEngineDto searchResultDto : searchResults) {
SearchResultDto.SearchResultDtoBuilder searchResultDtoBuilder = SearchResultDto.builder();

Optional<Bakery> 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<FlagBakery> flagBakeryOptional = flagBakeryRepository.findByBakeryAndUserId(bakeryOptional.get(), userId);
if(flagBakeryOptional.isPresent()) {
searchResultDtoBuilder.flagColor(flagBakeryOptional.get().getFlag().getColor().getCode());
}
}

searchResultDtoBuilder
.bakeryId(searchResultDto.getBakeryId())
.bakeryName(searchResultDto.getBakeryName())
.address(searchResultDto.getAddress())
.distance(searchResultDto.getDistance())
.reviewNum(0L) // init
.totalScore(0d); // init

if (searchResultDto.getBreadId() != null) {
searchResultDtoBuilder
.breadId(searchResultDto.getBreadId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.*;

import java.util.List;

@Getter
@Builder
@AllArgsConstructor
Expand All @@ -15,4 +17,11 @@ public class SearchResultDto {
private Double distance;
private Long reviewNum;
private Double totalScore;

private Double latitude;
private Double longitude;
private List<String> bakeryImageUrl;

private Integer flagCount;
private String flagColor;
}
Loading