Skip to content

Commit 5681e61

Browse files
committed
fix : recommend logix
1 parent 6d9056d commit 5681e61

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

src/main/java/com/back/domain/cocktail/repository/CocktailRepository.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ Page<Cocktail> searchWithFilters(@Param("keyword") String keyword,
3636
@Param("types") List<CocktailType> types,
3737
@Param("bases") List<AlcoholBaseType> bases,
3838
Pageable pageable);
39-
//유사칵테일 추천관련
40-
List<Cocktail> findByAlcoholStrengthAndIdNot(AlcoholStrength strength, Long excludeId);
4139

42-
//유사칵테일 추천관련
43-
List<Cocktail> findByCocktailTypeAndIdNot(CocktailType type, Long excludeId);
44-
45-
//유사칵테일 추천관련
46-
List<Cocktail> findByAlcoholBaseTypeAndIdNot(AlcoholBaseType baseType, Long excludeId);
40+
List<Cocktail> findByAlcoholStrengthAndAlcoholBaseTypeAndIdNot(
41+
AlcoholStrength alcoholStrength,
42+
AlcoholBaseType alcoholBaseType,
43+
Long id
44+
);
4745
}

src/main/java/com/back/domain/cocktail/service/RecommendService.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import lombok.RequiredArgsConstructor;
77
import org.springframework.stereotype.Service;
88

9-
import java.util.ArrayList;
10-
import java.util.LinkedHashSet;
119
import java.util.List;
12-
import java.util.Set;
1310

1411
@Service
1512
@RequiredArgsConstructor
@@ -21,24 +18,20 @@ public List<CocktailRecommendResponseDto> recommendRelatedCocktails(Long cocktai
2118
Cocktail current = cocktailRepository.findById(cocktailId)
2219
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 칵테일입니다."));
2320

24-
// 3가지 조건으로 유사 칵테일 조회
25-
List<Cocktail> byAlcoholStrength = cocktailRepository.findByAlcoholStrengthAndIdNot(current.getAlcoholStrength(), current.getId());
26-
List<Cocktail> byCocktailType = cocktailRepository.findByCocktailTypeAndIdNot(current.getCocktailType(), current.getId());
27-
List<Cocktail> byAlcoholBase = cocktailRepository.findByAlcoholBaseTypeAndIdNot(current.getAlcoholBaseType(), current.getId());
21+
// 알콜 강도와 베이스 타이이 같은 칵테일만 조회
22+
List<Cocktail> related = cocktailRepository
23+
.findByAlcoholStrengthAndAlcoholBaseTypeAndIdNot(
24+
current.getAlcoholStrength(),
25+
current.getAlcoholBaseType(),
26+
current.getId()
27+
);
2828

29-
// 합치고 중복 제거
30-
Set<Cocktail> combined = new LinkedHashSet<>();
31-
combined.addAll(byAlcoholStrength);
32-
combined.addAll(byCocktailType);
33-
combined.addAll(byAlcoholBase);
34-
35-
List<Cocktail> combinedList = new ArrayList<>(combined);
36-
if (combinedList.size() > maxSize) {
37-
combinedList = combinedList.subList(0, maxSize);
29+
if (related.size() > maxSize) {
30+
related = related.subList(0, maxSize);
3831
}
3932

4033
// DTO로 변환
41-
return combinedList.stream()
34+
return related.stream()
4235
.map(c -> new CocktailRecommendResponseDto(
4336
c.getId(),
4437
c.getCocktailNameKo(),

0 commit comments

Comments
 (0)