public IActionResult InsertOrder(string values)
        {
            var newOrder = new Order();

            JsonConvert.PopulateObject(values, newOrder);

            if (!TryValidateModel(newOrder))
            {
                return(BadRequest(ModelState.GetFullErrorMessage()));
            }

            _nwind.Orders.Add(newOrder);
            _nwind.SaveChanges();

            return(Ok(newOrder));
        }
Esempio n. 2
0
        public HttpResponseMessage InsertOrder(FormDataCollection form)
        {
            var values = form.Get("values");

            var newOrder = new Order();

            JsonConvert.PopulateObject(values, newOrder);

            Validate(newOrder);
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState.GetFullErrorMessage()));
            }

            _nwind.Orders.Add(newOrder);
            _nwind.SaveChanges();

            return(Request.CreateResponse(HttpStatusCode.Created, newOrder));
        }
Esempio n. 3
0
        public HttpResponseMessage Batch(List <DataChange> changes)
        {
            foreach (var change in changes)
            {
                Order order;

                if (change.Type == "update" || change.Type == "remove")
                {
                    var key = Convert.ToInt32(change.Key);
                    order = _nwind.Orders.First(o => o.OrderID == key);
                }
                else
                {
                    order = new Order();
                }

                if (change.Type == "insert" || change.Type == "update")
                {
                    JsonConvert.PopulateObject(change.Data.ToString(), order);

                    Validate(order);
                    if (!ModelState.IsValid)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState.GetFullErrorMessage()));
                    }

                    if (change.Type == "insert")
                    {
                        _nwind.Orders.Add(order);
                    }
                    change.Data = order;
                }
                else if (change.Type == "remove")
                {
                    _nwind.Orders.Remove(order);
                }
            }

            _nwind.SaveChanges();

            return(Request.CreateResponse(HttpStatusCode.OK, changes));
        }
Esempio n. 4
0
        public object Batch([FromBody] List <DataChange> changes)
        {
            foreach (var change in changes)
            {
                Order order;

                if (change.Type == "update" || change.Type == "remove")
                {
                    var key = Convert.ToInt32(change.Key);
                    order = _nwind.Orders.First(o => o.OrderID == key);
                }
                else
                {
                    order = new Order();
                }

                if (change.Type == "insert" || change.Type == "update")
                {
                    JsonConvert.PopulateObject(change.Data.ToString(), order);

                    if (!TryValidateModel(order))
                    {
                        return(BadRequest(ModelState.GetFullErrorMessage()));
                    }

                    if (change.Type == "insert")
                    {
                        _nwind.Orders.Add(order);
                    }
                    change.Data = order;
                }
                else if (change.Type == "remove")
                {
                    _nwind.Orders.Remove(order);
                }
            }

            _nwind.SaveChanges();

            return(Ok(changes));
        }