Beispiel #1
0
        // PUT api/User/5
        public async Task <IHttpActionResult> PutUser(int id, User user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != user.Id)
            {
                return(BadRequest());
            }

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

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Beispiel #2
0
        // POST api/cart
        public async Task <IHttpActionResult> POST(AddGamesToCartDTO games)
        {
            if (ModelState.IsValid)
            {
                ICollection <string> tempgames = games.Games.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                ICollection <Game>   dbgames   = new Collection <Game>();;


                string userEmail  = HttpContext.Current.User.Identity.Name;
                int    customerid = IdFromEmail(userEmail);

                //User has unprocessed cart
                if (has_oldcart(customerid))
                {
                    //New Cart
                    Cart newcart = new Cart();
                    newcart.isProcessed = false;
                    newcart.CustomerId  = customerid;

                    //Add Game
                    foreach (string tempgame in tempgames)
                    {
                        if (GameById(Convert.ToInt32(tempgame)) != null)
                        {
                            dbgames.Add(GameById(Convert.ToInt32(tempgame)));
                        }
                    }
                    if (dbgames.Count > 0)
                    {
                        newcart.Games = dbgames;
                        db.Carts.Add(newcart);
                        await db.SaveChangesAsync();

                        return(Ok());
                    }
                    else
                    {
                        return(BadRequest());
                    }
                }
                else
                {
                    Cart oldcart = get_unprocessed(customerid);
                    foreach (string tempgame in tempgames)
                    {
                        if (GameById(Convert.ToInt32(tempgame)) != null && !GameExists(Convert.ToInt32(tempgame), customerid))
                        {
                            dbgames.Add(GameById(Convert.ToInt32(tempgame)));
                        }
                    }
                    if (dbgames.Count > 0)
                    {
                        oldcart.Games = dbgames;

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

                        await db.SaveChangesAsync();

                        return(Ok());
                    }
                    else
                    {
                        return(BadRequest("Malformed input/Game exists in cart."));
                    }
                }
            }
            return(BadRequest());
        }