예제 #1
0
        public async Task Should_Create_MultipleChoicePoll()
        {
            var completedPoll = new AuthorityPoll
            {
                Name         = "Authority Poll",
                Active       = false,
                CreateTime   = DateTime.UtcNow.AddDays(-1),
                Deadline     = DateTime.UtcNow.AddDays(-1),
                QuestionBody = "test"
            };

            _context.AuthorityPolls.Add(completedPoll);
            _context.SaveChanges();

            var model = new MultipleChoicePollViewModel
            {
                Name    = "test",
                Options = new List <string> {
                    "a", "b", "c"
                }
            };
            var result = await _controller.NewMultipleChoicePoll(model);

            var actionResult    = Assert.IsType <OkObjectResult>(result);
            var actionResultObj = actionResult.Value as MultipleChoicePollViewModel;
            var poll            = _context.MultipleChoicePolls.ToList()[0];

            Assert.Equal(actionResultObj.PollId, poll.Id);
            Assert.Equal(actionResultObj.Name, poll.Name);
            Assert.Equal(actionResultObj.Options, JsonConvert.DeserializeObject <List <string> >(poll.OptionsJsonString));
        }
예제 #2
0
        public async Task Should_Return_Error_If_OptionsCountLessThen2()
        {
            var completedPoll = new AuthorityPoll
            {
                Name         = "Authority Poll",
                Active       = false,
                CreateTime   = DateTime.UtcNow.AddDays(-1),
                Deadline     = DateTime.UtcNow.AddDays(-1),
                QuestionBody = "test"
            };

            _context.AuthorityPolls.Add(completedPoll);
            _context.SaveChanges();

            var key             = "PollOptionCountError";
            var localizedString = new LocalizedString(key, key);

            _multipleChoiceLocalizerMock.Setup(_ => _[key]).Returns(localizedString);
            var model = new MultipleChoicePollViewModel
            {
                Name    = "test",
                Options = new List <string> {
                    "a"
                }
            };
            var result = await _controller.NewMultipleChoicePoll(model);

            var actionResult     = Assert.IsType <BadRequestObjectResult>(result);
            var actionResultList = actionResult.Value as List <ErrorViewModel>;

            Assert.Equal(key, actionResultList[0].Description);
        }
예제 #3
0
        public MultipleChoicePollViewModel MultipleChoicePollToViewModel(MultipleChoicePoll poll)
        {
            MultipleChoicePollViewModel model = null;

            if (poll != null)
            {
                model = _mapper.Map <MultipleChoicePoll, MultipleChoicePollViewModel>(poll);
            }

            return(model);
        }
예제 #4
0
        public async Task <Poll> NewMultipleChoicePoll(MultipleChoicePollViewModel model)
        {
            var poll = new MultipleChoicePoll
            {
                UserId            = model.UserId,
                CreateTime        = DateTime.UtcNow,
                Active            = true,
                Name              = model.Name,
                QuestionBody      = model.Description,
                OptionsJsonString = JsonConvert.SerializeObject(model.Options),
                TenantId          = _tenantProvider.GetTenantId()
            };
            await _pollService.AddPoll(poll);

            return(poll);
        }
예제 #5
0
        public async Task Should_Return_Error_If_AuthorityPoll_IsNotCompleted()
        {
            var key             = "CantStartPollBeforeAuthorityComplete";
            var localizedString = new LocalizedString(key, key);

            _multipleChoiceLocalizerMock.Setup(_ => _[key]).Returns(localizedString);

            var model = new MultipleChoicePollViewModel
            {
                Name = "test"
            };
            var result = await _controller.NewMultipleChoicePoll(model);

            var actionResult     = Assert.IsType <BadRequestObjectResult>(result);
            var actionResultList = actionResult.Value as List <ErrorViewModel>;

            Assert.Equal(key, actionResultList[0].Description);
        }
        public async Task <IActionResult> NewMultipleChoicePoll([FromBody] MultipleChoicePollViewModel model)
        {
            if (ModelState.IsValid)
            {
                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)));
                }

                if (model.Options.Count < 2 || model.Options.Any(string.IsNullOrEmpty))
                {
                    ModelState.AddModelError("", _localizer["PollOptionCountError"]);
                    return(BadRequest(Errors.GetErrorList(ModelState)));
                }

                var poll = await _pollViewModelService.NewMultipleChoicePoll(model);

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

                var result = _pollViewModelService.MultipleChoicePollToViewModel((MultipleChoicePoll)poll);
                return(Ok(result));
            }

            return(BadRequest(Errors.GetErrorList(ModelState)));
        }
        public async Task Should_Create_MultipleChoicePoll()
        {
            var model = new MultipleChoicePollViewModel
            {
                Name    = "test",
                Options = new List <string> {
                    "a", "b", "c"
                },
                Description = "test dec"
            };
            var poll = await _pollApiViewModelService.NewMultipleChoicePoll(model);

            var result = _context.MultipleChoicePolls.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);
        }