Ejemplo n.º 1
0
        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 });
        }
Ejemplo n.º 2
0
        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);
        }