public async Task <bool> SubmitAnswer(AnswerViewModel answer, Guid traineeId) { bool allCorrect = true; foreach (var model in answer.Answers) { Question dbQuestion = await Db.Questions.FindAsync(model.QuestionId); Option option = dbQuestion.Options.First(x => x.IsAnswer); if (option.Name != model.AnswerText) { allCorrect = false; break; } } TraineeQuizHistory history = await Db.TraineeQuizHistories.FindAsync(answer.QuizHistoryId); history.LastAccessed = DateTime.Now; if (allCorrect) { history.IsCompleted = true; history.Point = answer.Answers.Count; Trainee trainee = await Db.Trainees.FindAsync(traineeId); trainee.Point += history.Point; // save this answer history to the table for future reference foreach (Answer a in answer.Answers) { TraineeQuizAnswerHistory answerHistory = new TraineeQuizAnswerHistory() { TraineeId = traineeId, QuizHistoryId = answer.QuizHistoryId, QuestionId = a.QuestionId, Answer = a.AnswerText, IsCorrect = true, IsVerified = true, AnswerDate = DateTime.Now }; Db.TraineeQuizAnswerHistories.Add(answerHistory); } } await Db.SaveChangesAsync(); return(allCorrect); }
public async Task <bool> SubmitAssignment(AnswerViewModel answer, Guid traineeId) { foreach (Answer a in answer.Answers) { TraineeQuizAnswerHistory answerHistory = new TraineeQuizAnswerHistory() { TraineeId = traineeId, QuizHistoryId = answer.QuizHistoryId, QuestionId = a.QuestionId, Answer = a.AnswerText, IsCorrect = false, IsVerified = false, AnswerDate = DateTime.Now }; Db.TraineeQuizAnswerHistories.Add(answerHistory); } TraineeQuizHistory history = await Db.TraineeQuizHistories.FindAsync(answer.QuizHistoryId); history.LastAccessed = DateTime.Now; history.IsCompleted = true; int i = await Db.SaveChangesAsync(); return(true); }