public IActionResult CreateCustomer(CustomerCreateCmd command)
        {
            try
            {
                _logger.LogInformation("creando un nuevo customer");

                var newCustomer = _customerService.Create(command);
                return(Created($"https://localhost:8001/customers/{command.Id}", newCustomer));
            }
            catch (GenericValidationException ex)
            {
                _logger.LogError(ex.Message);
                _logger.LogError(ex.StackTrace);
                return(StatusCode(412, ex.ValidationErrors));
            }
            catch (DuplicatedPrimaryKeyException ex)
            {
                _logger.LogError(ex.Message);
                _logger.LogError(ex.StackTrace);
                return(StatusCode(412));
            }
            catch (Exception ex)
            {
                return(BadRequest());
            }
        }
        public Customer Create(CustomerCreateCmd command)
        {
            var newCustomer = CustomerMapper.FromCustomerCreateCmd(command);

            _customerList.Add(newCustomer);

            return(newCustomer);
        }
Пример #3
0
        public static Customer FromCustomerCreateCmd(CustomerCreateCmd command)
        {
            Customer newCustomer = new Customer();

            newCustomer.Id        = command.Id;
            newCustomer.FirstName = command.FirstName;
            newCustomer.LastName  = command.LastName;

            return(newCustomer);
        }
Пример #4
0
        public Customer Create(CustomerCreateCmd command)
        {
            // 1. validar
            var customer = _customerRepository.Find(command.Id);

            var isCustomerRepeated = customer != null;

            var validor = new CustomerCreateCmdValidator(isCustomerRepeated);

            var validationResult = validor.Validate(command);

            // 2. si falla alguna validacion, retornar los errores respectivos

            if (!validationResult.IsValid)
            {
                // retornamos los mensajes de error
                List <Validation> errorList = new List <Validation>();

                foreach (var error in validationResult.Errors)
                {
                    errorList.Add(new Validation {
                        Name = error.PropertyName, Message = error.ErrorMessage
                    });
                }

                throw new GenericValidationException(errorList);
            }



            // codigo taller pasado


            if (customer != null)
            {
                throw new DuplicatedPrimaryKeyException();
            }

            // 3. en caso contrario, guardar
            return(_customerRepository.Create(command));
        }