コード例 #1
0
        public void OnGetDelete(string UserID)
        {
            // Check if the user is logged in and authorised
            if (User.Identity.IsAuthenticated && User.IsInRole("Admin"))
            {
                ApplicationUser user          = _userManager.FindByIdAsync(UserID).Result;
                Shopping_card   Shopping_card = _context.Shopping_card.Where(shoping => shoping.User_ID == UserID)
                                                .FirstOrDefault();
                if (Shopping_card != null)
                {
                    List <Shopping_card_Product> Shopping_card_Products = _context.Shopping_Card_Products
                                                                          .Where(product => product.Shopping_card_ID == Shopping_card.ID)
                                                                          .ToList();

                    if (Shopping_card_Products.Count() > 0)
                    {
                        // Remove all items from the users shoppingcard.
                        foreach (var item in Shopping_card_Products)
                        {
                            _context.Shopping_Card_Products.Remove(item);
                        }
                    }

                    // Remove the shoppingcard of the user.
                    _context.Shopping_card.Remove(Shopping_card);
                }


                // Remove the user from database.
                var task = Task.Run(async() => { await _userManager.DeleteAsync(user); });
                task.Wait();

                Users = _context.Users.AsNoTracking().ToList();
            }
        }
コード例 #2
0
        public void OnGet()
        {
            if (User.Identity.IsAuthenticated)
            {
                string id     = _userManager.GetUserId(User);
                var    query2 = from shop in _context.Shopping_card
                                where shop.User_ID == id
                                select shop;
                YourCart = query2.FirstOrDefault();

                if (YourCart == null)
                {
                    YourCart = new Shopping_card()
                    {
                        User_ID = id
                    };
                    _context.Add(YourCart);
                    _context.SaveChanges();
                }

                var query = from shopping in _context.Shopping_card
                            where shopping.User_ID == id
                            let shoppingProducts = (
                    from shoppingProdutstable in _context.Shopping_Card_Products
                    from Products in _context.Product
                    where shoppingProdutstable.Shopping_card_ID == shopping.ID &&
                    shoppingProdutstable.Product_ID == Products.ID
                    select new ResponseShopingCart()
                {
                    product = Products, quantity = shoppingProdutstable.quantity + 1
                }
                    ).ToList()
                                                   select shoppingProducts;
                _Products = query.FirstOrDefault();
            }
            else
            {
                string cookieshoping = Request.Cookies["ShoppingCart"];
                List <shoppingCart_cookie> shoppingcartlist = Cookie.Cookiereader_shoppingcart(cookieshoping);
                _Products = new List <ResponseShopingCart>();

                foreach (shoppingCart_cookie item in shoppingcartlist)
                {
                    var query = from product in _context.Product
                                where product.ID == item.ProductID
                                select product;

                    _Products.Add(new ResponseShopingCart()
                    {
                        product = query.FirstOrDefault(), quantity = item.Quantity
                    });
                }
            }
        }
コード例 #3
0
        public void OnGet(string FirstName, string LastName, string Email)
        {
            //Store FirstName, LastName and Email in an class
            EmailKey = new FirstnameLastnameEmail()
            {
                Email = Email, Firstname = FirstName, Lastname = LastName
            };

            if (User.Identity.IsAuthenticated)
            {
                id    = _userManager.GetUserId(User);
                _User = _userManager.GetUserAsync(User).Result;
                var query2 = from shop in _context.Shopping_card
                             where shop.User_ID == id
                             select shop;
                YourCart = query2.FirstOrDefault();

                var query = from shopping in _context.Shopping_card
                            where shopping.User_ID == id
                            let shoppingProducts = (
                    from shoppingProdutstable in _context.Shopping_Card_Products
                    from Products in _context.Product
                    where shoppingProdutstable.Shopping_card_ID == shopping.ID &&
                    shoppingProdutstable.Product_ID == Products.ID
                    select new ResponseShopingCart()
                {
                    product = Products, quantity = shoppingProdutstable.quantity + 1
                }
                    ).ToList()
                                                   select shoppingProducts;
                _Products = query.FirstOrDefault();
            }
            else
            {
                string cookieshoping = Request.Cookies["ShoppingCart"];
                List <shoppingCart_cookie> shoppingcartlist = Cookie.Cookiereader_shoppingcart(cookieshoping);
                _Products = new List <ResponseShopingCart>();

                foreach (shoppingCart_cookie item in shoppingcartlist)
                {
                    var query = from product in _context.Product
                                where product.ID == item.ProductID
                                select product;

                    _Products.Add(new ResponseShopingCart()
                    {
                        product = query.FirstOrDefault(), quantity = item.Quantity
                    });
                }
            }
        }
