Exemple #1
0
        public IActionResult PutProduct(int key, [FromBody] Models.Northwind.Product newItem)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (newItem == null || (newItem.ProductID != key))
                {
                    return(BadRequest());
                }

                this.OnProductUpdated(newItem);
                this.context.Products.Update(newItem);
                this.context.SaveChanges();

                var itemToReturn = this.context.Products.Where(i => i.ProductID == key);
                Request.QueryString = Request.QueryString.Add("$expand", "Supplier,Category");
                return(new ObjectResult(SingleResult.Create(itemToReturn)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return(BadRequest(ModelState));
            }
        }
        public IActionResult Post([FromBody] Models.Northwind.Product item)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (item == null)
                {
                    return(BadRequest());
                }

                this.OnProductCreated(item);
                this.context.Products.Add(item);
                this.context.SaveChanges();

                var key          = item.ProductID;
                var itemToReturn = this.context.Products
                                   .Where(i => i.ProductID == key)
                                   .Include(i => i.Supplier)
                                   .Include(i => i.Category)
                                   .FirstOrDefault();

                return(new JsonResult(itemToReturn, new Newtonsoft.Json.JsonSerializerSettings
                {
                    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
                    DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc
                })
                {
                    StatusCode = 201
                });
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return(BadRequest(ModelState));
            }
        }
 partial void OnProductUpdated(Models.Northwind.Product item);
 partial void OnProductDeleted(Models.Northwind.Product item);