-
Notifications
You must be signed in to change notification settings - Fork 416
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
Issue #1168 storefront only get product added to warehouse #1246
base: main
Are you sure you want to change the base?
Changes from all commits
a447659
dc7e2e9
1f15df4
17e26f0
b6be106
378f99e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,22 +40,26 @@ Page<Product> getProductsWithFilter(@Param("productName") String productName, | |
List<Product> findAllByIdIn(List<Long> productIds); | ||
|
||
@Query(value = "FROM Product p WHERE p.isFeatured = TRUE " | ||
+ "AND p.isVisibleIndividually = TRUE " | ||
+ "AND p.isPublished = TRUE ORDER BY p.id ASC ") | ||
Page<Product> getFeaturedProduct(Pageable pageable); | ||
+ "AND p.isVisibleIndividually = TRUE " | ||
+ "AND p.isPublished = TRUE " | ||
+ "AND p.id IN (:productIds) " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move |
||
+ "ORDER BY p.id ASC ") | ||
Page<Product> getFeaturedProductByProductIds(@Param("productIds") List<Long> productIds, Pageable pageable); | ||
|
||
@Query(value = "SELECT p FROM Product p LEFT JOIN p.productCategories pc LEFT JOIN pc.category c " | ||
@Query(value = "SELECT distinct p FROM Product p LEFT JOIN p.productCategories pc LEFT JOIN pc.category c " | ||
+ "WHERE LOWER(p.name) LIKE %:productName% " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move the indexed condition |
||
+ "AND p.id IN (:productIds) " | ||
+ "AND (c.slug = :categorySlug OR (:categorySlug IS NULL OR :categorySlug = '')) " | ||
+ "AND (:startPrice IS NULL OR p.price >= :startPrice) " | ||
+ "AND (:endPrice IS NULL OR p.price <= :endPrice) " | ||
+ "AND p.isVisibleIndividually = TRUE " | ||
+ "AND p.isPublished = TRUE " | ||
+ "AND p.isVisibleIndividually = true " | ||
+ "AND p.isPublished = true " | ||
+ "ORDER BY p.id ASC ") | ||
Page<Product> findByProductNameAndCategorySlugAndPriceBetween(@Param("productName") String productName, | ||
@Param("categorySlug") String categorySlug, | ||
@Param("startPrice") Double startPrice, | ||
@Param("endPrice") Double endPrice, | ||
@Param("productIds") List<Long> productIds, | ||
Pageable pageable); | ||
|
||
@Query(value = "SELECT p FROM Product p " | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.yas.product.service; | ||
|
||
import com.yas.commonlibrary.config.ServiceUrlConfig; | ||
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker; | ||
import io.github.resilience4j.retry.annotation.Retry; | ||
import java.net.URI; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestClient; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class InventoryService extends AbstractCircuitBreakFallbackHandler { | ||
private final RestClient restClient; | ||
private final ServiceUrlConfig serviceUrlConfig; | ||
|
||
@Retry(name = "restApi") | ||
@CircuitBreaker(name = "restCircuitBreaker", fallbackMethod = "handleInventoryFallback") | ||
public List<Long> getProductIdsAddedWarehouse() { | ||
final URI url = UriComponentsBuilder.fromHttpUrl(serviceUrlConfig.inventory()) | ||
.path("/storefront/stocks/products-in-warehouse").buildAndExpand().toUri(); | ||
return restClient.get() | ||
.uri(url) | ||
.retrieve() | ||
.body(new ParameterizedTypeReference<>() { | ||
}); | ||
} | ||
|
||
private List<Long> handleInventoryFallback(Throwable throwable) throws Throwable { | ||
return handleTypedFallback(throwable); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,7 @@ | |
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.apache.commons.collections4.ListUtils; | ||
|
@@ -80,6 +81,7 @@ | |
@Service | ||
@Transactional | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class ProductService { | ||
private static final String NONE_GROUP = "None group"; | ||
private final ProductRepository productRepository; | ||
|
@@ -92,28 +94,7 @@ | |
private final ProductOptionValueRepository productOptionValueRepository; | ||
private final ProductOptionCombinationRepository productOptionCombinationRepository; | ||
private final ProductRelatedRepository productRelatedRepository; | ||
|
||
public ProductService(ProductRepository productRepository, | ||
MediaService mediaService, | ||
BrandRepository brandRepository, | ||
ProductCategoryRepository productCategoryRepository, | ||
CategoryRepository categoryRepository, | ||
ProductImageRepository productImageRepository, | ||
ProductOptionRepository productOptionRepository, | ||
ProductOptionValueRepository productOptionValueRepository, | ||
ProductOptionCombinationRepository productOptionCombinationRepository, | ||
ProductRelatedRepository productRelatedRepository) { | ||
this.productRepository = productRepository; | ||
this.mediaService = mediaService; | ||
this.brandRepository = brandRepository; | ||
this.categoryRepository = categoryRepository; | ||
this.productCategoryRepository = productCategoryRepository; | ||
this.productImageRepository = productImageRepository; | ||
this.productOptionRepository = productOptionRepository; | ||
this.productOptionValueRepository = productOptionValueRepository; | ||
this.productOptionCombinationRepository = productOptionCombinationRepository; | ||
this.productRelatedRepository = productRelatedRepository; | ||
} | ||
private final InventoryService inventoryService; | ||
|
||
public ProductGetDetailVm createProduct(ProductPostVm productPostVm) { | ||
validateProductVm(productPostVm); | ||
|
@@ -769,9 +750,15 @@ | |
} | ||
|
||
public ProductFeatureGetVm getListFeaturedProducts(int pageNo, int pageSize) { | ||
Pageable pageable = PageRequest.of(pageNo, pageSize); | ||
|
||
List<ProductThumbnailGetVm> productThumbnailVms = new ArrayList<>(); | ||
Page<Product> productPage = productRepository.getFeaturedProduct(pageable); | ||
List<Long> productIds = inventoryService.getProductIdsAddedWarehouse(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current approach of fetching all available products in real-time should work. However, I don't like this approach so much as it impacts the responsibility if the number of products or requests is high. Please consider caching or an event-driven approach. |
||
if (productIds.isEmpty()) { | ||
return new ProductFeatureGetVm(productThumbnailVms, 0); | ||
} | ||
|
||
Pageable pageable = PageRequest.of(pageNo, pageSize); | ||
Page<Product> productPage = productRepository.getFeaturedProductByProductIds(productIds, pageable); | ||
List<Product> products = productPage.getContent(); | ||
for (Product product : products) { | ||
productThumbnailVms.add(new ProductThumbnailGetVm( | ||
|
@@ -875,11 +862,14 @@ | |
Double endPrice | ||
) { | ||
Pageable pageable = PageRequest.of(pageNo, pageSize); | ||
List<Long> productIds = inventoryService.getProductIdsAddedWarehouse(); | ||
if (productIds.isEmpty()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add the block {} for this if. |
||
return new ProductsGetVm(new ArrayList<>(), 0, 0, 0, 0, true); | ||
|
||
Page<Product> productPage; | ||
productPage = productRepository.findByProductNameAndCategorySlugAndPriceBetween( | ||
productName.trim().toLowerCase(), | ||
categorySlug.trim(), startPrice, endPrice, pageable); | ||
categorySlug.trim(), startPrice, endPrice, productIds, pageable); | ||
|
||
List<ProductThumbnailGetVm> productThumbnailVms = new ArrayList<>(); | ||
List<Product> products = productPage.getContent(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have cases where quantity = 0? Those records should be excluded, right?
findProductIdsInStock
may be a better name.