private void MakeActiveSection(IUserSection userSection) { var sectionId = default(int); switch (userSection) { case UserSection _: sectionId = userSection.Id.GetValueOrDefault(); break; case UserSectionGroup group: var selectedSection = SelectSection(group); sectionId = selectedSection.Id.GetValueOrDefault(); break; } var section = Sections.Find(sectionId) ?? throw new SectionNotFoundException(sectionId); var sectionMarker = new TestSectionMarker { TestId = userSection.TestId.GetValueOrDefault(), Section = section, SectionId = sectionId, Started = DateTime.UtcNow }; TestSectionMarkers.Add(sectionMarker); _dbContext.SaveChanges(); }
private async Task MakeActiveSectionAsync(IUserSection userSection, CancellationToken cancellationToken = default(CancellationToken)) { var sectionId = default(int); switch (userSection) { case UserSection _: sectionId = userSection.Id.GetValueOrDefault(); break; case UserSectionGroup group: var selectedSection = SelectSection(group); sectionId = selectedSection.Id.GetValueOrDefault(); break; } var section = await Sections.FindAsync(cancellationToken, sectionId) ?? throw new SectionNotFoundException(sectionId); var sectionMarker = new TestSectionMarker { TestId = userSection.TestId.GetValueOrDefault(), Section = section, SectionId = sectionId, Started = DateTime.UtcNow }; TestSectionMarkers.Add(sectionMarker); await _dbContext.SaveChangesAsync(cancellationToken); }
private static UserSectionGroup GenerateSectionGroup(IUserSection sectionSeed) { var sectionId = sectionSeed.Id; var subSections = Enumerable .Range(0, 5) .Select(i => { return(Fixture .Build <UserSection>() .With(us => us.UserId, sectionSeed.UserId) .With(us => us.SurveyId, sectionSeed.SurveyId) .With(us => us.TestId, sectionSeed.TestId) .With(us => us.Order, sectionSeed.Order) .With(us => us.Id, sectionId++) .With(us => us.Modified, sectionSeed.Modified) .Without(us => us.TestSectionMarkerId) .Without(us => us.Started) .Without(us => us.Completed) .Create()); }).ToList(); var activeSubSection = sectionSeed.Started.HasValue ? subSections.Shuffle().First() : null; subSections = subSections.Select(ss => { if (ss.Id != activeSubSection?.Id) { return(ss); } ss.Started = sectionSeed.Started; ss.Completed = sectionSeed.Completed; ss.Modified = sectionSeed.Modified; ss.TestSectionMarkerId = ss.Started.HasValue ? Fixture.Create <Guid?>() : null; return(ss); }).ToList(); sectionSeed.Id = sectionId; return(new UserSectionGroup(subSections)); }