Beispiel #1
0
        public async Task <AuthorityPoll> NewAuthorityPoll(AuthorityPollViewModel model)
        {
            var poll = new AuthorityPoll
            {
                UserId     = model.UserId,
                CreateTime = DateTime.UtcNow,
                Active     = true,
                Name       = model.Name,
                TenantId   = _tenantProvider.GetTenantId()
            };
            await _pollService.AddPoll(poll);

            return(poll);
        }
        public async Task Should_Create_AuthorityPoll()
        {
            var model = new AuthorityPollViewModel
            {
                Name   = "test",
                UserId = 1.ToString()
            };
            var poll = await _pollApiViewModelService.NewAuthorityPoll(model);

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

            Assert.NotNull(result);
            Assert.Equal(model.Name, poll.Name);
            Assert.Equal(model.UserId, poll.UserId);
        }
        public async Task <IActionResult> NewAuthorityPoll()
        {
            var model = new AuthorityPollViewModel();

            if (ModelState.ContainsKey("Name"))
            {
                ModelState["Name"].Errors.Clear();
            }
            var pollCount = await _pollService.GetPollCountByType <AuthorityPoll>() + 1;

            model.Name = $"{pollCount}.{_localizer["AuthorityDistPoll"]}";

            model.UserId = User.ApiGetUserId();
            var adminRole = await _userService.UserInRole(User.ApiGetUserId(), "Admin");

            if (!adminRole)
            {
                ModelState.AddModelError("", _localizer["AuthorityPollUserAdminError"]);
                return(BadRequest(Errors.GetErrorList(ModelState)));
            }

            if (await _userService.GetMaxInitialAuthorityPercent() > 1 / 3M)
            {
                ModelState.AddModelError("", _localizer["AuthorityPollUserInitialAuthorityError"]);
                return(BadRequest(Errors.GetErrorList(ModelState)));
            }

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

            var poll = await _pollViewModelService.NewAuthorityPoll(model);

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

            return(Ok(_mapper.Map <AuthorityPoll, PollListViewModel>(poll)));
        }