public async Task<CategoryProduct> Update(CategoryProduct product) { if (_categoryproductRepository.Search(b => b.Id == product.Id && b.Id != product.Id).Result.Any()) return null; await _categoryproductRepository.Update(product); return product; }
public async Task Handle(CategoryUpdateEvent @event) { try { var categoryEvent = @event.CategoryEvent; var categoryProduct = new CategoryProduct(); categoryProduct.Update(categoryEvent.Id, categoryEvent.Name, categoryEvent.Image, categoryEvent.Status); var result = await _categoryProductRepository.GetCategoryProductsByDocumentId(categoryEvent.Id); if (result != null) { _categoryProductRepository.Update(categoryProduct); await _unitOfWork.Commit(); var categoryProducts = await _categoryProductRepository.GetAll(); var categories = categoryProducts.Where(x => x.Status == "A") .GroupBy(g => g.Name) .Select(s => new CategoryResponse { Id = s.FirstOrDefault().Id, Name = s.FirstOrDefault().Name, Image = s.FirstOrDefault().Image }).ToList(); _categoryRedisRepository.Remove(KEY_CACHE, (int)RedisBase.Category); _categoryRedisRepository.Set(KEY_CACHE, categories, (int)RedisBase.Category); } } catch (Exception ex) { } }
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); } }