@@ -37,37 +37,41 @@ public Cocktail getCocktailById(Long id) {
3737 .orElseThrow (() -> new IllegalArgumentException ("Cocktail not found. id=" + id ));
3838 }
3939
40- // 칵테일 무한스크롤 조회
4140 @ Transactional (readOnly = true )
42- public List <CocktailSummaryResponseDto > getCocktails (Long lastId , Integer size ) { // 무한스크롤 조회, 클라이언트 쪽에서 lastId와 size 정보를 받음.(스크롤 이벤트)
41+ public List <CocktailSummaryResponseDto > getCocktails (Long lastValue , Long lastId , Integer size , String sortBy ) {
4342 int fetchSize = (size != null ) ? size : DEFAULT_SIZE ;
44-
43+ Pageable pageable = PageRequest . of ( 0 , fetchSize );
4544 List <Cocktail > cocktails ;
46- if (lastId == null ) {
47- // 첫 요청 → 최신 데이터부터
48- cocktails = cocktailRepository .findAllByOrderByIdDesc (PageRequest .of (0 , fetchSize ));
49- } else {
50- // 무한스크롤 → 마지막 ID보다 작은 데이터 조회
51- cocktails = cocktailRepository .findByIdLessThanOrderByIdDesc (lastId , PageRequest .of (0 , fetchSize ));
45+
46+ switch (sortBy != null ? sortBy .toLowerCase () : "" ) {
47+ case "keeps" :
48+ cocktails = (lastValue == null )
49+ ? cocktailRepository .findAllOrderByKeepCountDesc (pageable )
50+ : cocktailRepository .findByKeepCountLessThanOrderByKeepCountDesc (lastValue , lastId , pageable );
51+ break ;
52+ case "comments" :
53+ cocktails = (lastValue == null )
54+ ? cocktailRepository .findAllOrderByCommentsCountDesc (pageable )
55+ : cocktailRepository .findByCommentsCountLessThanOrderByCommentsCountDesc (lastValue , lastId , pageable );
56+ break ;
57+ default :
58+ cocktails = (lastValue == null )
59+ ? cocktailRepository .findAllByOrderByIdDesc (pageable )
60+ : cocktailRepository .findByIdLessThanOrderByIdDesc (lastValue , pageable );
61+ break ;
5262 }
63+
5364 return cocktails .stream ()
54- .map (c -> new CocktailSummaryResponseDto (c .getId (), c .getCocktailName (), c .getCocktailNameKo (), c .getCocktailImgUrl (), c .getAlcoholStrength ().getDescription ()))
65+ .map (c -> new CocktailSummaryResponseDto (
66+ c .getId (),
67+ c .getCocktailName (),
68+ c .getCocktailNameKo (),
69+ c .getCocktailImgUrl (),
70+ c .getAlcoholStrength ().getDescription ()
71+ ))
5572 .collect (Collectors .toList ());
5673 }
5774
58- // 칵테일 검색기능
59- @ Transactional (readOnly = true )
60- public List <Cocktail > cocktailSearch (String keyword ) {
61- // cockTailName, ingredient이 하나만 있을 수도 있고 둘 다 있을 수도 있음
62- if (keyword == null || keyword .trim ().isEmpty ()) {
63- // 아무 검색어 없으면 전체 반환 처리
64- return cocktailRepository .findAll ();
65- } else {
66- // 이름 또는 재료 둘 중 하나라도 매칭되면 결과 반환
67- return cocktailRepository .findByCocktailNameContainingIgnoreCaseOrIngredientContainingIgnoreCase (keyword , keyword );
68- }
69- }
70-
7175 // 칵테일 검색,필터기능
7276 @ Transactional (readOnly = true )
7377 public List <CocktailSearchResponseDto > searchAndFilter (CocktailSearchRequestDto cocktailSearchRequestDto ) {
0 commit comments