/// <summary> /// อัพเดทข้อมูล Class room /// </summary> /// <param name="data">ข้อมูลที่จะทำการอัพเดท</param> public async Task UpsertClassRoom(ClassRoom data) { var update = Builders<ClassRoom>.Update .Set(it => it.Name, data.Name) .Set(it => it.CourseCatalogId, data.CourseCatalogId) .Set(it => it.CreatedDate, data.CreatedDate) .Set(it => it.DeletedDate, data.DeletedDate) .Set(it => it.Message, data.Message) .Set(it => it.IsPublic, data.IsPublic) .Set(it => it.Lessons, data.Lessons) .Set(it => it.LastUpdatedMessageDate, data.LastUpdatedMessageDate); var updateOption = new UpdateOptions { IsUpsert = true }; await MongoAccess.MongoUtil.Instance.GetCollection<ClassRoom>(AppConfigOptions.ClassRoomTableName) .UpdateOneAsync(it => it.id == data.id, update, updateOption); }
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); } }