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); }
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 })); }
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); }