public async Task <IActionResult> GetCategoryProductsByDocumentId(Guid id) { var key = $"{KEY_CACHE}-{id}"; var categoryProducts = await _categoryProductRedisRepository.Get <CategoryProduct>(key, (int)RedisBase.Product, true); if (categoryProducts != null && categoryProducts.Products.Any()) { return(Ok(categoryProducts)); } else { categoryProducts = await _categoryProductRepository.GetCategoryProductsByDocumentId(id); _categoryProductRedisRepository.Set(key, categoryProducts, (int)RedisBase.Product); } return(Ok(categoryProducts)); }
public async Task Handle(ProductUpdateEvent @event) { var productEvent = @event.ProductEvent; var key = $"{KEY_CACHE}-{productEvent.CategoryId}"; var categoryProduct = await _categoryProductRepository.GetCategoryProductsByDocumentId(productEvent.CategoryId); if (categoryProduct != null) { var productBase = categoryProduct?.Products?.FirstOrDefault(x => x.Id == productEvent.Id); if (productBase != null) { categoryProduct.Products.Remove(productBase); } var product = new Models.Product(); product.Update(productEvent.Id, productEvent.Name, productEvent.Description, productEvent.UnityPrice, productEvent.QuantityInStock, productEvent.Images, productEvent.SubCategory, productEvent.Status, productEvent.UpdatedAt); categoryProduct.Products.Add(product); _categoryProductRepository.Update(categoryProduct); await _uow.Commit(); _categoryProductRedisRepository.Remove(key, (int)RedisBase.Product); _categoryProductRedisRepository.Set(key, categoryProduct, (int)RedisBase.Product); } }