public async Task HandleAsync(UpdateProductCommand command) { var product = await _productRepository.GetAsync(command.Id, true); product.NullCheck(ErrorCodes.product_not_found, command.Id); if (product.IsDeleted) { throw new NotFoundException(ErrorCodes.product_is_deleted); } var newProduct = new Product(command.Id, command.Name, command.Description, command.Vendor, command.Price, command.Quantity); await _productRepository.UpdateAsync(newProduct); var carts = await _cartsRepository.GetAllWithProducts(command.Id); foreach (var cart in carts) { cart.UpdateProduct(newProduct); } await _cartsRepository.UpdateManyAsync(carts); }
public async Task HandleAsync(DeleteProductCommand command) { var product = await _productsRepository.GetAsync(command.Id, true); product.NullCheck(ErrorCodes.product_not_found, command.Id); product.Delete(); await _productsRepository.UpdateAsync(product); var carts = await _cartsRepository.GetAllWithProducts(command.Id); foreach (var cart in carts) { cart.DeleteProduct(command.Id); } await _cartsRepository.UpdateManyAsync(carts); }