public IHttpActionResult PostEmail(EmailViewModel newEmail) { if (!ModelState.IsValid) { return BadRequest(ModelState); } Email email = Mapper.Map<EmailViewModel, Email>(newEmail); var person = db.People.FirstOrDefault(p => p.Id == email.PersonId); email.Person = person; db.Emails.Add(email); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = email.Id }, new { data = email }); }
public IHttpActionResult PutEmail(int id, EmailViewModel existingEmail) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != existingEmail.Id) { return BadRequest(); } Email email = Mapper.Map<EmailViewModel, Email>(existingEmail); var person = db.People.FirstOrDefault(p => p.Id == email.PersonId); email.Person = person; db.Entry(email).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!EmailExists(id)) { return NotFound(); } return BadRequest(); } return StatusCode(HttpStatusCode.NoContent); }