public ActionResult Index() { if (Response.Cookies["User"].Value != null) { Session["User"] = ClientService.GetById(int.Parse(Response.Cookies["User"].Value)); } if (Session["User"] == null) { return(RedirectToAction("LoginClient", "Home")); } Client Client = Session["User"] as Client; List <ItemShopping> initList = ItemShoppingService.GetAll().ToList(); List <String> Cat = new List <string>(); List <String> Brand = new List <string>(); foreach (var item in initList) { item.ShoppingList = ShoppingListService.GetById((int)item.ShoppingListId); int id = (int)item.Id; ItemShopping sitem = ItemShoppingService.GetById(id); if (sitem.ShoppingList.ClientId == Client.Id) { if (sitem.Category != null) { Cat.Add(sitem.Category.ToString()); } if (sitem.Brand != null) { Brand.Add(sitem.Brand.ToString()); } if (sitem.Product != null) { Cat.Add(sitem.Product.Category.ToString()); Brand.Add(sitem.Product.Brand.ToString()); } } } var g = Cat.GroupBy(i => i); var k = Brand.GroupBy(i => i); int max = 0; string maxCat = ""; string maxBrand = ""; foreach (var grp in g) { if (grp.Count() > max) { maxCat = grp.Key; } } max = 0; foreach (var grp in k) { if (grp.Count() > max) { maxBrand = grp.Key; } } List <Product> prods = ProductService.GetAllProducts().ToList(); int ct = 0; foreach (var p in prods) { if (p.Category.Contains(maxCat) || p.Brand.Contains(maxBrand)) { if (ct == 0) { ViewBag.p1 = p; } else if (ct == 1) { ViewBag.p2 = p; } else if (ct == 2) { ViewBag.p3 = p; } else if (ct >= 3) { break; } ct++; } } if (ct < 3) { Random rnd = new Random(); int r = rnd.Next(prods.Count); if (ct == 0) { ViewBag.p1 = prods[r]; ViewBag.p2 = ViewBag.p1; while (ViewBag.p1 == ViewBag.p2) { rnd = new Random(); r = rnd.Next(prods.Count); ViewBag.p2 = prods[r]; } ViewBag.p3 = ViewBag.p1; while (ViewBag.p3 == ViewBag.p2 || ViewBag.p3 == ViewBag.p1) { rnd = new Random(); r = rnd.Next(prods.Count); ViewBag.p3 = prods[r]; } } else if (ct == 1) { ViewBag.p2 = ViewBag.p1; while (ViewBag.p1 == ViewBag.p2) { rnd = new Random(); r = rnd.Next(prods.Count); ViewBag.p2 = prods[r]; } ViewBag.p3 = ViewBag.p1; while (ViewBag.p3 == ViewBag.p2 || ViewBag.p3 == ViewBag.p1) { rnd = new Random(); r = rnd.Next(prods.Count); ViewBag.p3 = prods[r]; } } else { ViewBag.p3 = ViewBag.p1; while (ViewBag.p3 == ViewBag.p2 || ViewBag.p3 == ViewBag.p1) { rnd = new Random(); r = rnd.Next(prods.Count); ViewBag.p3 = prods[r]; } } } return(View()); }
public ActionResult Index() { TempData.Clear(); if (Session["Admin"] != null) { Admin a = Session["Admin"] as Admin; ViewBag.Admin += a.Name; } else if (Session["Vendor"] != null) { return(RedirectToAction("VendorPanel", "Admin")); } else { return(RedirectToAction("Login", "Admin")); } var prods = ProductService.GetAllProducts(); var items = ItemService.GetAll(); var shoppingLists = ShoppingListService.GetAll(); int count = 0; int count1 = 0; foreach (var i in items) { if (i.ProductId != null) { Product p = ProductService.GetProductById((int)i.ProductId); count++; if (TempData.ContainsKey(p.Name)) { TempData[p.Name] = 1 + (int)TempData[p.Name]; } else { TempData.Add(p.Name, 1); } } } TempData.Add("PieProds", count); ////// count = 0; foreach (var i in shoppingLists) { if (i.Finished != null) { if ((bool)i.Finished) { count++; } else { count1++; } } } TempData.Add("SlFinished", count); TempData.Add("SlUnFinished", count1); var caddies = CaddyService.GetAll(); int totalCaddies = caddies.Count(); int freeCaddies = 0; foreach (Caddy caddy in caddies) { if (caddy.State == "Free") { freeCaddies += 1; } } ViewBag.freeCaddies = freeCaddies; ViewBag.caddies = (int)(freeCaddies / totalCaddies) * 100; ViewBag.products = prods.Count(); ViewBag.ads = AdService.GetAll().Count(); ViewBag.users = ClientService.GetAll().Count(); return(View()); }