public bool Add(UserVoteModel model)
        {
            int userId = GetUserId();

            model.UserId = userId;
            return(_userVote.Add(model));
        }
Beispiel #2
0
        public bool Edit(UserVoteModel model)
        {
            using (var scope = new TransactionScope())
            {
                try
                {
                    var oldOptions = _option.GetOptionList(model.UserId, model.PollId);

                    foreach (var option in model.Options)
                    {
                        if (!oldOptions.Contains(option))
                        {
                            var userVote = new UserPollOptionModel()
                            {
                                UserId   = model.UserId,
                                OptionId = option,
                                PollId   = model.PollId
                            };
                            if (_userVote.IsAlreadyVoted(userVote))
                            {
                                if (model.Options.Count == 1)
                                {
                                    scope.Dispose();
                                    throw new ReturnExceptionModel(new CustomExceptionModel()
                                    {
                                        StatusCode = HttpStatusCode.BadRequest, Message = "User already voted"
                                    });
                                }
                            }
                            else
                            {
                                _userVote.Add(userVote);
                            }
                        }
                    }

                    foreach (var option in oldOptions)
                    {
                        if (!model.Options.Contains(option))
                        {
                            _option.Delete(option);
                        }
                    }
                    scope.Complete();
                    return(true);
                }
                catch (Exception ex)
                {
                    scope.Dispose();
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    string json             = js.Serialize(model);
                    Log.Error("BL-Voting - Edit" + json, ex);
                    throw new ReturnExceptionModel(new CustomExceptionModel()
                    {
                        StatusCode = HttpStatusCode.BadRequest, Message = ex.Message
                    });
                }
            }
        }
Beispiel #3
0
        public bool Add(UserVoteModel model)
        {
            using (var scope = new TransactionScope())
            {
                try
                {
                    if (!_poll.IsActive(model.PollId))
                    {
                        throw new ReturnExceptionModel(new CustomExceptionModel()
                        {
                            StatusCode = HttpStatusCode.BadRequest, Message = "Voting not started yet."
                        });
                    }
                    foreach (var option in model.Options)
                    {
                        var userVote = new UserPollOptionModel()
                        {
                            UserId   = model.UserId,
                            OptionId = option,
                            PollId   = model.PollId
                        };
                        if (_userVote.IsAlreadyVoted(userVote))
                        {
                            if (model.Options.Count == 1)
                            {
                                scope.Dispose();
                                throw new InvalidOperationException("You have already voted for this poll");
                            }
                        }
                        else
                        {
                            _userVote.Add(userVote);
                        }
                    }

                    scope.Complete();
                    return(true);
                }
                catch (Exception ex)
                {
                    scope.Dispose();
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    string json             = js.Serialize(model);
                    Log.Error("BL-Voting - Add" + json, ex);
                    throw new ReturnExceptionModel(new CustomExceptionModel()
                    {
                        StatusCode = HttpStatusCode.BadRequest, Message = ex.Message
                    });
                }
            }
        }
        public async Task <IActionResult> Downvote(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var post = _context.Post.Include(p => p.UsersWhoVoted).SingleOrDefault(p => p.Id == id);

            var user = await _signInManager.UserManager.GetUserAsync(User);

            var UserVoteModel = _context.VotedUsers.Where(p => p.PostId == id).Where(p => p.UserId == user.Id).Take(1).SingleOrDefault();

            UserVoteModel VotedUser = null;

            if (UserVoteModel == null)
            {
                VotedUser = new UserVoteModel
                {
                    Post   = post,
                    PostId = post.Id,
                    User   = user,
                    UserId = user.Id,
                    Vote   = Vote.Down
                };
                post.DownVotes++;
                _context.Update(post);
                _context.VotedUsers.Add(VotedUser);
            }
            else
            {
                UserVoteModel.Vote = Vote.Down;
                post.DownVotes++;
                post.UpVotes--;
                _context.Update(post);
                _context.VotedUsers.Update(UserVoteModel);
            }
            _context.SaveChanges();

            return(RedirectToAction($"Post/{id}"));
        }
 public bool Edit(UserVoteModel model)
 {
     return(_userVote.Edit(model));
 }