Exemplo n.º 1
0
        public async Task <IActionResult> CreateQuiz([FromBody][Bind("Topic", "Category", "Description", "ImagePath", "Questions")] Quiz quiz)
        {
            if (quiz.Id != 0)
            {
                return(BadRequest("Invalid ID"));
            }

            quiz.Category = QuizUtil.CategoryFromIndex(int.TryParse(quiz.Category, out var i) ? i : (int?)null);

            if (quiz.Category == null)
            {
                return(BadRequest("Invalid category!"));
            }
            if (!QuizUtil.QuestionsValid(quiz.Questions))
            {
                return(BadRequest("Invalid question format!"));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid quiz format!"));
            }

            quiz.OwnerUsername = _um.GetUserName(User);

            _db.Add(quiz);
            await _db.SaveChangesAsync();

            var user = await _um.GetUserAsync(User);

            user.MyQuizzes.Add(quiz);
            await _um.UpdateAsync(user);

            return(Ok());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UpdateQuiz([FromBody][Bind("Topic", "Category", "Description", "ImagePath", "Questions")] Quiz quizForm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Model state not valid!"));
            }

            var quiz = await _db.Quizzes
                       .Where(q => q.Id == quizForm.Id)
                       .Include(q => q.Questions)
                       .FirstOrDefaultAsync();

            if (quiz == null)
            {
                return(BadRequest("Quiz not found!"));
            }
            if (quiz.OwnerId != _um.GetUserId(User))
            {
                return(BadRequest("You are not the owner of this quiz!"));
            }
            if (!QuizUtil.QuestionsValid(quiz.Questions))
            {
                return(BadRequest("Question format not valid!"));
            }

            if (await _db.QuizzesTaken.AnyAsync(q => q.Quiz == quiz))
            {
                return(BadRequest("You cannot edit this quiz because someone has already taken it. You can however delete it, and create a new."));
            }

            try
            {
                if (quizForm.Category != quiz.Category)
                {
                    quiz.Category = QuizUtil.CategoryFromIndex(int.TryParse(quiz.Category, out var i) ? i : (int?)null);
                    if (quiz.Category == null)
                    {
                        return(BadRequest("Invalid category!"));
                    }
                }

                quiz.Topic       = quizForm.Topic;
                quiz.Description = quizForm.Description;
                quiz.ImagePath   = quizForm.ImagePath;

                _db.Questions.RemoveRange(quiz.Questions);
                quiz.Questions = quizForm.Questions;

                await _db.SaveChangesAsync();
            } catch (DbUpdateConcurrencyException) {
                return(BadRequest("Woopsie! Database error! :("));
            }

            return(Ok());
        }