public async Task <IActionResult> Post([FromBody] SaveQuizDTO quiz)
        {
            try
            {
                //1 map save quiz dto naar quiz
                var mappedQuiz = _mapper.Map <Quiz>(quiz);

                if (mappedQuiz == null)
                {
                    return(NotFound());
                }

                //  2. ADD TO REPO

                _quizrepo.Create(mappedQuiz);

                // 3 . savechanges async
                await _quizrepo.SaveAsync();

                // 4. map to dto (niet save dto)
                var quizDto = _mapper.Map <QuizDTO>(mappedQuiz);

                // 5 . return created at action
                return(Ok(quizDto));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(BadRequest());
            }
        }
        public async Task <ActionResult> CreateQuiz(IFormCollection collection, QuizClass quiz)
        {
            try
            {
                if (!ModelState.IsValid) //validatie error
                {
                    return(View());
                }
                if (await quizRepo.GetQuizByNameAsync(quiz.Name) != null)
                {
                    ModelState.AddModelError("", "This name is already in use");
                    return(View());
                }
                var created = await quizRepo.Create(quiz);

                if (created == null)
                {
                    return(Redirect("/Error/0"));
                }
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception exc)
            {
                Console.WriteLine("Create is giving an error: " + exc.Message);
                ModelState.AddModelError("", "Create actie is failed try again");
                return(View());
            }
        }
Ejemplo n.º 3
0
        public async Task <int> Create(CreateQuizRequestModel model)
        {
            if (_quizRepo.CheckIfQuizExisting(model.Name))
            {
                throw new Exception("Duplicated Quiz Name");
            }

            return(await _quizRepo.Create(_mapper.Map <Quiz>(model)));
        }
Ejemplo n.º 4
0
 public async Task <IActionResult> Create(IFormCollection collection, Quiz quiz)
 {
     try {
         if (ModelState.IsValid)
         {
             quizRepo.Create(quiz);
             await quizRepo.SaveChangesAsync();
         }
         return(RedirectToAction(nameof(Index)));
     } catch {
         return(View());
     }
 }
Ejemplo n.º 5
0
        public async static Task SeedQuizold(IQuizRepo quizRepo, IQuestionRepo questionRepo)
        {
            string[]         quizNames = { "Animals", "trivia", "geography" };
            List <QuizClass> quizzes   = new List <QuizClass>();

            foreach (var quizName in quizNames)
            {
                var quiz = await quizRepo.GetQuizByNameAsync(quizName);

                if (quiz == null)
                {
                    QuizClass newQuiz = new QuizClass()
                    {
                        Name        = quizName,
                        Difficulty  = 2,
                        Description = "A quiz about " + quizName
                    };
                    await quizRepo.Create(newQuiz);
                }
                quizzes.Add(quiz);
            }

            Question vraag1 = new Question()
            {
                Description = "hoeveel weegt pikachu"
            };
            Option option1 = new Option()
            {
                OptionDescription = "5 kg",
                CorrectAnswer     = true
            };
            Option option2 = new Option()
            {
                OptionDescription = "8 kg",
                CorrectAnswer     = false
            };

            List <Option> opties = new List <Option>();

            opties.Add(option1);
            opties.Add(option2);
            vraag1.PossibleOptions = opties;

            var question = await questionRepo.GetQuestionByDescriptionAsync(vraag1.Description);

            if (question == null)
            {
                await questionRepo.Create(vraag1);
            }
            await quizRepo.AddQuestionToQuizAsync(quizzes[0].QuizId, vraag1.QuestionId);
        }
Ejemplo n.º 6
0
        public override async Task <IActionResult> Post(QuizSaveResource saveResource)
        {
            try {
                var entity = _mapper.Map <Quiz>(saveResource);

                _repo.Create(entity);
                await _repo.SaveChangesAsync();

                entity = await _repo.GetAsync(entity.Id);

                var resoure = _mapper.Map <QuizResource>(entity);

                return(CreatedAtAction(nameof(GetAll), resoure));
            } catch (Exception ex) {
                _logger.LogError(ex.Message);
                return(BadRequest());
            }
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> PostQuiz([FromBody] QuizClass quiz)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState.Values));
                }
                QuizClass confirmedModel = await quizRepo.Create(quiz);

                if (confirmedModel == null)
                {
                    return(BadRequest("Something went wrong"));
                }
                return(Ok(confirmedModel));
            }
            catch (Exception exc)
            {
                return(BadRequest("Couldn't create the quiz"));
            }
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> CreateQuiz([Bind("Title,DifficultyId,Questions")] Quiz quiz)
        {
            try
            {
                if (quiz.DifficultyId == 0)
                {
                    quiz.DifficultyId = 1;
                }
                _quizRepo.Create(quiz);
                await _quizRepo.SaveAsync();

                //return View(quiz);
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(View("Error", new ErrorViewModel()
                {
                    RequestId = HttpContext.TraceIdentifier
                }));
            }
        }
Ejemplo n.º 9
0
        public async static Task SeedQuiz(IQuizRepo quizRepo, IQuestionRepo questionRepo)
        {
            //quiz over Trivia aanmaken
            string quizName = "Trivia";

            if (await quizRepo.GetQuizByNameAsync(quizName) == null)
            {
                QuizClass newQuiz = new QuizClass()
                {
                    Name        = quizName,
                    Difficulty  = 5,
                    Description = "A quiz about all sorts of things"
                };
                await quizRepo.Create(newQuiz);

                List <Option> opties = new List <Option>();
                Question      vraag  = new Question()
                {
                    Description = "Which of the following items was owned by the fewest U.S. homes in 1990?"
                };
                if (await questionRepo.GetQuestionByDescriptionAsync(vraag.Description) == null)
                {
                    Option option1 = new Option()
                    {
                        OptionDescription = "home computer",
                        CorrectAnswer     = false
                    };
                    Option option2 = new Option()
                    {
                        OptionDescription = "compact disk player",
                        CorrectAnswer     = true
                    };
                    Option option3 = new Option()
                    {
                        OptionDescription = "dishwasher",
                        CorrectAnswer     = false
                    };
                    opties.Add(option1);
                    opties.Add(option2);
                    opties.Add(option3);
                    vraag.PossibleOptions = opties;
                    await questionRepo.Create(vraag);

                    await quizRepo.AddQuestionToQuizAsync(newQuiz.QuizId, vraag.QuestionId);
                }

                vraag = new Question()
                {
                    Description = "How tall is the tallest man on earth?"
                };
                if (await questionRepo.GetQuestionByDescriptionAsync(vraag.Description) == null)
                {
                    Option option1 = new Option()
                    {
                        OptionDescription = "2.72 m",
                        CorrectAnswer     = true
                    };
                    Option option2 = new Option()
                    {
                        OptionDescription = "2.64 m",
                        CorrectAnswer     = false
                    };
                    Option option3 = new Option()
                    {
                        OptionDescription = "3.05 m",
                        CorrectAnswer     = false
                    };
                    opties = new List <Option>();
                    opties.Add(option1);
                    opties.Add(option2);
                    opties.Add(option3);
                    vraag.PossibleOptions = opties;
                    await questionRepo.Create(vraag);

                    await quizRepo.AddQuestionToQuizAsync(newQuiz.QuizId, vraag.QuestionId);
                }
                vraag = new Question()
                {
                    Description = "Which racer holds the record for the most Grand Prix wins?"
                };
                if (await questionRepo.GetQuestionByDescriptionAsync(vraag.Description) == null)
                {
                    Option option1 = new Option()
                    {
                        OptionDescription = "Michael Schumacher",
                        CorrectAnswer     = true
                    };
                    Option option2 = new Option()
                    {
                        OptionDescription = "Mario Andretti",
                        CorrectAnswer     = false
                    };
                    Option option3 = new Option()
                    {
                        OptionDescription = "Lewis Hamilton",
                        CorrectAnswer     = false
                    };
                    opties = new List <Option>();
                    opties.Add(option1);
                    opties.Add(option2);
                    opties.Add(option3);
                    vraag.PossibleOptions = opties;
                    await questionRepo.Create(vraag);

                    await quizRepo.AddQuestionToQuizAsync(newQuiz.QuizId, vraag.QuestionId);
                }
            }
            return;
        }