コード例 #1
0
        public async Task <ActionResult> Index(int recruit)
        {
            Recruit selectedRecruit = _recruitsService.GetById(recruit);

            if (selectedRecruit == null)
            {
                ModelState.AddModelError("recruit", "年度不存在");
                return(BadRequest(ModelState));
            }

            if (selectedRecruit.RecruitEntityType == RecruitEntityType.Year)
            {
                return(Ok(new List <Question>().GetPagedList(_mapper)));
            }

            var allSubjects = await _subjectsService.FetchAsync();

            var subject = _recruitsService.FindSubject(selectedRecruit, allSubjects);

            if (subject == null)
            {
                ModelState.AddModelError("subject", "科目不存在");
                return(BadRequest(ModelState));
            }

            _subjectsService.LoadSubItems(subject);

            var questions = await _questionsService.FetchByRecruitAsync(selectedRecruit, subject);

            List <Term>       allTerms    = null;
            List <Recruit>    allRecruits = null;
            List <UploadFile> attachments = null;

            if (questions.HasItems())
            {
                attachments = (await _attachmentsService.FetchAsync(PostType.Option)).ToList();
                allTerms    = (await _termsService.FetchAllAsync()).ToList();
            }

            var pageList = questions.GetPagedList(_mapper, allRecruits, attachments, allTerms);

            foreach (var item in pageList.ViewList)
            {
                item.Options = item.Options.OrderByDescending(o => o.Correct).ToList();
            }


            return(Ok(pageList));
        }
コード例 #2
0
        public async Task <ActionResult> Update(int id, [FromBody] RecruitViewModel model)
        {
            var existingEntity = _recruitsService.GetById(id);

            if (existingEntity == null)
            {
                return(NotFound());
            }

            await ValidateRequestAsync(model);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (model.Active)
            {
                if (existingEntity.Active == false)
                {
                    model.Order = 0;
                }
            }
            else
            {
                if (existingEntity.Active)
                {
                    model.Order = -1;
                }
            }

            var recruit = model.MapEntity(_mapper, CurrentUserId);

            await _recruitsService.UpdateAsync(existingEntity, recruit, recruit.SubItems);

            return(Ok());
        }