示例#1
0
        // POST api/<controller>
        public HttpResponseMessage Post(Order order)
        {
            if (ModelState.IsValid)
            {
                Order newOrder = new Order
                {
                    BaseTeaId = order.BaseTea.Id,
                    FlavorId  = order.Flavor.Id,
                    SizeId    = order.Size.Id,
                    Toppings  = new List <Topping>(),
                };

                foreach (var topping in order.Toppings)
                {
                    newOrder.Toppings.Add(db.Toppings.FirstOrDefault(x => x.Id == topping.Id));
                }

                db.Entry(newOrder).State = EntityState.Added;
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, order);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = order.Id }));
                return(response);
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
示例#2
0
        public async Task <ActionResult <int> > UpdateUsers(Users u)
        {
            db.Entry(u).State = EntityState.Modified;
            int result = await db.SaveChangesAsync();

            db.Entry(u).State = EntityState.Deleted;
            return(result);
        }
示例#3
0
        // PUT api/<controller>/5
        public HttpResponseMessage Put(int id, Topping topping)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            if (id != topping.Id)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            db.Entry(topping).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }