public ActionResult Vote(VoteInputModel model)
        {
            if (model != null && this.ModelState.IsValid)
            {
                var userId = this.User.Identity.GetUserId();
                if (!this.Data.Votes.All().Any(x => x.TeamId == model.TeamId && x.UserId == userId))
                {
                    var vote = new Vote()
                    {
                        TeamId = model.TeamId,
                        UserId = userId,
                        Value = 1
                    };
                    this.Data.Votes.Add(vote);
                    this.Data.SaveChanges();

                    var newVotes = this.Data.Votes.All().Where(x => x.TeamId == model.TeamId)
                        .Sum(x => x.Value);
                    return this.Json(newVotes);
                }
            }

            var votes = this.Data.Votes.All().Where(x => x.TeamId == model.TeamId)
                .Sum(x => x.Value);
            return this.Json(votes);
        }
Exemplo n.º 2
0
        public ActionResult Vote(Team team)
        {
            var vote = new Vote { TeamId = team.Id, UserId = this.UserProfille.Id, VoteValue = 1 };
            this.Data.Votes.Add(vote);
            this.Data.SaveChanges();

            return this.Content(team.Votes.Sum(v => v.VoteValue).ToString());
        }