//[HttpPost] //[ValidateAntiForgeryToken] public ActionResult Vote(int id) { var picture = this.Data.ContestPictures.Find(id); var resultMessage = ""; if (picture == null) { resultMessage = "No such picture in contest!"; return this.Json(resultMessage, JsonRequestBehavior.AllowGet); } // TODO: Check if current user is logged, is author of the contests, if he can vote and is he votted yet. // Check whether user is author of the contest var userId = this.User.Identity.GetUserId(); var user = this.Data.Users.Find(userId); var userContests = user.Contests.ToList(); var currentContest = picture.Contest; if (currentContest.OwnerId == userId/*userContests.Contains(currentContest)*/) { resultMessage = "You cannot vote in your contest!"; return this.Json(resultMessage, JsonRequestBehavior.AllowGet); } // Check whether user has uploaded picture he wants to vote for if (picture.OwnerId == userId) { resultMessage = "You cannot vote for picture uploaded by you!"; return this.Json(resultMessage, JsonRequestBehavior.AllowGet); } // Check whether user can vote var currentContestContestors = currentContest.Contestors.ToList(); if (currentContest.VotingStrategy == VotingStrategy.Closed /*&& !currentContestContestors.Contains(user)*/) { // TODO: Find way to check when VotingStrategy is "Closed" whether user is invited to participate in contest!!! resultMessage = "You are not allowed to vote for pictute in this contest"; return this.Json(resultMessage, JsonRequestBehavior.AllowGet); } // Check whether user has votted for this contest before var pictureVotesVoterIds = picture.Votes.Select(v => v.UserId).ToList(); if (pictureVotesVoterIds.Contains(userId)) { resultMessage = "You are not allowed to vote more than once for pictute"; return this.Json(resultMessage, JsonRequestBehavior.AllowGet); } var newVote = new Vote() { VotedOn = DateTime.Now, User = user, Picture = picture }; picture.Votes.Add(newVote); this.Data.SaveChanges(); return this.Json(resultMessage, JsonRequestBehavior.AllowGet); }
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(); }
public ActionResult Vote(int id) { var picture = this.Data.Pictures.GetById(id); if (picture == null) { return this.HttpNotFound(); } if (picture.Contest.IsDeleted || picture.Contest.IsClosedForVoting) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "The contest is closed for voting."); } if (picture.Contest.Type == ContestType.Private && !picture.Contest.Participants.Contains(this.UserProfile) && picture.Contest.Type == ContestType.Private && picture.Contest.Owner != this.UserProfile) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "The contest is private for voting. You need to be invited by the owner to vote."); } if (picture.Votes.FirstOrDefault(v => v.User == this.UserProfile && v.IsDeleted == false) != null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "You have already voted!"); } var vote = new Vote { Picture = picture, User = this.UserProfile }; this.Data.Votes.Add(vote); this.Data.SaveChanges(); var likeModel = new LikeViewModel { Likes = picture.Votes.Count(v => v.IsDeleted == false), PictureId = picture.PictureId, Action = "UnVote", ThumbDirection = "down" }; return PartialView("_LikesCount", likeModel); }