public int Vote(int id, string userId) { var user = this.Data.Users.Find(userId); var picture = this.Data.Pictures.Find(id); if (picture.Contest.Status != ContestStatus.Active) { throw new BadRequestException("The contest is closed."); } var votingStrategy = StrategyFactory.GetVotingStrategy(picture.Contest.VotingStrategy.VotingStrategyType); votingStrategy.CheckPermission(this.Data, user, picture.Contest); if (picture.Votes.Any(v => v.UserId == user.Id)) { throw new BadRequestException("You have already voted for this picture."); } var vote = new Vote { PictureId = picture.Id, UserId = user.Id }; this.Data.Votes.Add(vote); this.Data.SaveChanges(); return(picture.Votes.Select(p => p.Id).Count()); }