Skip to content

Commit

Permalink
fix: sorting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Kang1221 committed Sep 13, 2024
1 parent 7d5c63f commit 289f249
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ public interface ProductRepository extends JpaRepository<Product, String> {
@Query(value = "SELECT * FROM products p WHERE p.stock > 0 AND p.name LIKE %:keyword% ", nativeQuery = true)
List<Product> findAllByName(@Param("keyword") String keyword);

@Query("SELECT p FROM Product p WHERE p.id IN :productIds ORDER BY FIELD(p.id, :productIds)")
List<Product> findByIdIn(List<String> productIds);
}
16 changes: 14 additions & 2 deletions src/main/java/co/orange/ddanzi/service/SearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
Expand All @@ -34,8 +37,7 @@ public ApiResponse<?> searchPage(String devicetoken) {
User user = authUtils.getUser();
List<String> topSearchedList = List.of("멀티비타민", "망고", "핸드크림");
log.info("Searching page for devicetoken: {}", devicetoken);
List<String> recentViewedProductIds = redisRepository.getRecentProducts(devicetoken);
List<Product> productList = productRepository.findByIdIn(recentViewedProductIds);
List<Product> productList = getSortedRecentViewedProducts(devicetoken);
List<ProductInfo> productInfoList = homeService.setProductList(user, productList, interestProductRepository);
return ApiResponse.onSuccess(Success.GET_SEARCH_SCREEN_SUCCESS, SearchPageResponseDto.builder()
.topSearchedList(topSearchedList)
Expand All @@ -55,4 +57,14 @@ public ApiResponse<?> searchKeyword(String keyword) {
.searchedProductList(productInfoList)
.build());
}

public List<Product> getSortedRecentViewedProducts(String devicetoken) {
List<String> recentViewedProductIds = redisRepository.getRecentProducts(devicetoken);
List<Product> productList = productRepository.findByIdIn(recentViewedProductIds);
Map<String, Product> productMap = productList.stream()
.collect(Collectors.toMap(Product::getId, Function.identity()));
return recentViewedProductIds.stream()
.map(productMap::get)
.collect(Collectors.toList());
}
}

0 comments on commit 289f249

Please sign in to comment.