public async Task <ActionResult <ApiResultViewModel <QuestionViewModel> > > CreateAsync( [FromBody] QuestionInputModel inputModel, CancellationToken cancellationToken) { var item = new Question { Body = inputModel.Body, Answer1 = inputModel.Answer1, Answer2 = inputModel.Answer2, Answer3 = inputModel.Answer3, Answer4 = inputModel.Answer4, CorrectAnswerNumber = inputModel.CorrectAnswerNumber, Difficulty = inputModel.Difficulty, }; if (inputModel.CategoryIds != null && inputModel.CategoryIds.Any()) { item.CategoryIdsCache = string.Join(",", inputModel.CategoryIds); } using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { item = await _questionManager.SaveAsync(item, cancellationToken); if (inputModel.CategoryIds != null) { await _questionManager.UpdateQuestionCategories(item, inputModel.CategoryIds.Select(cid => cid.ToLong()), cancellationToken); } transaction.Complete(); } return(OkData(QuestionViewModel.Map(item))); }
public async Task <ActionResult <ApiResultViewModel <QuestionViewModel> > > UpdateAsync([FromRoute] string id, [FromBody] QuestionInputModel inputModel, CancellationToken cancellationToken) { var item = await _questionManager.GetByIdAsyncThrowNotFoundIfNotExists(id, cancellationToken); item.Body = inputModel.Body; item.Answer1 = inputModel.Answer1; item.Answer2 = inputModel.Answer2; item.Answer3 = inputModel.Answer3; item.Answer4 = inputModel.Answer4; item.CorrectAnswerNumber = inputModel.CorrectAnswerNumber; item.Difficulty = inputModel.Difficulty; item.CategoryIdsCache = string.Join(",", inputModel.CategoryIds); using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { item = await _questionManager.SaveAsync(item, cancellationToken); await _questionManager.UpdateQuestionCategories(item, inputModel.CategoryIds?.Select(cid => cid.ToLong()), cancellationToken); transaction.Complete(); } item = await _questionManager.SaveAsync(item, cancellationToken); return(OkData(QuestionViewModel.Map(item))); }