async Task <List <QuestionViewModel> > LoadQuestionViewsAsync(IEnumerable <Question> questions) { var allRecruits = await _recruitsService.GetAllAsync(); List <Term> allTerms = null; var types = new List <PostType> { PostType.Option }; var attachments = await _attachmentsService.FetchByTypesAsync(types); var viewList = questions.MapViewModelList(_mapper, allRecruits.ToList(), attachments.ToList(), allTerms); var sources = viewList.SelectMany(q => q.Resolves).SelectMany(r => r.Sources); foreach (var item in sources) { item.MapContent(_notesService, _termsService); } return(viewList); }
public async Task <ActionResult> Index(int subject, int term = 0, int recruit = 0, string keyword = "", int page = 1, int pageSize = 10) { if (page < 1) { var model = new QuestionsIndexModel(); await LoadOptionsAsync(model); return(Ok(model)); } Subject selectedSubject = _subjectsService.GetById(subject); if (selectedSubject == null) { ModelState.AddModelError("subject", "科目不存在"); return(BadRequest(ModelState)); } Term selectedTerm = null; ICollection <int> termIds = null; if (term > 0) { selectedTerm = _termsService.GetById(term); if (selectedTerm == null) { ModelState.AddModelError("term", "條文不存在"); return(BadRequest(ModelState)); } termIds = selectedTerm.GetSubIds(); termIds.Add(selectedTerm.Id); } var allRecruits = await _recruitsService.GetAllAsync(); Recruit selectedRecruit = null; List <int> recruitIds = new List <int>(); if (recruit > 0) { selectedRecruit = allRecruits.FirstOrDefault(x => x.Id == recruit); if (selectedRecruit == null) { ModelState.AddModelError("recruit", "招考年度不存在"); return(BadRequest(ModelState)); } recruitIds.Add(recruit); if (selectedRecruit.RecruitEntityType == RecruitEntityType.SubItem) { var partIds = allRecruits.Where(x => x.ParentId == recruit).Select(part => part.Id); recruitIds.AddRange(partIds.ToList()); recruitIds.Add(recruit); } } var questions = await _questionsService.FetchAsync(selectedSubject, termIds, recruitIds); if (questions.IsNullOrEmpty()) { return(Ok(questions.GetPagedList(_mapper, page, pageSize))); } var keywords = keyword.GetKeywords(); if (keywords.HasItems()) { questions = questions.FilterByKeyword(keywords); } List <Term> allTerms = (await _termsService.FetchAllAsync()).ToList(); List <UploadFile> attachments = (await _attachmentsService.FetchAsync(PostType.Option)).ToList(); var pagedList = questions.GetPagedList(_mapper, allRecruits.ToList(), attachments, allTerms, page, pageSize); foreach (var item in pagedList.ViewList) { item.Options = item.Options.OrderByDescending(o => o.Correct).ToList(); } return(Ok(pagedList)); }
public async Task <ActionResult> RQ(int subject) { var rootSubject = _subjectsService.GetById(subject); if (rootSubject == null) { return(NotFound()); } if (rootSubject.ParentId > 0) { ModelState.AddModelError("subject", "錯誤的科目"); return(BadRequest(ModelState)); } var rootSubjectView = rootSubject.MapViewModel(_mapper); var subjectViews = rootSubject.SubItems.MapViewModelList(_mapper); var allTerms = await _termsService.FetchAsync(new TermFilterBySubjectsSpecification(rootSubject.GetSubIds().ToList())); var allRecruits = await _recruitsService.GetAllAsync(); var recruits = allRecruits.Where(x => x.SubjectId == subject).ToList(); var models = new List <ExamSettingsViewModel>(); foreach (var recruit in recruits) { recruit.LoadParent(allRecruits); recruit.LoadSubItems(allRecruits); //取得一個recruit年度的試題 var rqModel = await GetRQModelAsync(recruit, rootSubject); var model = new ExamSettingsViewModel(rootSubjectView, recruit.MapViewModel(_mapper)); foreach (var rqPart in rqModel.Parts) { var partSettings = new ExamPartSettings() { Order = rqPart.Order, Title = rqPart.Title, Points = rqPart.Points, OptionCount = rqPart.OptionCount, OptionType = rqPart.OptionType, MultiAnswers = rqPart.MultiAnswers, Questions = rqPart.Questions.Count }; var subjectSettings = new List <ExamSubjectSettings>(); foreach (var subjectView in subjectViews) { var terms = allTerms.Where(x => x.SubjectId == subjectView.Id && x.ParentId == 0).GetOrdered(); foreach (var term in terms) { term.LoadSubItems(allTerms); } var termViews = terms.MapViewModelList(_mapper); var examSubjectSettings = new ExamSubjectSettings(subjectView, termViews); subjectSettings.Add(examSubjectSettings); } partSettings.Subjects = subjectSettings; foreach (var question in rqPart.Questions) { partSettings.AddQuestion(question); } model.Parts.Add(partSettings); } models.Add(model); } var indexModel = new AnalysisIndexModel(); indexModel.Results = models; indexModel.ViewList = models.MapAnalysisViewList(); return(Ok(indexModel)); }