public async Task <CourseExportResults> UpdateCourse(Course course, CourseUpdateOptions updateOptions) { var results = new CourseExportResults(course.Id, updateOptions.StepikCourseId); try { await TryUpdateCourse(course, updateOptions, results).ConfigureAwait(false); } catch (Exception e) { results.Exception = e; } return(results); }
private async Task TryUpdateCourse(Course course, CourseUpdateOptions updateOptions, CourseExportResults results) { results.Info($"Downloading stepik course #{updateOptions.StepikCourseId}"); var stepikCourse = await client.GetCourse(updateOptions.StepikCourseId).ConfigureAwait(false); results.StepikCourseTitle = stepikCourse.Title; results.Info($"Downloading stepik sections for it ({string.Join(", ", stepikCourse.SectionsIds)})"); var stepikSections = new Dictionary <int, StepikApiSection>(); // TODO (andgein): download multiple sections in one request foreach (var sectionId in stepikCourse.SectionsIds) { stepikSections[sectionId] = await client.GetSection(sectionId).ConfigureAwait(false); } var stepikUnitsIds = stepikSections.SelectMany(kvp => kvp.Value.UnitsIds).ToList(); results.Info($"Downloading stepik units ({string.Join(", ", stepikUnitsIds)}) and lessons for them"); var stepikUnits = new Dictionary <int, StepikApiUnit>(); foreach (var unitId in stepikUnitsIds) { stepikUnits[unitId] = await client.GetUnit(unitId).ConfigureAwait(false); } var stepikLessonsIds = stepikUnits.Select(kvp => kvp.Value.LessonId); var stepikLessons = new Dictionary <int, StepikApiLesson>(); foreach (var lessonId in stepikLessonsIds) { stepikLessons[lessonId] = await client.GetLesson(lessonId).ConfigureAwait(false); } var sectionIndex = stepikCourse.SectionsIds.Count; foreach (var slideUpdateOptions in updateOptions.SlidesUpdateOptions) { var slideId = slideUpdateOptions.SlideId; var slide = course.FindSlideById(slideId); if (slide == null) { results.Error($"Unable to find slide {slideId}, continue without it"); continue; } results.Info($"Updating slide «{slide.Title}» with id {slide.Id}"); var stepId = slideUpdateOptions.InsertAfterStep; StepikApiLesson lesson; int position; if (stepId != -1 && stepikLessons.Values.Any(l => l.StepsIds.Contains(stepId))) { var lessonId = stepikLessons.FirstOrDefault(kvp => kvp.Value.StepsIds.Contains(stepId)).Key; // Re-download lesson because it can be changed for a while lesson = await client.GetLesson(lessonId).ConfigureAwait(false); position = lesson.StepsIds.FindIndex(stepId); results.Info($"Removing old steps created for this slide: {string.Join(", ", slideUpdateOptions.RemoveStepsIds)}"); await RemoveStepsFromLesson(lesson, slideUpdateOptions.RemoveStepsIds, results).ConfigureAwait(false); } else { results.Info("Creating new stepik lesson for this slide"); lesson = await CreateLessonAndSectionForSlide(slide, stepikCourse, ++sectionIndex).ConfigureAwait(false); position = 0; } results.Info($"Inserting steps for slide «{slide.Title}» into lesson {lesson.Id} on position {position}"); await InsertSlideAsStepsInLesson(course, slide, lesson.Id.Value, position + 1, updateOptions, results); } }