diff --git a/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java b/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java index c5c1f60..e2ff257 100644 --- a/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java +++ b/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java @@ -136,62 +136,48 @@ public List search(String title, Map contentMap = contentRepository.findAllById(ids).stream() .collect(Collectors.toMap(Content::getContentId, c -> c)); - Map avgScoreMap = new HashMap<>(); - List avgRows = contentReviewRepository.findAvgScoreByContentIdIn(ids); - for (AvgScoreProjection row : avgRows) { - Double v = row.getAvgScore(); - avgScoreMap.put(row.getContentId(),v); - } + Map avgScoreMap = contentReviewRepository + .findAvgScoreByContentIdIn(ids).stream() + .collect(Collectors.toMap( + AvgScoreProjection::getContentId, + p -> Optional.ofNullable(p.getAvgScore()).orElse(0.0) + )); - Set wishedSet; - if (userId != null && !userId.isBlank()) { - wishedSet = contentWishRepository - .findWishedContentIdsByUserIdAndContentIds(Integer.parseInt(userId), ids); - } else { - wishedSet = Collections.emptySet(); - } + Set wishedSet = (userId != null && !userId.isBlank()) + ? contentWishRepository.findWishedContentIdsByUserIdAndContentIds(Integer.parseInt(userId), ids) + : Set.of(); - Map> hashtagMap = new HashMap<>(); - List hashtags = hashtagRepository.findByContentIdIn(ids); - for (Hashtag h : hashtags) { - int cid = h.getContentId(); - List list = hashtagMap.get(cid); - if (list == null) { - list = new ArrayList<>(); - hashtagMap.put(cid, list); - } - list.add(h.getContent()); - } + Map> hashtagMap = hashtagRepository.findByContentIdIn(ids).stream() + .collect(Collectors.groupingBy( + Hashtag::getContentId, + Collectors.mapping(Hashtag::getContent, Collectors.toList()) + )); - Map restDateMap = new HashMap<>(); - List sightRows = sightsInformationRepository.findRestDateByContentIdIn(ids); - for (RestDateProjection r : sightRows) { - String rd = r.getRestDate(); - if (rd != null) { - restDateMap.put(r.getContentId(), rd); - } - } - List restRows = restaurantInformationRepository.findRestDateByContentIdIn(ids); - for (RestDateProjection r : restRows) { - String rd = r.getRestDate(); - if (rd != null) { - restDateMap.putIfAbsent(r.getContentId(), rd); - } - } + Map restDateMap = sightsInformationRepository + .findRestDateByContentIdIn(ids).stream() + .collect(Collectors.toMap( + RestDateProjection::getContentId, + RestDateProjection::getRestDate, + (a, b) -> a + )); + restaurantInformationRepository.findRestDateByContentIdIn(ids) + .forEach(r -> restDateMap.putIfAbsent(r.getContentId(), r.getRestDate())); + + Map totalViewMap = viewTotalRepository + .findTotalViewByContentIdIn(ids).stream() + .collect(Collectors.toMap( + ViewTotalProjection::getContentId, + v -> Optional.ofNullable(v.getTotalView()).orElse(0) + )); - Map totalViewMap = new HashMap<>(); - List viewRows = viewTotalRepository.findTotalViewByContentIdIn(ids); - for (ViewTotalProjection v : viewRows) { - int tv = v.getTotalView(); - totalViewMap.put(v.getContentId(), tv); - } + Map wishCntMap = contentWishRepository + .countByContentIdIn(ids).stream() + .collect(Collectors.toMap( + WishCountProjection::getContentId, + w -> w.getWishCount() + )); - Map wishCntMap = new HashMap<>(); - List wishCntRows = contentWishRepository.countByContentIdIn(ids); - for (WishCountProjection w : wishCntRows) { - int cnt = w.getWishCount(); - wishCntMap.put(w.getContentId(), cnt); - } + Map regionCache = new HashMap<>(); return ids.stream() .map(contentMap::get) @@ -200,16 +186,19 @@ public List search(String title, .map(content -> { int id = content.getContentId(); - double avg = avgScoreMap.getOrDefault(id, 0.0); + double avg = Optional.ofNullable(avgScoreMap.get(id)).orElse(0.0); boolean wishData = wishedSet.contains(id); - List hashtag = hashtagMap.getOrDefault(id, List.of()); + List hashtags = hashtagMap.getOrDefault(id, List.of()); String restDate = restDateMap.get(id); - int totalView = totalViewMap.getOrDefault(id, 0); - int wishCnt = wishCntMap.getOrDefault(id, 0); + int totalView = Optional.ofNullable(totalViewMap.get(id)).orElse(0); + int wishCnt = Optional.ofNullable(wishCntMap.get(id)).orElse(0); - RegionCodeResponse regionName = getRegionName(content.getSidoCode(), content.getSigunguCode()); + String key = content.getSidoCode() + "-" + content.getSigunguCode(); + RegionCodeResponse regionName = regionCache.computeIfAbsent(key, k -> + getRegionName(content.getSidoCode(), content.getSigunguCode()) + ); - return ContentResponse.from(content, avg, wishData, regionName, hashtag, restDate, totalView, wishCnt); + return ContentResponse.from(content, avg, wishData, regionName, hashtags, restDate, totalView, wishCnt); }) .toList(); }