private object SetVote(LinkVote vote) { NormalizeValue(vote); var existingVote = Db.LinkVotes.FirstOrDefault(lv => lv.VoterId == LoginManager.CurrentUser.Id && lv.LinkId == vote.LinkId); if (existingVote != null) { if (vote.Value == 0) { Db.LinkVotes.Remove(existingVote); } else { existingVote.Value = vote.Value; } } else { vote.Voter = LoginManager.CurrentUser; Db.LinkVotes.Add(vote); } int voteTally = Db .Links .Include(l => l.Votes) .FirstOrDefault(l => l.Id == vote.LinkId).VoteTally; return(new { VoteTally = voteTally, UpvoteCurrentUser = vote.Value == 1, DownvoteCurrentUser = vote.Value == -1 }); }
public async Task <IActionResult> AddVote(int id) { bool hasVoted = _linkVotes.VoteCheck(id, _userManager.GetUserId(User)); if (!hasVoted) { await _links.AddVote(id); } else { TempData["alert"] = "alert"; return(RedirectToAction(nameof(Index))); } LinkVote linkVote = new LinkVote { LinkId = id, ApplicationUserId = _userManager.GetUserId(User) }; await _linkVotes.Add(linkVote); return(RedirectToAction(nameof(Index))); }
public object Post(LinkVote vote) { // TODO: if no LinkId, return an error var result = SetVote(vote); Db.SaveChanges(); return(result); }
private void NormalizeValue(LinkVote vote) { if (vote.Value < 0) { vote.Value = -1; } else if (vote.Value > 1) { vote.Value = 1; } }
private Task Vote(int id, ClaimsPrincipal user, int value, CancellationToken ct) { var vote = new LinkVote { LinkId = id, User = user.FindFirst(BobbinsClaimTypes.ScreenName)?.Value, Value = value }; return(_http.PutJsonAsync("/votes", vote, ct)); }