예제 #1
0
        public IActionResult Index(Enumerations.PageAction pageAction, int?lessonId)
        {
            UserSessionContext userContext = new UserSessionContext(this.HttpContext);

            var            lessonModel     = new LessonViewModel(this.HttpContext);
            LessonBusiness businessManager = new LessonBusiness(DbContext);

            SearchViewModel userSearch = new SearchViewModel();

            //Set Lesson List search based on page action
            switch (pageAction)
            {
            case Enumerations.PageAction.Submit:
                //Show only current user's draft lessons
                userSearch = new SearchViewModel {
                    OwnerSid = userContext.CurrentUser.Sid, Status = (int?)Enumerations.LessonStatus.Draft
                };
                break;

            case Enumerations.PageAction.Edit:
                //Show only editable lessons
                userSearch = new SearchViewModel {
                    ShowEditableOnly = true
                };
                break;

            case Enumerations.PageAction.Search:
                //Show the last search
                userSearch = userContext.LastSearch;
                break;

            case Enumerations.PageAction.MyLessons:
                //Show all owned lessons (or filtered lessons of comming from dashboard)
                userSearch = (SearchViewModel)TempData["MyLessonModel"] ?? new SearchViewModel {
                    ShowOnlyOwnedLessons = true
                };
                break;

            default:
                throw new ArgumentOutOfRangeException("pageAction");
            }

            if (TempData.ContainsKey("Lesson"))
            {
                //Existing lesson (Edit Validation)
                lessonModel = (LessonViewModel)TempData["Lesson"];
            }
            else if (lessonId.HasValue &&
                     (pageAction == Enumerations.PageAction.Edit || pageAction == Enumerations.PageAction.Submit))
            {
                //Existing Lesson (Edit Existing)
                var lesson = businessManager.GetLessonById(lessonId.Value);
                lessonModel = LessonViewModel.ToViewModel(this.HttpContext, lesson);

                if (!Utils.IsLessonVisible(lessonModel, userContext.CurrentUser))
                {
                    return(RedirectToAction("Search"));
                }
            }
            else
            {
                if (pageAction != Enumerations.PageAction.Edit)
                {
                    if (lessonId.HasValue)
                    {
                        //Submit with "Save Changes", leave on submit tab with the recently added lesson
                        var lesson = businessManager.GetLessonById(lessonId.Value);
                        lessonModel = LessonViewModel.ToViewModel(this.HttpContext, lesson);
                    }
                    else
                    {
                        //New Lesson (Submit)
                        lessonModel = userContext.DraftDefaults;
                    }
                }
                else
                {
                    int unused = 0;
                    //Edit / Review, set to first lesson in search list
                    var lesson = businessManager.GetLessonsPaged(userContext.CurrentUser, userSearch, false, true, 0, 1, out unused).FirstOrDefault();
                    lessonModel = LessonViewModel.ToViewModel(this.HttpContext, lesson);
                    if (lesson != null)
                    {
                        lessonId = lesson.Id;
                    }
                    else
                    {
                        ViewBag.NothingToEdit = true;
                    }
                }
            }

            lessonModel.ReturnToAction = pageAction;

            var submittedUsers = businessManager.GetSubmittedByUsers();

            ViewBag.SubmittedByUsers = submittedUsers;

            userContext.LastSystemSearch = userSearch;

            LessonIndexViewModel model = new LessonIndexViewModel(this.HttpContext)
            {
                PageAction  = pageAction,
                LessonId    = lessonId,
                Lesson      = lessonModel,
                SearchModel = userSearch
            };

            return(View("Index", model));
        }