示例#1
0
        public IActionResult Index()
        {
            string username = HttpContext.Session.GetString("username");

            if (username == null)
            {
                return(RedirectToAction("Index", "Login"));
            }
            else
            {
                List <PurchasedProduct> purchasedProducts = cv.UserPurProd(username);



                List <PurchasedProduct> sortedlist = new List <PurchasedProduct>();

                foreach (PurchasedProduct pp in purchasedProducts)
                {
                    sortedlist.Add(pp);
                }

                List <string>   prodNameList = new List <string>();
                List <string>   actCodeList  = new List <string>();
                List <DateTime> dateList     = new List <DateTime>();
                List <string>   imageList    = new List <string>();
                List <string>   descList     = new List <string>();

                for (int i = 0; i < sortedlist.Count(); i++)
                {
                    if (i == 0)
                    {
                        prodNameList.Add(sortedlist[i].ProductName);
                        actCodeList.Add(sortedlist[i].ActivationCode);
                        dateList.Add(sortedlist[i].PurchaseDate.Date);
                        imageList.Add(sortedlist[i].Image);
                        descList.Add(sortedlist[i].Description);
                    }
                    else
                    {
                        int status = 0;
                        for (int j = 0; j < prodNameList.Count(); j++)
                        {
                            if (sortedlist[i].PurchaseDate.Date == dateList[j] && sortedlist[i].ProductName == prodNameList[j])
                            {
                                actCodeList[j] += "," + sortedlist[i].ActivationCode;
                                status          = 1;
                                break;
                            }
                        }
                        if (status == 0)
                        {
                            prodNameList.Add(sortedlist[i].ProductName);
                            actCodeList.Add(sortedlist[i].ActivationCode);
                            dateList.Add(sortedlist[i].PurchaseDate.Date);
                            imageList.Add(sortedlist[i].Image);
                            descList.Add(sortedlist[i].Description);
                        }
                    }
                }

                ViewBag.prodName    = prodNameList;
                ViewBag.actCode     = actCodeList;
                ViewBag.date        = dateList;
                ViewBag.image       = imageList;
                ViewBag.desc        = descList;
                ViewData["session"] = username;
                ViewData["CartQty"] = (cv.GetQtyInSessionCart(username)).ToString();

                return(View());
            }
        }
        public IActionResult Index(string id)
        {
            //initiate session for not logged in users
            if (HttpContext.Session.GetString("tempCart") == null)
            {
                HttpContext.Session.SetString("tempCart", "");
            }
            //search view
            if (!String.IsNullOrEmpty(id))
            {
                string searchString = id.ToLower();

                List <Product> productsNew = new List <Product>();

                var products  = cv.Listofproduct();
                var products2 = products.Where(p => p.ProductName.ToLower().Contains(searchString) || p.ProductDescription.ToLower().Contains(searchString));

                foreach (Product product in products2)
                {
                    productsNew.Add(product);
                }
                ViewData["ProductData"] = productsNew;
                ViewData["session"]     = HttpContext.Session.GetString("username");
                string username1 = (string)ViewData["session"];

                Customer customer1 = cv.GetCust(username1);
                if (customer1 != null)
                {
                    ViewData["CartQty"]      = (cv.GetQtyInSessionCart(username1)).ToString();
                    ViewData["customername"] = customer1.FirstName;
                    return(View());
                }
                ViewData["CartQty"] = HttpContext.Session.GetString("cartQty");
                return(View());
            }
            ViewData["session"] = HttpContext.Session.GetString("username");
            string   username = (string)ViewData["session"];
            Customer customer = cv.GetCust(username);

            if (customer != null)
            {
                ViewData["customername"] = customer.FirstName;
                ViewData["ProductData"]  = cv.Listofproduct();

                string tempCart = HttpContext.Session.GetString("tempCart");

                if (tempCart != "")
                {
                    string[]       tempProductList = tempCart.Split(";");
                    List <Product> products        = new List <Product>();
                    foreach (string tempProd in tempProductList)
                    {
                        Product product = cv.GetProd(tempProd);
                        products.Add(product);
                    }

                    foreach (Product p in products)
                    {
                        ShoppingCart cart1 = cv.GetCart(username, p.ProductName);
                        if (cart1 == null)
                        {
                            cv.CreateCart(username, p.ProductName);
                        }
                        else
                        {
                            cv.IncreaseProductQty(cart1);
                        }
                    }
                }
                HttpContext.Session.Remove("tempCart");
                ViewData["CartQty"] = (cv.GetQtyInSessionCart(username)).ToString();
                return(View());
            }
            ViewData["ProductData"] = cv.Listofproduct();
            ViewData["CartQty"]     = HttpContext.Session.GetString("cartQty");
            return(View());
        }