public IHttpActionResult PostContact(ContactHelper contact) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var newContact = new Contact() { FirstName = contact.contact.FirstName, LastName = contact.contact.LastName, Title = contact.contact.Title, Email = contact.contact.Email, Phone = contact.contact.Phone, TwitterHandle = contact.contact.TwitterHandle, Notes = contact.contact.Notes }; var existingCompany = db.Companies.Find(contact.contact.Company); if (existingCompany != null) { newContact.Company = existingCompany; } else { return BadRequest("You must select a company."); } var existingLead = db.Leads.Find(contact.contact.Lead); if (existingLead != null && newContact.Company.Leads.Any(x => x.Id == existingLead.Id)) { newContact.Company.Leads.Add(existingLead); } db.Contacts.Add(newContact); db.SaveChanges(); contact.contact.Id = newContact.Id; return Ok(new { contact = contact.contact }); }
public IHttpActionResult PutContact(int id, Contact contact) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != contact.Id) { return BadRequest(); } db.Entry(contact).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ContactExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }