private async Task createPublicClassRooms(IEnumerable <CourseCatalog> allCourseCatalog) { var courseCatalogIds = allCourseCatalog.Select(it => it.Id.ToString()).Distinct(); var classRoomRepo = new WebManagementPortal.Repositories.ClassRoomRepository(); var publicClassRooms = (await classRoomRepo.GetPublicClassRoomByCourseCatalogId(courseCatalogIds)).ToList(); foreach (var publicClassRoom in publicClassRooms) { var coursCatalog = allCourseCatalog.FirstOrDefault(it => it.Id.ToString() == publicClassRoom.id); if (coursCatalog == null) { continue; } publicClassRoom.Name = coursCatalog.SideName; publicClassRoom.DeletedDate = coursCatalog.RecLog.DeletedDate; var lessonQry = coursCatalog.Semesters .Where(it => !it.RecLog.DeletedDate.HasValue) .SelectMany(it => it.Units) .Where(it => !it.RecLog.DeletedDate.HasValue) .SelectMany(it => it.Lessons) .Where(it => !it.RecLog.DeletedDate.HasValue) .Select(lesson => { var existingLesson = publicClassRoom.Lessons.FirstOrDefault(l => l.LessonCatalogId == lesson.Id.ToString()); if (existingLesson != null) { // Update Lesson in the public ClassRoom return(new repoModel.ClassRoom.Lesson { id = existingLesson.id, TotalLikes = existingLesson.TotalLikes, LessonCatalogId = lesson.Id.ToString(), }); } else { // Create new lesson return(new repoModel.ClassRoom.Lesson { id = Guid.NewGuid().ToString(), LessonCatalogId = lesson.Id.ToString() }); } }); publicClassRoom.Lessons = lessonQry.ToList(); await classRoomRepo.UpsertClassRoom(publicClassRoom); } var needToCreateClassRooms = allCourseCatalog.Where(it => publicClassRooms.All(p => p.id != it.Id.ToString())); foreach (var courseCatalog in needToCreateClassRooms) { var lessonQry = courseCatalog.Semesters .Where(it => !it.RecLog.DeletedDate.HasValue) .SelectMany(it => it.Units) .Where(it => !it.RecLog.DeletedDate.HasValue) .SelectMany(it => it.Lessons) .Where(it => !it.RecLog.DeletedDate.HasValue) .Select(it => new repoModel.ClassRoom.Lesson { id = Guid.NewGuid().ToString(), LessonCatalogId = it.Id.ToString(), }); var classRoom = new repoModel.ClassRoom { id = courseCatalog.Id.ToString(), Name = courseCatalog.SideName, CourseCatalogId = courseCatalog.Id.ToString(), CreatedDate = courseCatalog.RecLog.CreatedDate, DeletedDate = courseCatalog.RecLog.DeletedDate, IsPublic = true, Lessons = lessonQry }; await classRoomRepo.UpsertClassRoom(classRoom); } }
private async Task createPublicClassRooms(IEnumerable<CourseCatalog> allCourseCatalog) { var courseCatalogIds = allCourseCatalog.Select(it => it.Id.ToString()).Distinct(); var classRoomRepo = new WebManagementPortal.Repositories.ClassRoomRepository(); var publicClassRooms = (await classRoomRepo.GetPublicClassRoomByCourseCatalogId(courseCatalogIds)).ToList(); foreach (var publicClassRoom in publicClassRooms) { var coursCatalog = allCourseCatalog.FirstOrDefault(it => it.Id.ToString() == publicClassRoom.id); if (coursCatalog == null) continue; publicClassRoom.Name = coursCatalog.SideName; publicClassRoom.DeletedDate = coursCatalog.RecLog.DeletedDate; var lessonQry = coursCatalog.Semesters .Where(it => !it.RecLog.DeletedDate.HasValue) .SelectMany(it => it.Units) .Where(it => !it.RecLog.DeletedDate.HasValue) .SelectMany(it => it.Lessons) .Where(it => !it.RecLog.DeletedDate.HasValue) .Select(lesson => { var existingLesson = publicClassRoom.Lessons.FirstOrDefault(l => l.LessonCatalogId == lesson.Id.ToString()); if (existingLesson != null) { // Update Lesson in the public ClassRoom return new repoModel.ClassRoom.Lesson { id = existingLesson.id, TotalLikes = existingLesson.TotalLikes, LessonCatalogId = lesson.Id.ToString(), }; } else { // Create new lesson return new repoModel.ClassRoom.Lesson { id = Guid.NewGuid().ToString(), LessonCatalogId = lesson.Id.ToString() }; } }); publicClassRoom.Lessons = lessonQry.ToList(); await classRoomRepo.UpsertClassRoom(publicClassRoom); } var needToCreateClassRooms = allCourseCatalog.Where(it => publicClassRooms.All(p => p.id != it.Id.ToString())); foreach (var courseCatalog in needToCreateClassRooms) { var lessonQry = courseCatalog.Semesters .Where(it => !it.RecLog.DeletedDate.HasValue) .SelectMany(it => it.Units) .Where(it => !it.RecLog.DeletedDate.HasValue) .SelectMany(it => it.Lessons) .Where(it => !it.RecLog.DeletedDate.HasValue) .Select(it => new repoModel.ClassRoom.Lesson { id = Guid.NewGuid().ToString(), LessonCatalogId = it.Id.ToString(), }); var classRoom = new repoModel.ClassRoom { id = courseCatalog.Id.ToString(), Name = courseCatalog.SideName, CourseCatalogId = courseCatalog.Id.ToString(), CreatedDate = courseCatalog.RecLog.CreatedDate, DeletedDate = courseCatalog.RecLog.DeletedDate, IsPublic = true, Lessons = lessonQry }; await classRoomRepo.UpsertClassRoom(classRoom); } }