コード例 #4
0
        public void OnPost(/*[FromBody]*/ int productid, string WichPage = "True")
        {
            // Controlleert of de gebruiker is ingelogd.
            if (User.Identity.IsAuthenticated) // Wordt uitgevoerd als de gebruiker is ingelogd.
            {
                string id = _userManager.GetUserId(User);

                var query = from shop in _context.Shopping_card
                            where shop.User_ID == id
                            select shop;

                Shopping_card shoping_check = query.FirstOrDefault();

                // Controlleert of de gebruiker al een shopping cart heeft.
                if (shoping_check == null) // Wordt uitgevoerd als de gebruiker nog geen shopping cart heeft.
                {
                    Console.WriteLine("");
                    Shopping_card shoppingCard = new Shopping_card()
                    {
                        User_ID = id.ToString(),
                        ShoppingCardProducts = new List <Shopping_card_Product>()
                        {
                            new Shopping_card_Product()
                            {
                                Product_ID = productid
                            }
                        }
                    };
                    _context.Shopping_card.Add(shoppingCard);
                    _context.SaveChanges();
                }
                else // Wordt uitgevoerd als gebruiker uit de shoppingcard al bestaat
                {
                    var query3 = (from shoping in _context.Shopping_card
                                  from shopingProduct in _context.Shopping_Card_Products
                                  where shopingProduct.Shopping_card_ID == shoping.ID && shoping.User_ID == id
                                  select shopingProduct).ToList();

                    List <Shopping_card_Product> shoppingCardProducts = query3;
                    bool checkProductExists = false;

                    foreach (var item in shoppingCardProducts)
                    {
                        if (item.Product_ID == productid)
                        {
                            checkProductExists = true;
                        }
                    }

                    var query2 = from shopingCard in _context.Shopping_card
                                 where shopingCard.User_ID == id
                                 select shopingCard;

                    Shopping_card shoppingCard = query2.FirstOrDefault();

                    if (!checkProductExists) // Wordt uitgevoerd als het product nog niet in de shopping cart zit.
                    {
                        Shopping_card_Product shoppingCardProduct = new Shopping_card_Product()
                        {
                            Shopping_card_ID = shoppingCard.ID,
                            Product_ID       = productid
                        };
                        _context.Shopping_Card_Products.Add(shoppingCardProduct);
                        _context.SaveChanges();
                    }
                    else // Wordt uitgevoerd als het product al wel in de shopping cart zit.
                    {
                        var queryShoppingCardId = (from shoppingCard_I in _context.Shopping_card
                                                   where shoppingCard_I.User_ID == id
                                                   select shoppingCard).FirstOrDefault();
                        Shopping_card_Product shoppingCardProduct = _context.Shopping_Card_Products.SingleOrDefault(b => b.Product_ID == productid && b.Shopping_card_ID == queryShoppingCardId.ID);
                        if (shoppingCardProduct != null)
                        {
                            shoppingCardProduct.quantity = shoppingCardProduct.quantity + 1;
                            _context.SaveChanges();
                        }
                    }
                }
            }
            else // Wordt uitgevoerd als de gebruiker niet is ingelogd. (Wordt dus gebruikt voor de cookies).
            {
                //
                CookieOptions cookieOptions = new CookieOptions
                {
                    Expires  = DateTime.Now.AddDays(14),
                    HttpOnly = true
                };

                // Split characters:
                // - between productID and quantity
                // + between Produts in shopping card.
                // Tuple<ProductID, Quantity

                string cookieshoping = Request.Cookies["ShoppingCart"];
                List <shoppingCart_cookie> shoppingcartlist = Cookie.Cookiereader_shoppingcart(cookieshoping);

                if (shoppingcartlist.Count == 0)
                {
                    Response.Cookies.Append("ShoppingCart", productid + "-" + 1, cookieOptions);
                }
                else
                {
                    // Controlleer of product al bestaat en zo je increase the count bij 1.
                    bool Productexists = false;
                    int  count         = 0;
                    foreach (shoppingCart_cookie item in shoppingcartlist)
                    {
                        if (item.ProductID == productid) // Wordt uigevoerd als item al in shoppingcard zit en verhoogd quantity met 1
                        {
                            Productexists = true;
                            shoppingcartlist[count].Quantity = shoppingcartlist[count].Quantity + 1;
                        }

                        count++;
                    }

                    if (!Productexists) // Als product nog niet bestaat wordt deze toegevoegd.
                    {
                        Response.Cookies.Append("ShoppingCart", cookieshoping + "_" + productid + "-" + 1, cookieOptions);
                    }
                    else // Als product all wel bestaat moet de cookie geupdate worden met een verhoogde quantity.
                    {
                        string cookieshoping_update = shoppingcartlist[0].ProductID + "-" + shoppingcartlist[0].Quantity;

                        for (int i = 1; i < shoppingcartlist.Count; i++)
                        {
                            cookieshoping_update = cookieshoping_update + "_" + shoppingcartlist[i].ProductID + "-" +
                                                   shoppingcartlist[i].Quantity;
                        }
                        Response.Cookies.Append("ShoppingCart", cookieshoping_update, cookieOptions);
                    }
                }
            }

            if (WichPage == "True")
            {
                // Redirect back to Product info
                Response.Redirect("ProductInfo?id=" + productid + "&ProductAdded=ShoppingCard");
            }
            // Redirect to shoppingcard
            Response.Redirect("ShoppingCart");
        }