Exemple #1
0
        public async Task <IActionResult> Vote([FromBody] SharePollVoteModel model)
        {
            if (ModelState.IsValid)
            {
                var poll = await _pollService.GetPoll(model.PollId);

                var userId = User.ApiGetUserId();
                if (poll == null)
                {
                    ModelState.AddModelError("", _localizer["PollNotFound"]);
                    return(BadRequest(Errors.GetErrorList(ModelState)));
                }

                if (poll.Deadline <= DateTime.UtcNow)
                {
                    ModelState.AddModelError("", _localizer["PollCompleted"]);
                    return(BadRequest(Errors.GetErrorList(ModelState)));
                }

                if (!await _pollService.UserCanVote(poll.Id, User.ApiGetUserId()))
                {
                    return(BadRequest(Errors.GetSingleErrorList("", _localizer["UserCannotVoteAfterAddedPollStart"])));
                }

                if (!await _userService.IsVoter(User.ApiGetUserId()))
                {
                    return(BadRequest(Errors.GetSingleErrorList("", _localizer["PollVoterError"])));
                }

                //check if the user voted in poll
                var userVoted = await _voteService.UserVotedInPoll(userId, model.PollId);

                if (userVoted)
                {
                    ModelState.AddModelError("", _localizer["PollRecordExist"]);
                    return(BadRequest(Errors.GetErrorList(ModelState)));
                }

                if (model.Options.Any(v => v.SharePercent < 0 || v.SharePercent > 100))
                {
                    ModelState.AddModelError("", _localizer["InvalidVoteError"]);
                    return(BadRequest(Errors.GetErrorList(ModelState)));
                }

                if (model.Options.Sum(v => v.SharePercent) != 100)
                {
                    ModelState.AddModelError("", _localizer["VoteSumError"]);
                    return(BadRequest(Errors.GetErrorList(ModelState)));
                }

                model.UserId = userId;
                await _pollViewModelService.SaveSharePollVotes(model);

                var result = _mapper.Map <Poll, PollListViewModel>(poll);
                result.UserVoted = true;
                return(Ok(result));
            }

            return(BadRequest(Errors.GetErrorList(ModelState)));
        }
        public async Task Should_SaveSalaryVotes()
        {
            var options = new List <SharePollVoteValuesModel>
            {
                new SharePollVoteValuesModel
                {
                    Option       = "a",
                    SharePercent = 40
                },
                new SharePollVoteValuesModel
                {
                    Option       = "b",
                    SharePercent = 60
                }
            };
            var model = new SharePollVoteModel
            {
                PollId  = 1,
                UserId  = 1.ToString(),
                Options = options
            };

            await _pollApiViewModelService.SaveSharePollVotes(model);

            var result = _context.Votes.Where(x => x.PollId == 1);

            Assert.Equal(2, result.Count());
            Assert.Equal(new[] { "a", "b" }, result.Select(r => r.VotedUserId));
        }