public ActionResult Index(VotingViewModel votes) { //Not Working To Capture Only Checked Box //if (votes.voteResults.Where(x => x.IsSelected == true) != null) // votes.newCheckedBoxID = votes.voteResults.Where(x => x.IsSelected == true).Select(x => x.ID).First(); //Build Internal DB Object Vote vote = new Vote(); vote.SnackID = votes.newCheckedBoxID; vote.VotedOn = votes.VotedOn; //Save TO Internal DB On Success if (ModelState.IsValid) { db.Votes.Add(vote); db.SaveChanges(); } //Get Cookie Info IF Applicable HttpCookie myCookie = HttpContext.Request.Cookies["votes"]; votes.votedCnt = Convert.ToInt32(myCookie.Value) + 1; //Save Cookie - incremented votedCnt var voteCookie = new HttpCookie("votes", votes.votedCnt.ToString()); DateTime begNextMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1); voteCookie.Expires = begNextMonth; HttpContext.Response.Cookies.Add(voteCookie); // Over-Voting Handled Without A Message return RedirectToAction("Index"); }
public ActionResult Index() { VotingViewModel votList = new VotingViewModel(); //Make Web Service Request string serviceURL = "https://api-snacks.nerderylabs.com/v1/snacks/"; var client = new RestClient(serviceURL, HttpVerb.GET); var json = client.MakeSnackRequest(""); //Parse JSON Into PRoper Payload Structure List<RestDataModel.GetSnacks> payload = JsonConvert.DeserializeObject<List<RestDataModel.GetSnacks>>(json); //Separate Both Snack Lists votList.votOnlineList = payload.Where(x => x.optional == true); votList.alwaysPurchasedOnlineList = payload.Where(x => x.optional == false); //Legacy Internal DB Code //votList.sugList = (from a in db.Suggestions // select a).ToList(); votList.votList = (from a in db.Votes select a).ToList(); //Get Cookie Info IF Applicable HttpCookie myCookie = HttpContext.Request.Cookies["votes"]; votList = VotingViewLogic.GetCounterInfo(votList, myCookie); return View(votList); }
public static VotingViewModel GetCounterInfo(VotingViewModel votList, HttpCookie myCookie) { if (myCookie == null) { votList.votedCnt = 0; votList.voteCounter = "3"; votList.voteCounterCss = "counter counter_green"; } else if (myCookie != null) { //Check number of votes in Cookie votList.votedCnt = Convert.ToInt32(myCookie.Value); if (votList.votedCnt == 1) { votList.votedCnt = 1; votList.voteCounter = "2"; votList.voteCounterCss = "counter counter_yellow"; } else if (votList.votedCnt == 2) { votList.votedCnt = 2; votList.voteCounter = "1"; votList.voteCounterCss = "counter counter_red"; } else if (votList.votedCnt == 3) { votList.votedCnt = 3; votList.voteCounter = "No Votes Remaining"; votList.voteCounterCss = "counter counter_red"; } } // //Replaced With Cookies // //Calculate vote counter and css to return to view // if (votList.votList.Count() == 1) // { // votList.voteCounter = "1"; // votList.voteCounterCss = "counter counter_green"; // } // else if (votList.votList.Count() == 2) // { // votList.voteCounter = "2"; // votList.voteCounterCss = "counter counter_yellow"; // } // else if (votList.votList.Count() == 3) // { // votList.voteCounter = "3"; // votList.voteCounterCss = "counter counter_red"; // } // else // { // votList.voteCounter = "No Snacks Have Been Suggested!"; // votList.voteCounterCss = "counter counter_red"; // } return votList; }