Beispiel #1
0
        public IHttpActionResult PostProduct(ProductViewModel newProduct)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Product product = Mapper.Map<ProductViewModel, Product>(newProduct);

            db.Products.Add(product);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = product.Id }, new { data = product });
        }
Beispiel #2
0
        public IHttpActionResult PutProduct(int id, ProductViewModel newProduct)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

            Product product = Mapper.Map<ProductViewModel, Product>(newProduct);
            db.Entry(product).State = EntityState.Modified;

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

            return StatusCode(HttpStatusCode.NoContent);
        }