Exemplo n.º 1
0
        public async Task <ValidationResult> ValidateRemoveBasketProductAsync(RemoveBasketProductInput input)
        {
            ValidationResult validationResult = new();

            if (string.IsNullOrWhiteSpace(input.CustomerEmail))
            {
                validationResult.Messages.Add(new(nameof(AddBasketProductInput.CustomerEmail), $"Debe ingresar un cliente."));
            }
            else
            {
                Customer customer = await _customerRepository.GetCustomer(input.CustomerEmail);

                if (customer is null)
                {
                    validationResult.Messages.Add(new(nameof(AddBasketProductInput.CustomerEmail), $"El usuario '{input.CustomerEmail}' no existe."));
                }
            }

            Basket basket = await _basketRepository.GetByCustomerAsync(input.CustomerEmail);

            if (basket is null)
            {
                validationResult.Messages.Add(new(nameof(RemoveBasketProductInput.CustomerEmail), $"El carrito no existe."));
            }
            else if (!basket.Products.Any(x => x.ProductId == input.ProductId))
            {
                validationResult.Messages.Add(new(nameof(RemoveBasketProductInput.CustomerEmail), $"El producto no esta en el carrito."));
            }

            return(validationResult);
        }
Exemplo n.º 2
0
        public async Task <OperationResult <BasketDto> > RemoveBasketProductAsync(RemoveBasketProductInput input)
        {
            var validatioResult = await _validator.ValidateRemoveBasketProductAsync(input);

            if (validatioResult.IsSuccess)
            {
                var basket = await _repository.GetByCustomerAsync(input.CustomerEmail);

                basket.Products = basket.Products.Where(x => x.ProductId != input.ProductId);

                await _repository.UpdateAsync(basket);

                return(OperationResult <BasketDto> .Success(basket.ConvertToDto()));
            }

            return(OperationResult <BasketDto> .Fail(validatioResult));
        }