コード例 #1
0
        public Product Create(Product product)
        {
            var dbProduct = Mapper.Map<DbProduct>(product);
            _db.Products.Add(dbProduct);
            _db.SaveChanges();

            return Mapper.Map<Product>(dbProduct);
        }
コード例 #2
0
 public IHttpActionResult Post(Product entity)
 {
     if (!ModelState.IsValid)
     {
         return BadRequest(ModelState);
     }
     var product = _repository.Create(entity) ;
     return Created(product);
 }
コード例 #3
0
 public IHttpActionResult Put([FromODataUri] long key, Product update)
 {
     if (!ModelState.IsValid)
     {
         return BadRequest(ModelState);
     }
     if (key != update.ID)
     {
         return BadRequest();
     }
     return Updated(_repository.Update(key, update, Request));
 }
コード例 #4
0
        public Product Update(long id, Product product, HttpRequestMessage request)
        {
            if (id != product.ID)
            {
                throw new HttpResponseException(request.CreateErrorResponse(
                    HttpStatusCode.BadRequest,
                    "Changing key property is not allowed for PUT method."));
            }

            var dbProduct = _db.Products.Find(id);
            if (dbProduct == null)
            {
                throw new HttpResponseException(request.CreateResponse(HttpStatusCode.NotFound));
            }

            dbProduct = Mapper.Map<DbProduct>(product);
            _db.Products.Attach(dbProduct);
            _db.Entry(dbProduct).State = EntityState.Modified;
            _db.SaveChanges();

            return product;
        }