public async Task <QuestionResponseModel> CreateQuestionAsync(QuestionRequestModel model, string userId) { var question = model.Map(userId); _dbContext.Add(question); await _dbContext.SaveChangesAsync(); return(await GetQuestionAsync(question.Id)); }
public async Task <QuestionResponseModel> UpdateQuestionAsync(string questionId, QuestionRequestModel model, string userId) { using (var transaction = _dbContext.Database.BeginTransaction()) { try { var entity = _dbContext.Questions.First(u => u.Id == questionId); entity.Text = model.Text; entity.SubjectId = model.SubjectId; entity.ScoreValue = model.ScoreValue.Value; entity.QuestionType = model.QuestionType; entity.ShuffleOptions = model.ShuffleOptions.Value; entity.DifficultyLevelId = model.DifficultyLevelId; entity.Options = model.Options.Select(u => new Option { Text = u.Text, IsAnswer = u.IsAnswer.Value }).ToList(); var question = model.Map(userId, questionId); var options = _dbContext.Options.Where(u => u.QuestionId == questionId); _dbContext.RemoveRange(options); await _dbContext.SaveChangesAsync(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw new Exception("An error occurred"); } } return(await GetQuestionAsync(questionId)); }