public async Task <ValidationResult> ValidateUpdateBasketAsync(UpdateBasketInput input) { ValidationResult validationResult = new(); if (string.IsNullOrWhiteSpace(input.CustomerEmail)) { validationResult.Messages.Add(new(nameof(UpdateBasketInput.CustomerEmail), $"Debe ingresar un cliente.")); } else { Customer customer = await _customerRepository.GetCustomer(input.CustomerEmail); if (customer is null) { validationResult.Messages.Add(new(nameof(UpdateBasketInput.CustomerEmail), $"El cliente '{input.CustomerEmail}' no existe.")); } } foreach (ProductDefinitive productInput in input.Products) { Product product = await _productRepository.GetAsync(productInput.ProductId); if (product is null) { validationResult.Messages.Add(new(nameof(UpdateBasketInput.Products), "El producto no existe.")); } else { if (productInput.Quantity <= 0) { validationResult.Messages.Add(new(nameof(UpdateBasketInput.Products), $"La cantidad debe ser mayor a cero para el producto {product.Name}.")); } if (!product.IsActive) { validationResult.Messages.Add(new(nameof(UpdateBasketInput.Products), $"El producto {product.Name} debe estar activo.")); } IEnumerable <ProductVariant> variants = await _productVariantRepository.GetByProduct(product); foreach (ProductVariantPair variantInput in productInput.Variants) { if (!variants.Any(x => x.Name == variantInput.Name && x.Values.Contains(variantInput.Value))) { validationResult.Messages.Add(new(nameof(UpdateBasketInput.Products), $"La variante '{variantInput.Name}' con valor '{variantInput.Value}' del producto '{product.Name}' no existe.")); } } } } return(validationResult); }
public async Task <OperationResult <BasketDto> > UpdateBasketAsync(UpdateBasketInput input) { var validatioResult = await _validator.ValidateUpdateBasketAsync(input); if (validatioResult.IsSuccess) { var basket = await _repository.GetByCustomerAsync(input.CustomerEmail); if (basket is null) { basket = await CreateEmplyBasket(input.CustomerEmail); } basket.Products = input.Products; await _repository.UpdateAsync(basket); return(OperationResult <BasketDto> .Success(basket.ConvertToDto())); } return(OperationResult <BasketDto> .Fail(validatioResult)); }
public async Task <IActionResult> UpdateBasketAsync(UpdateBasketInput input) { var result = await _service.UpdateBasketAsync(input); return(new OperationActionResult(result)); }