Esempio n. 1
0
        public async Task <IActionResult> UpdateQuestion(int quizId, int questionId, [FromBody] QuestionArgs data)
        {
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            Quiz quiz = await _Db.Quizzes.FindAsync(quizId);

            QuizQuestion question = await _Db.QuizQuestions.Include(q => q.Answers).Where(q => q.Id == questionId).FirstOrDefaultAsync();

            if (quiz == null || question == null)
            {
                return(NotFound(new ResponseHelper("Something went wrong. Please refresh your browser and try again.", "Unable to find question in database")));
            }

            using (var transaction = await _Db.Database.BeginTransactionAsync())
            {
                question.CorrectAnswerId = null;

                await _Db.SaveChangesAsync();

                _Db.RemoveRange(question.Answers);

                await _Db.SaveChangesAsync();

                question.ImageId         = data.Image?.Id;
                question.Text            = data.Text;
                question.TrueFalseAnswer = data.TrueFalseAnswer;
                question.AudioId         = data.Audio?.Id;

                if (data.Answers != null)
                {
                    foreach (AnswerArgs answerArgs in data.Answers)
                    {
                        QuizAnswer newAns = new QuizAnswer
                        {
                            QuizQuestionId = question.Id,
                            Text           = answerArgs.Text,
                            ImageId        = answerArgs.Image?.Id
                        };

                        await _Db.AddAsync(newAns);

                        await _Db.SaveChangesAsync();

                        if (data.Answers.IndexOf(answerArgs) == data.CorrectAnswerIndex)
                        {
                            question.CorrectAnswerId = newAns.Id;
                        }
                    }
                }

                quiz.UpdatedAt = DateTime.UtcNow;

                await _Db.SaveChangesAsync();

                transaction.Commit();
            }

            return(Ok());
        }
Esempio n. 2
0
        public async Task <IActionResult> AddQuestion(int quizId, [FromBody] QuestionArgs data)
        {
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            Quiz quiz = await _Db.Quizzes.Include(q => q.Questions).Where(q => q.Id == quizId).FirstOrDefaultAsync();

            if (quiz == null)
            {
                return(NotFound(new ResponseHelper("Something went wrong. Please refresh your browser and try again.", "Unable to find quiz in database")));
            }

            QuizQuestion question = new QuizQuestion
            {
                Quiz            = quiz,
                TrueFalseAnswer = data.TrueFalseAnswer,
                ImageId         = data.Image?.Id,
                AudioId         = data.Audio?.Id,
                Text            = data.Text,
                OrderIndex      = quiz.Questions.Count,
            };

            // Transaction rolls back all changes if a failure occurs halfway through
            using (var transaction = await _Db.Database.BeginTransactionAsync())
            {
                quiz.UpdatedAt = DateTime.UtcNow;

                // Generate question ID
                await _Db.AddAsync(question);

                await _Db.SaveChangesAsync();

                // Generate IDs for each question, update question correctAnswerId
                for (int i = 0; i < data.Answers?.Count; i++)
                {
                    QuizAnswer answer = new QuizAnswer
                    {
                        QuizQuestionId = question.Id,
                        Text           = data.Answers[i].Text,
                        ImageId        = data.Answers[i].Image?.Id
                    };

                    await _Db.AddAsync(answer);

                    await _Db.SaveChangesAsync();

                    if (i == data.CorrectAnswerIndex)
                    {
                        question.CorrectAnswerId = answer.Id;
                    }
                }

                await _Db.SaveChangesAsync();

                transaction.Commit();
            }

            return(Ok());
        }