/// <summary>
        /// อัพเดทหรือเพิ่มข้อมูล course catalog
        /// </summary>
        /// <param name="data">ข้อมูลที่ต้องการดำเนินการ</param>
        public async Task UpsertCourseCatalog(CourseCatalog data)
        {
            var update = Builders<CourseCatalog>.Update
               .Set(it => it.GroupName, data.GroupName)
               .Set(it => it.Advertisements, data.Advertisements)
               .Set(it => it.Grade, data.Grade)
               .Set(it => it.SideName, data.SideName)
               .Set(it => it.PriceUSD, data.PriceUSD)
               .Set(it => it.Series, data.Series)
               .Set(it => it.Title, data.Title)
               .Set(it => it.FullDescription, data.FullDescription)
               .Set(it => it.DescriptionImageUrl, data.DescriptionImageUrl)
               .Set(it => it.CreatedDate, data.CreatedDate)
               .Set(it => it.DeletedDate, data.DeletedDate)
               .Set(it => it.Semesters, data.Semesters)
               .Set(it => it.IsFree, data.IsFree)
               .Set(it => it.TotalWeeks, data.TotalWeeks);

            var updateOption = new UpdateOptions { IsUpsert = true };
            await MongoAccess.MongoUtil.Instance
                .GetCollection<CourseCatalog>(AppConfigOptions.CourseCatalogTableName)
               .UpdateOneAsync(it => it.id == data.id, update, updateOption);
        }
        private async Task updateCourseCatalog(IEnumerable <CourseCatalog> allCourseCatalog)
        {
            var courseCatalogRepo = new WebManagementPortal.Repositories.CourseCatalogRepository();

            foreach (var courseCatalog in allCourseCatalog)
            {
                var semesterQry = courseCatalog.Semesters.Where(it => !it.RecLog.DeletedDate.HasValue);
                var unitQry     = semesterQry.SelectMany(it => it.Units).Where(it => !it.RecLog.DeletedDate.HasValue);
                var lessonQry   = unitQry.SelectMany(it => it.Lessons).Where(it => !it.RecLog.DeletedDate.HasValue);

                var lessonIdRunner     = 1;
                var unitIdRunner       = 1;
                var semesterNameRunner = (byte)65;
                var semesters          = semesterQry.Select(semester => new repoModel.CourseCatalog.Semester
                {
                    id          = semester.Id.ToString(),
                    Title       = semester.Title,
                    Description = semester.Description,
                    Name        = string.Format("{0}", (char)semesterNameRunner++),
                    TotalWeeks  = semester.TotalWeeks,
                    Units       = unitQry.Where(unit => unit.SemesterId == semester.Id).Select(unit => new repoModel.CourseCatalog.Unit
                    {
                        id          = unit.Id.ToString(),
                        Title       = unit.Title,
                        Description = unit.Description,
                        Order       = unitIdRunner++,
                        TotalWeeks  = unit.TotalWeeks,
                        Lessons     = lessonQry.Where(it => it.UnitId == unit.Id).Select(lesson =>
                        {
                            var teacherContentQry = lesson.TeacherLessonItems
                                                    .Where(it => !it.RecLog.DeletedDate.HasValue)
                                                    .Select(it => new repoModel.CourseCatalog.LessonContent
                            {
                                ContentURL    = it.ContentURL,
                                ImageUrl      = it.IconURL,
                                Description   = it.Description,
                                IsPreviewable = it.IsPreviewable,
                            });
                            var studentContentQry = lesson.StudentLessonItems
                                                    .Where(it => !it.RecLog.DeletedDate.HasValue)
                                                    .Select(it => new repoModel.CourseCatalog.LessonContent
                            {
                                ContentURL    = it.ContentURL,
                                ImageUrl      = it.IconURL,
                                Description   = it.Description,
                                IsPreviewable = it.IsPreviewable,
                            });
                            var contentQry = teacherContentQry.Union(studentContentQry);
                            return(new repoModel.CourseCatalog.Lesson
                            {
                                id = lesson.Id.ToString(),
                                Order = lessonIdRunner++,
                                Contents = contentQry,
                            });
                        }),
                    }),
                });

                var result = new repoModel.CourseCatalog
                {
                    id                  = courseCatalog.Id.ToString(),
                    GroupName           = courseCatalog.GroupName,
                    Grade               = courseCatalog.Grade.ToString(),
                    Advertisements      = courseCatalog.Advertisements.Split(new string[] { "#;" }, StringSplitOptions.RemoveEmptyEntries),
                    SideName            = courseCatalog.SideName,
                    PriceUSD            = courseCatalog.PriceUSD,
                    Series              = courseCatalog.Series,
                    Title               = courseCatalog.Title,
                    FullDescription     = courseCatalog.FullDescription,
                    DescriptionImageUrl = courseCatalog.DescriptionImageUrl,
                    CreatedDate         = courseCatalog.RecLog.CreatedDate,
                    DeletedDate         = courseCatalog.RecLog.DeletedDate,
                    Semesters           = semesters,
                    TotalWeeks          = courseCatalog.TotalWeeks,
                    IsFree              = courseCatalog.IsFree
                };

                await courseCatalogRepo.UpsertCourseCatalog(result);
            }
        }
        private async Task updateCourseCatalog(IEnumerable<CourseCatalog> allCourseCatalog)
        {
            var courseCatalogRepo = new WebManagementPortal.Repositories.CourseCatalogRepository();
            foreach (var courseCatalog in allCourseCatalog)
            {
                var semesterQry = courseCatalog.Semesters.Where(it => !it.RecLog.DeletedDate.HasValue);
                var unitQry = semesterQry.SelectMany(it => it.Units).Where(it => !it.RecLog.DeletedDate.HasValue);
                var lessonQry = unitQry.SelectMany(it => it.Lessons).Where(it => !it.RecLog.DeletedDate.HasValue);

                var lessonIdRunner = 1;
                var unitIdRunner = 1;
                var semesterNameRunner = (byte)65;
                var semesters = semesterQry.Select(semester => new repoModel.CourseCatalog.Semester
                {
                    id = semester.Id.ToString(),
                    Title = semester.Title,
                    Description = semester.Description,
                    Name = string.Format("{0}", (char)semesterNameRunner++),
                    TotalWeeks = semester.TotalWeeks,
                    Units = unitQry.Where(unit => unit.SemesterId == semester.Id).Select(unit => new repoModel.CourseCatalog.Unit
                    {
                        id = unit.Id.ToString(),
                        Title = unit.Title,
                        Description = unit.Description,
                        Order = unitIdRunner++,
                        TotalWeeks = unit.TotalWeeks,
                        Lessons = lessonQry.Where(it => it.UnitId == unit.Id).Select(lesson =>
                        {
                            var teacherContentQry = lesson.TeacherLessonItems
                               .Where(it => !it.RecLog.DeletedDate.HasValue)
                               .Select(it => new repoModel.CourseCatalog.LessonContent
                               {
                                   ContentURL = it.ContentURL,
                                   ImageUrl = it.IconURL,
                                   Description = it.Description,
                                   IsPreviewable = it.IsPreviewable,
                               });
                            var studentContentQry = lesson.StudentLessonItems
                                .Where(it => !it.RecLog.DeletedDate.HasValue)
                                .Select(it => new repoModel.CourseCatalog.LessonContent
                                {
                                    ContentURL = it.ContentURL,
                                    ImageUrl = it.IconURL,
                                    Description = it.Description,
                                    IsPreviewable = it.IsPreviewable,
                                });
                            var contentQry = teacherContentQry.Union(studentContentQry);
                            return new repoModel.CourseCatalog.Lesson
                            {
                                id = lesson.Id.ToString(),
                                Order = lessonIdRunner++,
                                Contents = contentQry,
                            };
                        }),
                    }),
                });

                var result = new repoModel.CourseCatalog
                {
                    id = courseCatalog.Id.ToString(),
                    GroupName = courseCatalog.GroupName,
                    Grade = courseCatalog.Grade.ToString(),
                    Advertisements = courseCatalog.Advertisements.Split(new string[] { "#;" }, StringSplitOptions.RemoveEmptyEntries),
                    SideName = courseCatalog.SideName,
                    PriceUSD = courseCatalog.PriceUSD,
                    Series = courseCatalog.Series,
                    Title = courseCatalog.Title,
                    FullDescription = courseCatalog.FullDescription,
                    DescriptionImageUrl = courseCatalog.DescriptionImageUrl,
                    CreatedDate = courseCatalog.RecLog.CreatedDate,
                    DeletedDate = courseCatalog.RecLog.DeletedDate,
                    Semesters = semesters,
                    TotalWeeks = courseCatalog.TotalWeeks,
                    IsFree = courseCatalog.IsFree
                };

                await courseCatalogRepo.UpsertCourseCatalog(result);
            }
        }