Ejemplo n.º 1
0
        public async Task <Poll> CreateAsync(PollCreateInputModel pollCreateInputModel, string creatorId)
        {
            var lastActive = await this.db.Polls.SingleOrDefaultAsync(p => !p.IsDeleted && p.IsActive);

            if (lastActive != null)
            {
                lastActive.IsActive = false;
            }

            var poll = new Poll
            {
                Question  = pollCreateInputModel.Question,
                CreatorId = creatorId,
                IsActive  = true,
                IsDeleted = false,
                CreatedOn = DateTime.UtcNow,
                Options   = pollCreateInputModel.Options?.Select(o => new PollOption {
                    Value = o
                }).ToList()
            };

            await this.db.Polls.AddAsync(poll);

            await this.db.SaveChangesAsync();

            return(poll);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create(PollCreateInputModel pollCreateInputModel)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(pollCreateInputModel));
            }

            var userId  = this.userManager.GetUserId(this.User);
            var article = await this.pollService.CreateAsync(pollCreateInputModel, userId);

            return(RedirectToAction("Details", "Polls", new { article.Id }));
        }
Ejemplo n.º 3
0
        public async Task CreateAsync_ShouldAddAllOptions()
        {
            var pollService = new PollService(this.dbContext);

            var creatorId    = Guid.NewGuid().ToString();
            var pollToCreate = new PollCreateInputModel {
                Options = new List <string>()
                {
                    "1", "2", "3"
                }
            };


            await pollService.CreateAsync(pollToCreate, creatorId);

            var pollFromDb = this.dbContext.Polls.FirstOrDefault();

            Assert.True(pollFromDb?.Options.Count == pollToCreate.Options.Count);
        }