コード例 #1
0
        public async Task <ActionResult> Vote(FormVote formVote)
        {
            Logger.Init();
            try
            {
                var vote = formVote.ToVote(UserName);
                if (!VoteValidator.VoteIsValid(vote, out var messages))
                {
                    var poll = PollDbManager.GetPollWithId(vote.PollId.ToString());
                    ViewBag.Poll      = poll;
                    ViewBag.UserVoted = poll.HasVoted(UserName);
                    ViewBag.Errors    = messages;
                    return(View(formVote));
                }

                await BcClient.SendVoteToBlockChain(vote);

                PollDbManager.AddVotedUser(formVote.PollGuid, Cryptography.Sha256(UserName));

                return(Redirect("/"));
            }
            catch (Exception e)
            {
                Logger.Log.Error(e);
                return(Redirect("/ControlPanel"));
            }
        }
コード例 #2
0
        public ActionResult Create(FormPoll model)
        {
            Logger.Init();

            try
            {
                var poll = model.ToPoll(UserName);

                if (!PollValidator.PollIsValid(poll, out var message))
                {
                    ModelState.AddModelError("", message);
                    return(View(model));
                }

                PollDbManager.Set(poll);
                ViewBag.Polls = PollDbManager.GetPolls();

                return(Redirect("/ControlPanel"));
            }
            catch (Exception e)
            {
                Logger.Log.Error(e);
                return(Redirect("/ControlPanel"));
            }
        }
コード例 #3
0
        public static bool VoteIsValid(Vote vote, out List <string> messages)
        {
            messages = new List <string>();
            var poll = new PollDbManager().GetPollWithId(vote.PollId.ToString());

            foreach (var question in poll.Questions)
            {
                if (question.Type == QuestionType.Free && vote.CustomOptions[question.Guid] == "")
                {
                    messages.Add($"Не введен ответ в вопросе {question.Title}");
                    continue;
                }

                var selected = question.Options.Select(o => o.Guid).Intersect(vote.SelectedOptions).
                               ToList();
                if ((question.Type == QuestionType.Single || question.Type == QuestionType.Multi) &&
                    selected.Count < 1)
                {
                    messages.Add($"Не выбрана опция в вопросе {question.Title}");
                }

                if (question.Type == QuestionType.Single && selected.Count > 1)
                {
                    messages.Add($"Нельзя выбрать больше одной опции в вопросе {question.Title}");
                }
            }

            return(messages.Count == 0);
        }
コード例 #4
0
        public async Task <ActionResult> Stat(string id)
        {
            Logger.Init();
            try
            {
                var poll  = PollDbManager.GetPollWithId(id);
                var votes = await BcClient.GetVotesFromDate(poll.TimeStamp);

                poll.Votes = votes;
                var stat = new Statistics(poll);
                ViewBag.stat = stat;

                return(View());
            }
            catch (Exception e)
            {
                Logger.Log.Error(e);
                return(Redirect("/ControlPanel"));
            }
        }
コード例 #5
0
        public async Task <ActionResult> Vote(string id)
        {
            Logger.Init();
            try
            {
                var poll = PollDbManager.GetPollWithId(id);

                // TODO Switch to server votecheck logic
                var votes = await BcClient.GetVotesFromDate(poll.TimeStamp);

                poll.Votes   = votes;
                ViewBag.Poll = poll;

                ViewBag.UserVoted = poll.HasVoted(UserName);

                return(View());
            }
            catch (Exception e)
            {
                Logger.Log.Error(e);
                return(Redirect("/ControlPanel"));
            }
        }
コード例 #6
0
 public ControlPanelController(PollValidator pollValidator)
 {
     PollDbManager = new PollDbManager();
 }
コード例 #7
0
 public PollsController(PollValidator pollValidator)
 {
     PollDbManager = new PollDbManager();
     PollValidator = pollValidator;
     BcClient      = new BcClient();
 }