public async Task CreateQuiz(apiModels.Quiz quiz) { if (quiz == null) { throw new ArgumentNullException(nameof(quiz)); } var title = quiz.Title.TrimToNull(); if (title == null) { throw new InvalidOperationException("Quiz With Empty Title cannot be created!"); } var dbQuiz = new Quiz { Title = title }; _dbContext.Quiz.Add(dbQuiz); if (quiz.Questions?.Any() == true) { await _dbContext.SaveChangesAsync(); // we need quiz Id foreach (var question in quiz.Questions) { await CreateQuestion(dbQuiz.Id, question); } } await _dbContext.SaveChangesAsync(); }
public async Task UpdateQuizById(apiModels.Quiz quiz) { if (quiz == null) { throw new ArgumentNullException(nameof(quiz)); } var title = quiz.Title.TrimToNull(); if (title == null) { throw new InvalidOperationException("Quiz With Empty Title cannot be updated!"); } var dbQuiz = await _dbContext.Quiz.FirstOrDefaultAsync(q => q.Id == quiz.Id) ?? throw new InvalidOperationException("Wrong Quiz Id Provided."); dbQuiz.Title = title; await _dbContext.SaveChangesAsync(); }