public async Task <IActionResult> Edit(ModChapterModel model, ModChapterEntity item)
        {
            ViewBag.Title = "Cập nhật";
            if (string.IsNullOrEmpty(model.ID))
            {
                if (!string.IsNullOrEmpty(model.CourseID) && model.CourseID != "0")
                {
                    return(RedirectToAction("Detail", "ModCourses", new { ID = model.CourseID }));
                }
                else
                {
                    return(RedirectToAction("Index", "ModCourses"));
                }
            }
            else
            {
                var _item = _service.GetByID(model.ID);
                if (_item == null)
                {
                    SetMessageError("Không tìm thấy chương");
                    return(RedirectToAction("Detail", "ModCourses", new { ID = item.CourseID }));
                }
                if (!string.IsNullOrEmpty(item.Name))
                {
                    _item.Name = item.Name;
                }
                _item.Updated  = DateTime.Now;
                _item.ParentID = item.ParentID;
                await _service.AddAsync(_item);

                SetMessageSuccess("Cập nhật thành công");
                return(RedirectToAction("Detail", "ModChapters", new { ID = item.ID }));
            }
        }
        public IActionResult Detail(ModChapterModel model)
        {
            ViewBag.Title = "Nội dung chương";

            ViewData.Model = model;
            var item = new ModChapterEntity();

            if (string.IsNullOrEmpty(model.ID))
            {
                if (!string.IsNullOrEmpty(model.CourseID) && model.CourseID != "0")
                {
                    return(RedirectToAction("Detail", "ModCourses", new { ID = model.CourseID }));
                }
                else
                {
                    return(RedirectToAction("Index", "ModCourses"));
                }
            }
            else
            {
                item           = _service.GetByID(model.ID);
                model.CourseID = item.CourseID;
                if (item == null)
                {
                    SetMessageError("Không tìm thấy chương");
                    return(RedirectToAction("Detail", "ModCourses", new { ID = item.CourseID }));
                }
                ViewBag.Data = item;
            }

            var course        = _courseService.GetByID(item.CourseID);
            var lessonsCourse = _lessonService.CreateQuery().Find(o => o.CreateUser == _currentUser.ID && o.IsParentCourse == true && o.CourseID == item.CourseID).ToList();

            ViewBag.LessonCourse = lessonsCourse;
            if (course == null)
            {
                return(RedirectToAction("Index", "ModCourses"));
            }
            ViewBag.Course = course;

            var chapters = _service.Find(true, o => o.CourseID == item.CourseID);

            ViewBag.Chapters = chapters.Select(o => new ModChapterViewModel(o)
            {
                Parent = (!string.IsNullOrEmpty(o.ParentID) && o.ParentID != "0")
                    ? chapters.FirstOrDefault(t => t.ID == o.ParentID)
                    : new ModChapterEntity {
                    Order = o.Order
                },
                ChildNode = _service.CreateQuery().Find(x => x.ParentID == o.ID).ToList(),
                ItemChild = _lessonService.CreateQuery().Find(x => x.IsParentCourse == false && x.ChapterID == o.ID).ToList()
            }).OrderBy(o => o.Parent.Order).ThenBy(o => o.Order).ToList();

            ViewBag.CurrentChapter = item.ID;

            return(View());
        }
        public IActionResult Edit(ModChapterModel model)
        {
            ViewBag.Title = "Cập nhật";
            string userID = _currentUser.ID;

            var item = new ModChapterEntity();

            if (string.IsNullOrEmpty(model.ID))
            {
                if (!string.IsNullOrEmpty(model.CourseID) && model.CourseID != "0")
                {
                    return(RedirectToAction("Detail", "ModCourses", new { ID = model.CourseID }));
                }
                else
                {
                    return(RedirectToAction("Index", "ModCourses"));
                }
            }
            else
            {
                item           = _service.GetByID(model.ID);
                model.CourseID = item.CourseID;
                if (item == null)
                {
                    SetMessageError("Không tìm thấy chương");
                    return(RedirectToAction("Detail", "ModCourses", new { ID = item.CourseID }));
                }
                ViewBag.Data = item;
            }

            var course = _courseService.GetByID(model.CourseID);

            if (course == null)
            {
                return(RedirectToAction("Index", "ModCourses"));
            }

            ViewBag.Course = course;

            var chapters = _service.Find(true, o => o.CourseID == model.CourseID);

            ViewBag.Chapters = chapters.Select(o => new ModChapterViewModel(o)
            {
                Parent = (!string.IsNullOrEmpty(o.ParentID) && o.ParentID != "0")
                    ? chapters.FirstOrDefault(t => t.ID == o.ParentID)
                    : new ModChapterEntity {
                    Order = o.Order
                }
            }).OrderBy(o => o.Parent.Order).ThenBy(o => o.Order).ToList();

            ViewBag.Model          = model;
            ViewBag.CurrentChapter = item.ID;
            return(View());
        }
        public IActionResult Create(ModChapterModel model)
        {
            ViewBag.Title = "Thêm mới";
            string userID = _currentUser.ID;

            if (string.IsNullOrEmpty(model.CourseID))
            {
                return(RedirectToAction("Index", "ModCourses"));
            }

            var course = _courseService.GetByID(model.CourseID);

            if (course == null)
            {
                return(RedirectToAction("Index", "ModCourses"));
            }

            ViewBag.Course = course;

            var chapters = _service.Find(true, o => o.CourseID == model.CourseID);

            ViewBag.Chapters = chapters.Select(o => new ModChapterViewModel(o)
            {
                Parent = (!string.IsNullOrEmpty(o.ParentID) && o.ParentID != "0")
                    ? chapters.FirstOrDefault(t => t.ID == o.ParentID)
                    : new ModChapterEntity {
                    Order = o.Order
                }
            }).OrderBy(o => o.Parent.Order).ThenBy(o => o.Order).ToList();

            if (!string.IsNullOrEmpty(model.ID))
            {
                return(RedirectToAction("Edit", new { model.ID, model.CourseID }));
            }

            ViewBag.Model = model;
            return(View());
        }
        public async Task <IActionResult> Create(ModChapterModel model, ModChapterEntity item)
        {
            ViewBag.Title  = "Thêm mới";
            ViewData.Model = model;
            if (string.IsNullOrEmpty(model.CourseID))
            {
                return(RedirectToAction("Index", "ModCourses"));
            }
            var course = _courseService.GetByID(model.CourseID);

            if (course == null)
            {
                return(RedirectToAction("Index", "ModCourses"));
            }
            ViewBag.Course = course;

            var chapters = _service.Find(true, o => o.CourseID == model.CourseID);

            ViewBag.Chapters = chapters.Select(o => new ModChapterViewModel(o)
            {
                Parent = (!string.IsNullOrEmpty(o.ParentID) && o.ParentID != "0")
                    ? chapters.FirstOrDefault(t => t.ID == o.ParentID)
                    : new ModChapterEntity {
                    Order = o.Order
                }
            }).OrderBy(o => o.Parent.Order).ThenBy(o => o.Order).ToList();

            if (!string.IsNullOrEmpty(model.ID))
            {
                return(RedirectToAction("Edit", new { model.ID, model.CourseID }));
            }

            if (!string.IsNullOrEmpty(model.ID) || !string.IsNullOrEmpty(item.ID))
            {
                return(RedirectToAction("Edit", new { model.ID, model.CourseID }));
            }
            else
            {
                if (string.IsNullOrEmpty(item.Name))
                {
                    SetMessageWarning("Bạn chưa điền tên của mục");
                    return(View());
                }
                else
                {
                    item.Code       = UnicodeName.ConvertUnicodeToCode(item.Name, "-", true);
                    item.Created    = DateTime.Now;
                    item.Updated    = DateTime.Now;
                    item.CreateUser = _currentUser.ID;
                    item.Order      = chapters.Count() + 1;
                    if (!string.IsNullOrEmpty(item.ParentID) && item.ParentID != "0")
                    {
                        item.ParentType = PARENT_TYPE_CODE.CHAPTER;
                    }
                    else
                    {
                        item.ParentType = PARENT_TYPE_CODE.COURSE;
                    }
                    await _service.AddAsync(item);

                    SetMessageSuccess("Thêm mới thành công");
                    return(RedirectToAction("Detail", "ModCourses", new { ID = model.CourseID }));
                }
            }
        }