public virtual IHttpActionResult PostContact(Guid id, [FromBody] ContactModelExtended model) { var customerContact = CustomerContact.CreateInstance(); CustomerMappings.CreateContact(customerContact, id, model); CreateUserIfNotExists(customerContact); model = customerContact.ConvertToContactModel(); return(CreatedAtRoute("GetContact", new { contactId = model.PrimaryKeyId }, model)); }
public static void CreateContact(CustomerContact customerContact, Guid userId, ContactModelExtended contactModel) { customerContact.PrimaryKeyId = new PrimaryKeyId(userId); customerContact.FirstName = contactModel.FirstName; customerContact.LastName = contactModel.LastName; customerContact.Email = contactModel.Email; customerContact.UserId = "String:" + contactModel .Email; // The UserId needs to be set in the format "String:{email}". Else a duplicate CustomerContact will be created later on. customerContact.RegistrationSource = contactModel.RegistrationSource; if (contactModel.Addresses != null) { foreach (var address in contactModel.Addresses) { customerContact.AddContactAddress( address.ConvertToCustomerAddress(CustomerAddress.CreateInstance())); } } UpdateMetaFields(customerContact, contactModel, new List <string> { "FirstName", "LastName", "Email", "RegistrationSource" }); // The contact, or more likely its related addresses, must be saved to the database before we can set the preferred // shipping and billing addresses. Using an address id before its saved will throw an exception because its value // will still be null. customerContact.SaveChanges(); // Once the contact has been saved we can look for any existing addresses. var defaultAddress = customerContact.ContactAddresses.FirstOrDefault(); if (defaultAddress != null) { // If an addresses was found, it will be used as default for shipping and billing. customerContact.PreferredShippingAddress = defaultAddress; customerContact.PreferredBillingAddress = defaultAddress; // Save the address preferences also. customerContact.SaveChanges(); } }
public virtual IHttpActionResult PutContact(Guid id, [FromBody] ContactModelExtended model) { var existingContact = CustomerContext.Current.GetContactById(id); if (existingContact == null) { return(NotFound()); } if (CreateUserIfNotExists(existingContact)) { existingContact.UserId = "String:" + model .Email; // The UserId needs to be set in the format "String:{email}". Else a duplicate CustomerContact will be created later on. } existingContact.FirstName = model.FirstName; existingContact.LastName = model.LastName; existingContact.Email = model.Email; existingContact.RegistrationSource = model.RegistrationSource; if (model.Addresses != null) { foreach (var address in model.Addresses) { CustomerMappings.CreateOrUpdateCustomerAddress(existingContact, address); } } existingContact.UpdateMetaFields(model, new List <string> { "FirstName", "LastName", "Email", "RegistrationSource" }); existingContact.SaveChanges(); return(StatusCode(HttpStatusCode.NoContent)); }