Ejemplo n.º 1
0
 public static void Vote(VoteBindingModel model)
 {
     using (var context = new PizzaMoreDbContext())
     {
         var pizzaToVote = context.Pizzas.Find(model.PizzaId);
         if (model.Vote == -1)
         {
             pizzaToVote.DownVotes = model.Vote;
         }
         else
         {
             pizzaToVote.UpVotes = model.Vote;
         }
         context.SaveChanges();
     }
 }
Ejemplo n.º 2
0
        public ActionResult Vote(VoteBindingModel voteModel)
        {
            int userId = this.User.GetUserId();

            bool voteResult = this.votingManager.Vote(voteModel.VotingId, userId, voteModel.PresentId);

            if (!voteResult)
            {
                this.TempData.Add(TempDataErrorMessageKey, VoteError);
            }
            else
            {
                this.TempData.Add(TempDataSuccessMessageKey, VoteSuccessful);
            }

            return(RedirectToAction(nameof(VotingsController.Index)));
        }
Ejemplo n.º 3
0
        public IHttpActionResult Post([FromBody] VoteBindingModel vote)
        {
            if (this.data.Votes.All().Any(x => x.ProjectId == vote.ProjectId && x.UserId == vote.UserId))
            {
                return(this.BadRequest("You already voted for this project."));
            }

            var newVote = Mapper.Map <Vote>(vote);

            this.data.Votes.Add(newVote);
            var project = this.data.Projects.Find(vote.ProjectId);

            project.Points += vote.Value;
            this.data.Projects.Update(project);

            this.data.SaveChanges();

            return(this.Ok());
        }
Ejemplo n.º 4
0
        public IActionResult Menu(VoteBindingModel model, HttpSession session, HttpResponse response)
        {
            if (UserService.HasLoggedInUser(session, this.db))
            {
                if (model.Vote == 1)
                {
                    db.Pizzas.FirstOrDefault(p => p.Id == model.PizzaId).UpVotes += 1;
                }
                else
                {
                    db.Pizzas.FirstOrDefault(p => p.Id == model.PizzaId).DownVotes -= 1;
                }

                db.SaveChanges();

                Redirect(response, "/home/menu");
            }

            return(null);
        }
Ejemplo n.º 5
0
        public ActionResult Vote(int id)
        {
            int userId = this.User.GetUserId();

            bool canEmployeeVote = this.votingManager.CanEmployeeVoteInVoting(id, userId);

            if (!canEmployeeVote)
            {
                this.TempData.Add(TempDataErrorMessageKey, VoteError);
                return(this.RedirectToAction(nameof(VotingsController.Index)));
            }

            ICollection <PresentDescription> presents = this.votingManager.GetAllPresents();

            VoteBindingModel voteModel = new VoteBindingModel();

            voteModel.VotingId           = id;
            voteModel.PresentsSelectList = new SelectList(presents, "Id", "Name", "-- none --");

            return(this.View(voteModel));
        }
Ejemplo n.º 6
0
        public ActionResult Vote(VoteBindingModel model)
        {
            if (model != null && this.ModelState.IsValid)
            {
                var userId = this.User.Identity.GetUserId();

                var contest = this.ContestsData.Contests.Find(model.ContestId);
                if (contest == null)
                {
                    this.AddToastMessage("Error", "Non existing contest!", ToastType.Error);
                    var currentVotesNumber = this.ContestsData.Votes.All().Count(v => v.PhotoId == model.PhotoId && !v.Photo.IsDeleted);
                    return this.Json(currentVotesNumber);
                }

                if (contest.IsFinalized && !contest.IsActive)
                {
                    this.AddToastMessage("Warning", "You are not allowed to vote for this contest.", ToastType.Warning);
                    var currentVotesNumber = this.ContestsData.Votes.All().Count(v => v.PhotoId == model.PhotoId && !v.Photo.IsDeleted);
                    return this.Json(currentVotesNumber);
                }

                var photo = this.ContestsData.Photos.Find(model.PhotoId);
                if (photo == null)
                {
                    this.AddToastMessage("Error", "Non existing photo!", ToastType.Error);
                    var currentVotesNumber = this.ContestsData.Votes.All().Count(v => v.PhotoId == model.PhotoId && !v.Photo.IsDeleted);
                    return this.Json(currentVotesNumber);
                }

                VotingModel votingModel = new VotingModel { Contest = contest, Photo = photo, UserId = userId };
                VotingStrategy strategy = VotingFactory.CreateStrategy(contest.VotingType, votingModel);
                bool canVote = strategy.CanVote();
                if (canVote)
                {
                    var vote = new Vote
                    {
                        PhotoId = model.PhotoId,
                        UserId = userId,
                        ContestId = model.ContestId
                    };
                    this.ContestsData.Votes.Add(vote);
                    this.ContestsData.SaveChanges();

                    var newVotes = this.ContestsData.Votes.All()
                        .Count(v => v.PhotoId == model.PhotoId && !v.Photo.IsDeleted);
                    return this.Json(newVotes);
                }
            }

            var votes = this.ContestsData.Votes.All().Count(v => v.PhotoId== model.PhotoId && !v.Photo.IsDeleted);
            return this.Json(votes);
        }
Ejemplo n.º 7
0
 public IActionResult Menu(VoteBindingModel model, HttpResponse response)
 {
     UserService.Vote(model);
     Redirect(response, "/home/menu");
     return(null);
 }