public async Task <IHttpActionResult> AddPhoneNumber(AddNewPhone phone) { if (ModelState.IsValid) { t_CRM_Phones NewPhone = new t_CRM_Phones() { ContactId = phone.ContactId, PhoneNumber = phone.Number, CreatedDate = DateTime.Now }; db.t_CRM_Phones.Add(NewPhone); try { await db.SaveChangesAsync(); return(Ok(NewPhone)); } catch (DbUpdateException ex) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex))); } } else { return(BadRequest(ModelState)); } }
public async Task <IHttpActionResult> CreateContact(NewContact newContact) { if (ModelState.IsValid) { t_CRM_Contacts contact = new t_CRM_Contacts() { FistName = newContact.FirstName, LastName = newContact.LastName, UserUid = newContact.UserUid, CreatedDate = DateTime.Now }; db.t_CRM_Contacts.Add(contact); try { await db.SaveChangesAsync(); List <t_CRM_Phones> PhoneList = new List <t_CRM_Phones>(); foreach (NewPhoneNumber c in newContact.Phones) { t_CRM_Phones phone = new t_CRM_Phones() { PhoneNumber = c.Number, ContactId = contact.ContactId, CreatedDate = contact.CreatedDate }; PhoneList.Add(phone); } db.t_CRM_Phones.AddRange(PhoneList); await db.SaveChangesAsync(); Contact DTOContact = new Contact() { ContactId = contact.ContactId, FirstName = contact.FistName, LastName = contact.LastName, Phones = db.t_CRM_Phones .Where(x => x.ContactId == contact.ContactId) .Select(y => new PhoneNumber { PhoneId = y.PhoneId, ContactId = y.ContactId, Number = y.PhoneNumber, CreatedDate = y.CreatedDate, }).ToList(), CreatedDate = contact.CreatedDate, UserUid = contact.UserUid }; return(Ok(DTOContact)); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex))); } catch (DbUpdateException ex) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex))); } } else { return(BadRequest(ModelState)); } }