Пример #1
0
        public IHttpActionResult PutCustomer(int id, CustomerViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != vm.Id)
            {
                return BadRequest();
            }

            var customer = db.Customers.Find(vm.Id);
            vm.UpdateDb(customer);
            db.Entry(customer).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Пример #2
0
        public IHttpActionResult PostCustomer(CustomerViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Customers.Add(vm.ToNewDb());
            db.SaveChanges();

            return CreatedAtRoute("GetCustomer", new { id = vm.Id }, vm);
        }