/// <summary> /// Updates the daily deals daily automatically. /// </summary> public void DailyDeals(int numberOfDeals = 3) { // Settings var save = 0.85; // 15% off // Remove old deals List <DailyDealModel> currentDeals = dailyDeals.GetAll().ToList(); foreach (DailyDealModel deal in currentDeals) { deal.Product.Price = deal.ProductOriginalPrice; products.Edit(deal.Product); } dailyDeals.RemoveRange(currentDeals); // Add new deals Random r = new Random(); List <int> usedNumbers = new List <int>(); List <ProductModel> allProducts = products.GetAll().ToList(); int count = allProducts.Count; for (int i = 0; i < numberOfDeals; i++) { int rInt = r.Next(0, count - 1); int maxTry = 10; while (usedNumbers.Contains(rInt) && maxTry >= 0) { rInt = r.Next(0, count - 1); maxTry--; } usedNumbers.Add(rInt); try { DailyDealModel deal = new DailyDealModel { Product = allProducts[rInt] }; deal.ProductOriginalPrice = deal.Product.Price; deal.Product.Price = (deal.Product.Price * save); dailyDeals.Add(deal); } catch (Exception) { if (numberOfDeals < 50) { numberOfDeals++; } } } }
public ActionResult Index() { return(View(dailyDeals.GetAll())); }