Exemplo n.º 1
0
        public async Task <IActionResult> CreateCustomer([FromBody] CustomerResource customerResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var customer = mapper.Map <CustomerResource, Customer>(customerResource);

            if (customerRepository.CustomerExists(customer))
            {
                return(BadRequest(Errors.AddErrorToModelState("login_failure", "Username already taken.", ModelState)));
            }

            customer.CustomerContact.CustomerTitle = staticDataRepository.GetTitle(customerResource.TitleId);
            customerRepository.Add(customer);

            await unitOfWork.CompleteAsync();

            customer = await customerRepository.GetCustomer(customer.Id);

            var result = mapper.Map <Customer, CustomerResource>(customer);

            return(Ok(result));
        }