public IActionResult Post([FromBody] Models.Northwind.OrderDetail item)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

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

                this.OnOrderDetailCreated(item);
                this.context.OrderDetails.Add(item);
                this.context.SaveChanges();

                var keyOrderID   = item.OrderID;
                var keyProductID = item.ProductID;
                var itemToReturn = this.context.OrderDetails
                                   .Where(i => i.OrderID == keyOrderID && i.ProductID == keyProductID)
                                   .Include(i => i.Order)
                                   .Include(i => i.Product)
                                   .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));
            }
        }
示例#2
0
        public IActionResult Post([FromBody] Models.Northwind.OrderDetail item)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

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

                this.OnOrderDetailCreated(item);
                this.context.OrderDetails.Add(item);
                this.context.SaveChanges();

                var keyOrderID   = item.OrderID;
                var keyProductID = item.ProductID;

                var itemToReturn = this.context.OrderDetails.Where(i => i.OrderID == keyOrderID && i.ProductID == keyProductID);

                Request.QueryString = Request.QueryString.Add("$expand", "Order,Product");

                return(new ObjectResult(SingleResult.Create(itemToReturn))
                {
                    StatusCode = 201
                });
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return(BadRequest(ModelState));
            }
        }
 partial void OnOrderDetailUpdated(Models.Northwind.OrderDetail item);
示例#4
0
        public IActionResult PutOrderDetail([FromODataUri] int keyOrderID, [FromODataUri] int keyProductID, [FromBody] Models.Northwind.OrderDetail newItem)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (newItem == null || (newItem.OrderID != keyOrderID && newItem.ProductID != keyProductID))
                {
                    return(BadRequest());
                }

                this.OnOrderDetailUpdated(newItem);
                this.context.OrderDetails.Update(newItem);
                this.context.SaveChanges();

                var itemToReturn = this.context.OrderDetails.Where(i => i.OrderID == keyOrderID && i.ProductID == keyProductID);
                Request.QueryString = Request.QueryString.Add("$expand", "Order,Product");
                return(new ObjectResult(SingleResult.Create(itemToReturn)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return(BadRequest(ModelState));
            }
        }