public async Task UpdateAnswer(apiModels.Answer answer) { if (answer == null) { throw new ArgumentNullException(nameof(answer)); } var text = answer.Text.TrimToNull(); if (text == null) { throw new InvalidOperationException("Answer With Empty Text cannot be updated!"); } var dbModel = await _dbContext.Answer.FirstOrDefaultAsync(q => q.Id == answer.Id) ?? throw new InvalidOperationException("Wrong Answer Id Provided."); dbModel.Text = text; _dbContext.Answer.Update(dbModel); await _dbContext.SaveChangesAsync(); }
public async Task CreateAnswer(int questionId, apiModels.Answer answer) { if (answer == null) { throw new ArgumentNullException(nameof(answer)); } var text = answer.Text.TrimToNull(); if (text == null) { throw new InvalidOperationException("Answer With Empty Text cannot be created!"); } var dbQuestion = await _dbContext.Question.FirstOrDefaultAsync(q => q.Id == questionId) ?? throw new InvalidOperationException("Wrong Question Id Provided."); var dbModel = new Answer { Text = text, QuestionId = dbQuestion.Id }; _dbContext.Answer.Add(dbModel); await _dbContext.SaveChangesAsync(); }