Ejemplo n.º 1
0
        public bool UpdateCart(CartEdit model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var cartUpdateEntity = ctx.Carts.Include(e => e.GearInCart).Single(e => e.CartId == model.CartId);

                cartUpdateEntity.CartId = model.CartId;
                cartUpdateEntity.UserId = model.UserId;

                var gearToHaveInCart     = new List <IndividualGear>();
                var gearToRemoveFromCart = new List <IndividualGear>();

                foreach (var gear in model.GearInCart)
                {
                    var gearEntity      = ctx.Gear.Find(gear.GearId);
                    var gearToCompareTo = cartUpdateEntity.GearInCart.Single(e => e.GearId == gear.GearId);

                    if (gearToCompareTo.AmountOfGearInCart > gear.AmountOfGearInCart)
                    {
                        gearEntity.NumAvailable += gearToCompareTo.AmountOfGearInCart - gear.AmountOfGearInCart;
                    }
                    else
                    {
                        if (gear.AmountOfGearInCart - gearToCompareTo.AmountOfGearInCart > gearEntity.NumAvailable)
                        {
                            return(false);
                        }

                        gearEntity.NumAvailable -= gear.AmountOfGearInCart - gearToCompareTo.AmountOfGearInCart;
                    }

                    if (gear.AmountOfGearInCart <= 0)
                    {
                        gearToRemoveFromCart.Add(gear);
                    }
                    else
                    {
                        gearToHaveInCart.Add(gear);
                    }
                }

                foreach (var gear in gearToRemoveFromCart)
                {
                    cartUpdateEntity.GearInCart.Remove(gear);
                    //ctx.GearInCarts.Remove(gear);
                }

                cartUpdateEntity.GearInCart = gearToHaveInCart;

                return(ctx.SaveChanges() >= 1);
            }
        }
Ejemplo n.º 2
0
        public ActionResult Edit(int id)
        {
            var service = CreateCartService();
            var detail  = service.GetCartById(id);
            var model   =
                new CartEdit
            {
                PlantId = detail.PlantId,
                Plant   = detail.Plants,
            };

            return(View(model));
        }
Ejemplo n.º 3
0
        public bool UpdateCart(CartEdit model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var entity =
                    ctx
                    .Carts
                    .Single(e => e.CartId == model.CartId && e.OwnerId == _userId);

                entity.PlantId = model.PlantId;
                return(ctx.SaveChanges() == 1);
            }
        }
Ejemplo n.º 4
0
        // GET: Cart/Edit/
        public ActionResult Edit()
        {
            var service = CreateCartService();
            var detail  = service.ViewCart();
            var model   = new CartEdit()
            {
                CartId     = detail.CartId,
                GearInCart = detail.GearInCart,
                UserId     = detail.UserId
            };

            return(View(model));
        }
Ejemplo n.º 5
0
        public bool UpdateCart(CartEdit model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var entity =
                    ctx
                    .Cart
                    .Single(e => e.CartId == model.CartId && e.OwnerId == _userId);

                entity.Quantity  = model.Quantity;
                entity.ItemTotal = model.Price * Convert.ToDecimal(model.Quantity);
                return(ctx.SaveChanges() == 1);
            }
        }
Ejemplo n.º 6
0
        public IHttpActionResult Put(CartEdit cart)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            CartService cartService = CreateCartService();

            if (!cartService.UpdateCart(cart))
            {
                return(InternalServerError());
            }
            return(Ok());
        }
Ejemplo n.º 7
0
        public ActionResult Edit(int id)
        {
            var service = CreateCartService();
            var detail  = service.GetCartById(id);
            var model   =
                new CartEdit
            {
                CartId    = detail.CartId,
                ProductId = detail.ProductId,
            };
            var productService = CreateProductService();

            ViewBag.ProductId = new SelectList(productService.GetProducts(), "ProductId", "Title");
            return(View(model));
        }
Ejemplo n.º 8
0
        public bool UpdateCart(CartEdit model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var entity = ctx.Carts.Single(e => e.CartId == model.CartId && e.CartUser == _cartId);

                if (model.ItemCount != null)
                {
                    entity.ItemCount = model.ItemCount;
                }
                if (model.GiftBoxId != null)
                {
                    entity.GiftBoxId = model.GiftBoxId;
                }
                return(ctx.SaveChanges() == 1);
            }
        }
Ejemplo n.º 9
0
        public ActionResult Edit(int id)
        {
            var service = CreateCartService();
            var detail  = service.GetCartById(id);
            var model   =
                new CartEdit
            {
                CartId    = detail.CartId,
                OwnerId   = detail.OwnerId,
                BookId    = detail.BookId,
                Title     = detail.Title,
                Quantity  = detail.Quantity,
                Price     = detail.Price,
                ItemTotal = detail.ItemTotal,
            };

            return(View(model));
        }
Ejemplo n.º 10
0
        public ActionResult Edit(CartEdit model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var service = CreateCartService();

            if (service.UpdateCart(model))
            {
                TempData["SaveResult"] = "Cart Updated";
                return(RedirectToAction("Index"));
            }

            ModelState.AddModelError("", "You cannot add that much gear to your cart");
            return(View(model));
        }
        public bool UpdateCart(CartEdit model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var entity =
                    ctx
                    .Cart
                    .Single(e => e.CartId == model.CartId && e.OwnerId == _userId);

                entity.CartId    = model.CartId;
                entity.ProductId = model.ProductId;



                entity.ModifiedUtc = DateTimeOffset.UtcNow;

                return(ctx.SaveChanges() == 1);
            }
        }
Ejemplo n.º 12
0
        public ActionResult Edit(int id, CartEdit model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (model.CartId != id)
            {
                ModelState.AddModelError("", "Id Mismatch");
                return(View(model));
            }
            var service = CreateCartService();

            if (service.UpdateCart(model))
            {
                TempData["SaveResult"] = "Your cart has been updated.";
                return(RedirectToAction("Index"));
            }
            ModelState.AddModelError("", "Your cart could not be updated.");
            return(View(model));
        }