async Task <Term> ValidateRequestAsync(TermQuestion model) { var term = await _termsService.GetByIdAsync(model.TermId); if (term == null) { ModelState.AddModelError("termId", "條文不存在"); } var question = await _questionsService.GetByIdAsync(model.QuestionId); if (question == null) { ModelState.AddModelError("questionId", "試題不存在"); } return(term); }
public async Task <ActionResult> Store([FromBody] TermQuestion model) { var term = await ValidateRequestAsync(model); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var qids = term.QIds.SplitToIds(); if (!qids.Contains(model.QuestionId)) { qids.Add(model.QuestionId); } term.QIds = qids.JoinToStringIntegers(); await _termsService.UpdateAsync(term); return(Ok()); }