public async Task <DbVocabularyExercise> CreateVocabularyExerciseAsync(DbVocabularyExercise vocabularyExercise) { if (vocabularyExercise == null || vocabularyExercise.Id != 0) { return(null); } var test = await _context.Tests .Where(t => t.Id == vocabularyExercise.TestId) .SingleOrDefaultAsync(); test.NumberOfQuestions++; _context.VocabularyExercises.Add(vocabularyExercise); await _context.SaveChangesAsync(); return(vocabularyExercise); }
public async Task <DbVocabularyExercise> EditVocabularyExerciseAsync(DbVocabularyExercise vocabularyExercise) { var oldExercise = await _context.VocabularyExercises .Where(e => e.Id == vocabularyExercise.Id) .SingleOrDefaultAsync(); if (oldExercise == null) { return(null); } if (oldExercise.Sentence != vocabularyExercise.Sentence) { oldExercise.Sentence = vocabularyExercise.Sentence; } if (oldExercise.TranslatedSentence != vocabularyExercise.TranslatedSentence) { oldExercise.TranslatedSentence = vocabularyExercise.TranslatedSentence; } if (oldExercise.CorrectAnswer != vocabularyExercise.CorrectAnswer) { oldExercise.CorrectAnswer = vocabularyExercise.CorrectAnswer; } if (oldExercise.TranslatedCorrectAnswer != vocabularyExercise.TranslatedCorrectAnswer) { oldExercise.TranslatedCorrectAnswer = vocabularyExercise.TranslatedCorrectAnswer; } if (oldExercise.Picture != vocabularyExercise.Picture) { oldExercise.Picture = vocabularyExercise.Picture; } if (oldExercise.WrongAnswer1 != vocabularyExercise.WrongAnswer1) { oldExercise.WrongAnswer1 = vocabularyExercise.WrongAnswer1; } if (oldExercise.WrongAnswer2 != vocabularyExercise.WrongAnswer2) { oldExercise.WrongAnswer2 = vocabularyExercise.WrongAnswer2; } if (oldExercise.WrongAnswer3 != vocabularyExercise.WrongAnswer3) { oldExercise.WrongAnswer3 = vocabularyExercise.WrongAnswer3; } if (oldExercise.TranslatedWrongAnswer1 != vocabularyExercise.TranslatedWrongAnswer1) { oldExercise.TranslatedWrongAnswer1 = vocabularyExercise.TranslatedWrongAnswer1; } if (oldExercise.TranslatedWrongAnswer2 != vocabularyExercise.TranslatedWrongAnswer2) { oldExercise.TranslatedWrongAnswer2 = vocabularyExercise.TranslatedWrongAnswer2; } if (oldExercise.TranslatedWrongAnswer3 != vocabularyExercise.TranslatedWrongAnswer3) { oldExercise.TranslatedWrongAnswer3 = vocabularyExercise.TranslatedWrongAnswer3; } try { _context.VocabularyExercises.Update(oldExercise); await _context.SaveChangesAsync(); } catch (Exception) { return(null); } return(vocabularyExercise); }