Пример #1
0
        public async Task <ValidationResult> ValidateDeleteAddressAsync(DeleteAddressInput input)
        {
            ValidationResult validationResult = new();

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

                if (customer is null)
                {
                    validationResult.Messages.Add(new(nameof(DeleteAddressInput.CustomerEmail), "No existe el cliente."));
                }

                AddressCustomer addressCustomer = await _addressRepository.GetByCustomerAsync(input.CustomerEmail);

                if (addressCustomer is null || addressCustomer.Addresses is null || !addressCustomer.Addresses.Any(x => x.Id == input.AddressId))
                {
                    validationResult.Messages.Add(new(nameof(DeleteAddressInput.AddressId), "La dirección no existe."));
                }
            }

            return(validationResult);
        }
Пример #2
0
        public async Task <OperationResult> DeleteAddressAsync(DeleteAddressInput input)
        {
            var validatioResult = await _addressValidator.ValidateDeleteAddressAsync(input);

            if (validatioResult.IsSuccess)
            {
                var addresses = await _addressRepository.GetByCustomerAsync(input.CustomerEmail);

                addresses.Addresses = addresses.Addresses.Where(x => x.Id != input.AddressId);

                await _addressRepository.UpdateAsync(addresses);

                return(OperationResult.Success());
            }

            return(OperationResult.Fail());
        }