public ActionResult Index() { List<Snack> snacks = new List<Snack>(); List<SuggestedSnack> suggestedSnacks = new List<SuggestedSnack>(); //get json string containing snacks from web service string snackResult = SnacksServiceHelper.GetSnacks(); if (!string.IsNullOrEmpty(snackResult)) { try { snacks = JsonConvert.DeserializeObject<List<Snack>>(snackResult); } catch(Exception ex) { //display error } if (snacks.Count() > 0) { List<Vote> votes = new List<SnaFoo.Models.Vote>(); try { //get all votes from database for this month votes = db.Votes.Where(x => x != null && x.VotedOn.Month == DateTime.Now.Month).ToList(); } catch(Exception Ex) { } List<Suggestion> suggestions = new List<Suggestion>(); try { //get all suggestions from database for this month suggestions = db.Suggestions.Where(x => x != null && x.SuggestedOn.Month == DateTime.Now.Month).ToList(); } catch (Exception ex) { } foreach (Snack s in snacks.Where(x => suggestions.Select(y => y.SnackId).Contains(x.Id))) { SuggestedSnack ss = new SuggestedSnack(); ss.Id = s.Id; ss.LastPurchaseDate = s.LastPurchaseDate; ss.Name = s.Name; ss.Optional = s.Optional; ss.PurchaseCount = s.PurchaseCount; ss.PurchaseLocations = s.PurchaseLocations; ss.Votes = votes.Count(x => x.SnackId == s.Id); suggestedSnacks.Add(ss); } } } return View(new SnacksAndSuggestedSnacks { Snacks = snacks.Where(x => x.Optional == false).ToList(), SuggestedSnacks = suggestedSnacks }); }
public ActionResult ShoppingList() { List<Snack> snacks = new List<Snack>(); try { //get snacks from web service snacks = JsonConvert.DeserializeObject<List<Snack>>(SnacksServiceHelper.GetSnacks()); } catch (Exception ex) { //display error } if (snacks != null && snacks.Count() > 0) { List<Vote> votes = new List<Vote>(); List<Suggestion> suggestions = new List<Suggestion>(); try { //get votes from database votes = db.Votes.Where(x => x != null && x.VotedOn.Month == DateTime.Now.Month).ToList(); } catch(Exception ex) { } try { //get suggestions from database suggestions = db.Suggestions.Where(x => x != null && x.SuggestedOn.Month == DateTime.Now.Month).ToList(); } catch(Exception ex) { } List<SuggestedSnack> suggestedSnacks = new List<SuggestedSnack>(); foreach (Snack s in snacks.Where(x => suggestions.Select(y => y.SnackId).Contains(x.Id))) { SuggestedSnack ss = new SuggestedSnack(); ss.Id = s.Id; ss.LastPurchaseDate = s.LastPurchaseDate; ss.Name = s.Name; ss.Optional = s.Optional; ss.PurchaseCount = s.PurchaseCount; ss.PurchaseLocations = s.PurchaseLocations; ss.Votes = votes.Count(x => x.SnackId == s.Id); suggestedSnacks.Add(ss); } //list of always purchased snacks snacks = snacks.Where(x => x.Optional == false).ToList(); //list of suggested snacks ordered by number of votes and we only take the difference between 10 and the number of always purchased snacks suggestedSnacks = suggestedSnacks.OrderByDescending(x => x.Votes).Take(10 - snacks.Count()).ToList(); foreach (var ss in suggestedSnacks) { Snack s = new Snack(); s.Id = ss.Id; s.PurchaseLocations = ss.PurchaseLocations; s.Name = ss.Name; snacks.Add(s); } } if (snacks != null) { return View(snacks); } else { return View(new List<Snack>()); } }