// // GET: /vraag/VoteUp/QuestionID public ActionResult VoteUp(int id) { if (User.Identity.IsAuthenticated) // check if logged in { // get user stuff var userVoiting = from userVote in db.Users where userVote.UserName == User.Identity.Name select userVote; // get the info of the question which is getting the vote var QuestionGettingVoted = from vraag in db.Questions where vraag.QuestionID == id select vraag; // check if they yielded some info (and not more then one) if (userVoiting.Count() == 1 && QuestionGettingVoted.Count() == 1) { var UserVoting = userVoiting.First(); // check if this is a up vote or a second time vote (so downvote) var voteInfo = from vote in db.Votes where vote.UserID == UserVoting.UserID && vote.QuestionID == id select vote; // cast this question to a single so we can do somestuff var Question = QuestionGettingVoted.First(); // get the info of the person which made this question var userGettingVoted = from user in db.UserMeta where user.UserId == Question.UserId select user; if (voteInfo.Count() == 1) // if so we are downvotting { // remove the vote from the datebase db.Votes.Remove(voteInfo.First()); Question.Votes -= 1; // lower the votes on this question userGettingVoted.First().Votes -= 1; // and on the user db.SaveChanges(); // save } else // else we are upvoting (firsttime) { // create a new vote VoteUser newVote = new VoteUser() { QuestionID = Question.QuestionID, UserID = UserVoting.UserID }; Question.Votes += 1; // increase the votes on this question with one userGettingVoted.First().Votes += 1; // and the user that made this question db.Votes.Add(newVote); // add the vote to the db db.SaveChanges(); // save } } else ModelState.AddModelError("", "Je account bestaat niet of de vraag bestaat niet."); } else ModelState.AddModelError("", "Je moet ingelogd zijn om te stemmen."); // redirect us to the right page if (!Request.UrlReferrer.AbsolutePath.Contains("vraag")) return RedirectToAction("index", "default"); else return Redirect(Request.UrlReferrer.AbsolutePath); }
// // GET: answer/voteup/answerID public ActionResult VoteUp(int id) { // check if we are logged in if (User.Identity.IsAuthenticated) { // if so get the stuff of this user var userVoiting = from userVote in db.Users where userVote.UserName == User.Identity.Name select userVote; // get the stuff of this answer (which gets the vote up or down var AnswerGettingVoted = from Answers in db.Answers where Answers.AnswerID == id select Answers; // check if that did return some stuff if (userVoiting.Count() == 1 && AnswerGettingVoted.Count() == 1) { // cast it to a single var UserVoting = userVoiting.First(); // check if this is a up vote or a second time vote (so downvote) var voteInfo = from vote in db.Votes where vote.UserID == UserVoting.UserID && vote.AnswerID == id select vote; // cast the answer to a single var answer = AnswerGettingVoted.First(); // get the userMeta of the person whoes answer it is var userGettingVoted = (from user in db.UserMeta where user.UserId == answer.UserId select user).Single(); if (voteInfo.Count() == 1) { // downvote :< db.Votes.Remove(voteInfo.First()); answer.Votes -= 1; userGettingVoted.Votes -= 1; db.SaveChanges(); } else { // upvote VoteUser newVote = new VoteUser() { AnswerID = answer.AnswerID, UserID = UserVoting.UserID }; answer.Votes += 1; db.Votes.Add(newVote); userGettingVoted.Votes += 1; db.SaveChanges(); } } else // if we didn't find the answer of the account didn't exist ModelState.AddModelError("", "Je account bestaat niet of het antwoord bestaat niet."); } // then return them back to their page they came from if (!Request.UrlReferrer.AbsolutePath.Contains("vraag")) return RedirectToAction("index", "default"); else return Redirect(Request.UrlReferrer.AbsolutePath); }