private async Task updateLessonCatalog(IEnumerable <CourseCatalog> allCourseCatalog)
        {
            var lessonCatalogRepo = new WebManagementPortal.Repositories.LessonCatalogRepository();

            foreach (var courseCatalog in allCourseCatalog)
            {
                var unitOrderRunner    = 1;
                var lessonOrderRunner  = 1;
                var semesterNameRunner = (byte)65;
                foreach (var semester in courseCatalog.Semesters.Where(it => !it.RecLog.DeletedDate.HasValue))
                {
                    foreach (var unit in semester.Units.Where(it => !it.RecLog.DeletedDate.HasValue))
                    {
                        foreach (var lesson in unit.Lessons.Where(it => !it.RecLog.DeletedDate.HasValue))
                        {
                            var adsQry = lesson.Advertisements.Where(it => !it.RecLog.DeletedDate.HasValue).Select(it => new repoModel.LessonCatalog.Ads
                            {
                                id          = it.Id.ToString(),
                                ImageUrl    = it.ImageUrl,
                                LinkUrl     = it.LinkUrl,
                                CreatedDate = it.RecLog.CreatedDate,
                                DeletedDate = it.RecLog.DeletedDate
                            });
                            var totdQry = lesson.TopicOfTheDays.Where(it => !it.RecLog.DeletedDate.HasValue).Select(it => new repoModel.LessonCatalog.TopicOfTheDay
                            {
                                id          = it.Id.ToString(),
                                Message     = it.Message,
                                SendOnDay   = it.SendOnDay,
                                CreatedDate = it.RecLog.CreatedDate,
                                DeletedDate = it.RecLog.DeletedDate
                            });
                            var teacherItemQry    = createLessonItems(lesson.TeacherLessonItems);
                            var studentItemQry    = createLessonItems(lesson.StudentLessonItems);
                            var preAssessmentQry  = createAssessmentItems(lesson.PreAssessments);
                            var postAssessmentQry = createAssessmentItems(lesson.PostAssessments);
                            var lessonCatalog     = new repoModel.LessonCatalog
                            {
                                id              = lesson.Id.ToString(),
                                Order           = lessonOrderRunner++,
                                Title           = lesson.Title,
                                UnitNo          = unitOrderRunner,
                                SemesterName    = string.Format("{0}", (char)semesterNameRunner),
                                TeacherItems    = teacherItemQry,
                                StudentItems    = studentItemQry,
                                PreAssessments  = preAssessmentQry,
                                PostAssessments = postAssessmentQry,
                                CourseCatalogId = courseCatalog.Id.ToString(),
                                CreatedDate     = lesson.RecLog.CreatedDate,
                                DeletedDate     = lesson.RecLog.DeletedDate,
                                Advertisments   = adsQry,
                                TopicOfTheDays  = totdQry
                            };
                            await lessonCatalogRepo.UpsertLessonCatalog(lessonCatalog);
                        }
                        unitOrderRunner++;
                    }
                    semesterNameRunner++;
                }
            }
        }
        /// <summary>
        /// อัพเดทหรือเพิ่มข้อมูล lesson catalog
        /// </summary>
        /// <param name="lessonCatalogId">ข้อมูลที่ต้องการดำเนินการ</param>
        public async Task UpsertLessonCatalog(LessonCatalog data)
        {
            var update = Builders<LessonCatalog>.Update
              .Set(it => it.Order, data.Order)
              .Set(it => it.Title, data.Title)
              .Set(it => it.UnitNo, data.UnitNo)
              .Set(it => it.SemesterName, data.SemesterName)
              .Set(it => it.CourseCatalogId, data.CourseCatalogId)
              .Set(it => it.CreatedDate, data.CreatedDate)
              .Set(it => it.DeletedDate, data.DeletedDate)
              .Set(it => it.Advertisments, data.Advertisments)
              .Set(it => it.TopicOfTheDays, data.TopicOfTheDays)
              .Set(it => it.TeacherItems, data.TeacherItems)
              .Set(it => it.StudentItems, data.StudentItems)
              .Set(it => it.PreAssessments, data.PreAssessments)
              .Set(it => it.PostAssessments, data.PostAssessments);

            var updateOption = new UpdateOptions { IsUpsert = true };
            await MongoAccess.MongoUtil.Instance
                .GetCollection<LessonCatalog>(AppConfigOptions.LessonCatalogTableName)
               .UpdateOneAsync(it => it.id == data.id, update, updateOption);
        }
 private async Task updateLessonCatalog(IEnumerable<CourseCatalog> allCourseCatalog)
 {
     var lessonCatalogRepo = new WebManagementPortal.Repositories.LessonCatalogRepository();
     foreach (var courseCatalog in allCourseCatalog)
     {
         var unitOrderRunner = 1;
         var lessonOrderRunner = 1;
         var semesterNameRunner = (byte)65;
         foreach (var semester in courseCatalog.Semesters.Where(it => !it.RecLog.DeletedDate.HasValue))
         {
             foreach (var unit in semester.Units.Where(it => !it.RecLog.DeletedDate.HasValue))
             {
                 foreach (var lesson in unit.Lessons.Where(it => !it.RecLog.DeletedDate.HasValue))
                 {
                     var adsQry = lesson.Advertisements.Where(it => !it.RecLog.DeletedDate.HasValue).Select(it => new repoModel.LessonCatalog.Ads
                     {
                         id = it.Id.ToString(),
                         ImageUrl = it.ImageUrl,
                         LinkUrl = it.LinkUrl,
                         CreatedDate = it.RecLog.CreatedDate,
                         DeletedDate = it.RecLog.DeletedDate
                     });
                     var totdQry = lesson.TopicOfTheDays.Where(it => !it.RecLog.DeletedDate.HasValue).Select(it => new repoModel.LessonCatalog.TopicOfTheDay
                     {
                         id = it.Id.ToString(),
                         Message = it.Message,
                         SendOnDay = it.SendOnDay,
                         CreatedDate = it.RecLog.CreatedDate,
                         DeletedDate = it.RecLog.DeletedDate
                     });
                     var teacherItemQry = createLessonItems(lesson.TeacherLessonItems);
                     var studentItemQry = createLessonItems(lesson.StudentLessonItems);
                     var preAssessmentQry = createAssessmentItems(lesson.PreAssessments);
                     var postAssessmentQry = createAssessmentItems(lesson.PostAssessments);
                     var lessonCatalog = new repoModel.LessonCatalog
                     {
                         id = lesson.Id.ToString(),
                         Order = lessonOrderRunner++,
                         Title = lesson.Title,
                         UnitNo = unitOrderRunner,
                         SemesterName = string.Format("{0}", (char)semesterNameRunner),
                         TeacherItems = teacherItemQry,
                         StudentItems = studentItemQry,
                         PreAssessments = preAssessmentQry,
                         PostAssessments = postAssessmentQry,
                         CourseCatalogId = courseCatalog.Id.ToString(),
                         CreatedDate = lesson.RecLog.CreatedDate,
                         DeletedDate = lesson.RecLog.DeletedDate,
                         Advertisments = adsQry,
                         TopicOfTheDays = totdQry
                     };
                     await lessonCatalogRepo.UpsertLessonCatalog(lessonCatalog);
                 }
                 unitOrderRunner++;
             }
             semesterNameRunner++;
         }
     }
 }