Ejemplo n.º 1
0
        public ActionResult Buy(Purchase purch)
        {
            // if (purch.PurchasedItems == null) Response.Redirect("~/Home/Index");
            if (ModelState.IsValid)
            {
                purch.PurchasedItems = new List <ItemAndSize>();

                HttpCookie cookie  = Request.Cookies.Get("ShoppingCartId"); // get the cookie
                var        cartObj = (from obj in context.Carts             // get the cart
                                      where obj.ShoppingCartId == cookie.Value
                                      select obj).FirstOrDefault();

                foreach (var item in cartObj.BoughtItems)
                {
                    purch.PurchasedItems.Add(item); // add it to the new list
                }

                context.Purchases.Add(purch);         // add to Db
                context.SaveChanges();
                return(RedirectToAction("ThankYou")); // if purchase was made
            }
            else
            {
                //     purch.CartItems = context.Carts.ToList(); // populate the list again

                return(View(purch)); // if something failed return the view
            }
        }
Ejemplo n.º 2
0
        public ActionResult Sent(int id)
        {
            var purch = context.Purchases.Find(id);

            purch.Status = PurchaseStatus.Sent;
            context.SaveChanges();
            return(Redirect(Request.UrlReferrer.ToString()));
        }
Ejemplo n.º 3
0
 public ActionResult Create(Item item)
 {
     if (ModelState.IsValid) // if everything is good
     {
         context.Items.Add(item);
         context.SaveChanges();
         return(Content("<script>if (confirm('Item was created.')) {    (window.location.replace('/Admin/Index'));} else {(window.location.replace('/Admin/Index'));}</script>"));
     }
     else
     {
         return(View(item));
     }
 }
Ejemplo n.º 4
0
        public ActionResult Shirts(int id, string sortOrder, string submit, int?page)
        {
            string size = string.Empty;

            switch (submit)
            {
            case "XS": size = "XS";
                break;

            case "S": size = "S";
                break;

            case "M": size = "M";
                break;

            case "L": size = "L";
                break;

            case "XL": size = "XL";
                break;

            case "XXL": size = "XS";
                break;

            default:
                throw new Exception();
            }

            HttpCookie cookie = Request.Cookies.Get("ShoppingCartId"); // get the user's cookie

            if (cookie == null)                                        // if cookie is null initialize new cart => new client and purchase
            {
                Random rand        = new Random();
                int    random      = rand.Next();
                string cookieValue = random.ToString();
                // ADD COOKIE
                // every cookie is tied with long random generated number ( almost in every case its going to be unique)
                HttpCookie userIdCookie = new HttpCookie("ShoppingCartId");
                userIdCookie.Value   = cookieValue;
                userIdCookie.Expires = DateTime.Now.AddDays(90);
                Response.SetCookie(userIdCookie);
                // END ADD COOKIE
                var itemInDb = context.Items.Find(id);  // find the item

                Cart cart = new Cart();
                cart.BoughtItems = new List <ItemAndSize>();
                ItemAndSize itemToBuy = new ItemAndSize(); // create new item for adding
                itemToBuy.Item = itemInDb.ItemId.ToString();

                itemToBuy.Sizes = size; // add it

                cart.ShoppingCartId = cookieValue;
                cart.BoughtItems.Add(itemToBuy);
                cart.BoughtSizes = size;
                context.Carts.Add(cart);
                context.SaveChanges();
            }
            else
            {
                var getCart = (from obj in context.Carts //get the cart
                               where obj.ShoppingCartId == cookie.Value
                               select obj).FirstOrDefault();
                var         itemInDb  = context.Items.Find(id); // find the item
                ItemAndSize itemToBuy = new ItemAndSize();
                itemToBuy.Item = itemInDb.ItemId.ToString();

                itemToBuy.Sizes = size;
                getCart.BoughtItems.Add(itemToBuy);
            }

            context.SaveChanges();


            if ((page <= 0) || (page == null))
            {
                ViewBag.id = 1;
            }
            else
            {
                ViewBag.id = page;
            }

            ViewBag.CurrentSort = sortOrder;
            List <Item> items = new List <Item>();

            items = context.Items.ToList();
            List <Item> toRender = new List <Item>();

            foreach (var item2 in items)
            {
                if (item2.Description.ToLower() == "shirt")
                {
                    toRender.Add(item2);
                }
            }
            int pageSize   = 8;
            int pageNumber = page ?? 1;

            switch (sortOrder)
            {
            case "high":
                toRender = toRender.OrderByDescending(obj => obj.Price).ToList();
                break;

            case "low":
                toRender = toRender.OrderBy(obj => obj.Price).ToList();
                break;
            }
            return(View(toRender.ToPagedList(pageNumber, pageSize)));
        }