Exemple #1
0
        public async Task <IActionResult> NewSharePoll([FromBody] SharePollViewModel model)
        {
            model.UserId = User.ApiGetUserId();

            var latestAuthorityPoll = await _pollService.GetLastPollOfType <AuthorityPoll>();

            if (latestAuthorityPoll == null)
            {
                ModelState.AddModelError("", _localizer["CantStartPollBeforeAuthorityComplete"]);
                return(BadRequest(Errors.GetErrorList(ModelState)));
            }

            if (latestAuthorityPoll.Result == "Kararsız" ||
                latestAuthorityPoll.Result == PollResults.Undecided.ToString())
            {
                ModelState.AddModelError("", _localizer["CantStartPollBeforeAuthorityComplete"]);
                return(BadRequest(Errors.GetErrorList(ModelState)));
            }

            if (await _pollService.HasActivePollOfType <AuthorityPoll>())
            {
                ModelState.AddModelError("", _localizer["AuthorityPollActivePollError"]);
                return(BadRequest(Errors.GetErrorList(ModelState)));
            }

            var poll = await _pollViewModelService.NewSharePoll(model);

            await _pollService.NotifyUsers(poll.PollType, PollNotificationTypes.Started, poll);

            return(Ok(_mapper.Map <SharePoll, PollListViewModel>((SharePoll)poll)));
        }
        public async Task Should_Create_SharePoll()
        {
            var model = new SharePollViewModel
            {
                Name    = "test",
                Options = new List <string> {
                    "a", "b", "c"
                },
                Description = "test dec"
            };
            var poll = await _pollApiViewModelService.NewSharePoll(model);

            var result = _context.SharePolls.FirstOrDefault(x => x.Id == poll.Id);

            Assert.NotNull(result);
            Assert.Equal(model.Name, result.Name);
            Assert.Equal(JsonConvert.SerializeObject(model.Options), result.OptionsJsonString);
            Assert.Equal(model.Description, result.QuestionBody);
        }