Exemplo n.º 1
0
        public IHttpActionResult PostPhone(PhoneViewModel newPhone)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            Phone phone = Mapper.Map<PhoneViewModel, Phone>(newPhone);
            var person = db.People.FirstOrDefault(p => p.Id == phone.PersonId);
            phone.Person = person;
            db.Phones.Add(phone);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = phone.Id }, new { data = phone });
        }
Exemplo n.º 2
0
        public IHttpActionResult PutPhone(int id, PhoneViewModel existingPhone)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != existingPhone.Id)
            {
                return BadRequest();
            }
            Phone phone = Mapper.Map<PhoneViewModel, Phone>(existingPhone);
            var person = db.People.FirstOrDefault(p => p.Id == phone.PersonId);
            phone.Person = person;
            db.Entry(phone).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PhoneExists(id))
                {
                    return NotFound();
                }
                return BadRequest();
            }

            return StatusCode(HttpStatusCode.NoContent);
        }