public UserSurvey CompleteCurrentSection(string userId, int surveyId) { var section = _surveyManager.GetCurrentUserSection(userId, surveyId); var sectionMarker = TestSectionMarkers.SingleOrDefault(tsm => tsm.TestId == section.TestId && tsm.SectionId == section.Id) ?? throw new SectionMarkerNotFoundException(section.TestId.GetValueOrDefault(), section.Id.GetValueOrDefault()); sectionMarker.Completed = DateTime.UtcNow; // Check if the survey is complete var sectionMarkers = from s in Sections join tsm in TestSectionMarkers on s.Id equals tsm.SectionId into sm from m in sm.DefaultIfEmpty() select new { SectionId = s.Id, m.Completed }; if (sectionMarkers.All(sm => sm.Completed.HasValue)) { var test = Tests.SingleOrDefault(t => t.UserId.Equals(userId, StringComparison.OrdinalIgnoreCase) && t.SurveyId == surveyId) ?? throw new TestNotFoundException(userId, surveyId); test.Completed = DateTime.UtcNow; } _dbContext.SaveChanges(); var result = _surveyManager.GetUserSurvey(userId, surveyId); return(result); }