public ActionResult CreateCustomer([FromBody] ServiceCustomers.Customer customer) { _logger.LogInformation($"Creating customer {customer.GivenName} {customer.Surname}"); var customerResponse = _customersService.CreateCustomer(customer); return(Ok(customerResponse)); }
public ServiceResponse <ServiceCustomers.Customer> CreateCustomer(ServiceCustomers.Customer customerToAdd) { var now = DateTime.Now; var response = new ServiceResponse <ServiceCustomers.Customer>() { Time = now, Data = customerToAdd }; try { var entityCustomerToAdd = CustomerMapper.SerialiseCustomer(customerToAdd); entityCustomerToAdd.CreatedOn = now; entityCustomerToAdd.UpdatedOn = now; entityCustomerToAdd.PrimaryAddress.CreatedOn = now; entityCustomerToAdd.PrimaryAddress.UpdatedOn = now; _customersWriter.AddCustomerToDb(entityCustomerToAdd); response.Data = CustomerMapper.SerialiseCustomer(entityCustomerToAdd); response.IsSuccessful = true; response.Message = $"Successfully added {customerToAdd.GivenName} {customerToAdd.Surname}"; } catch (Exception e) { response.IsSuccessful = false; response.Message = $"Failed to add {customerToAdd.GivenName} {customerToAdd.Surname}. Stack trace: {e.StackTrace}"; } return(response); }
// in goes an entity model and out comes a service model public static ServiceCustomers.Customer SerialiseCustomer(EntityCustomers.Customer entityCustomer) { var serviceCustomer = new ServiceCustomers.Customer() { Id = entityCustomer.Id, GivenName = entityCustomer.GivenName, Surname = entityCustomer.Surname, PrimaryAddress = SerialiseCustomerAddress(entityCustomer.PrimaryAddress), CreatedOn = entityCustomer.CreatedOn, UpdatedOn = entityCustomer.UpdatedOn }; return(serviceCustomer); }