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()); } }
public async Task Delete(Guid quizId) { try { QuizClass quiz = await GetQuizByIdAsync(quizId); if (quiz == null) { return; } //vragen uit de tussentabel verwijderen verwijderen var tussentabel = await context.QuizQuestions.Where(e => e.QuizId == quizId).ToListAsync(); foreach (var tab in tussentabel) { //questions uit quiz deleten (en tussentabel) await questionRepo.Delete(tab.QuestionId); //om enkel uit tussentabel halen //context.QuizQuestions.Remove(tab); } context.Quizzes.Remove(quiz); await context.SaveChangesAsync(); } catch (Exception exc) { Console.WriteLine(exc.Message); } return; }
public async Task <ActionResult <QuizClass> > PutQuiz([FromBody] QuizClass quiz, Guid?id) { if (id == null) { return(BadRequest("This enpoind requires a valid QuizId")); } quiz.QuizId = id ?? Guid.Empty; var confirmedModel = new QuizClass(); //te returnen try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!await quizRepo.QuizExists(id ?? Guid.Empty)) { return(BadRequest($"{id} bestaat niet")); } confirmedModel = await quizRepo.Update(quiz); } catch (DbUpdateException) { if (await quizRepo.QuizExists(id ?? Guid.Empty)) { return(Conflict()); } else { return(BadRequest("Something went wrong, try again later")); } } return(Ok(confirmedModel)); }
public async Task <ActionResult> Scoreboard() { var scores = await gameRepo.GetAllFinishedGamesAsync(); List <Scoreboard_VM> scoreboard = new List <Scoreboard_VM>(); foreach (Game score in scores) { QuizClass quiz = await quizRepo.GetQuizByIdAsync(score.QuizId); if (quiz != null) { User user = await userMgr.FindByIdAsync(score.UserId); var questions = await quizRepo.GetQuizQuestionsAsync(score.QuizId); Scoreboard_VM vm = new Scoreboard_VM() { GameId = score.GameId, QuizName = quiz.Name, UserEmail = user.Email, UserName = user.Name, correctanswers = score.CorrectAnswers, maxquestions = questions.Count(), completetime = score.TimeFinished.Value.Subtract(score.TimeStarted) }; scoreboard.Add(vm); } } return(View(scoreboard.OrderByDescending(e => e.correctanswers / e.maxquestions).ThenBy(e => e.completetime).ToList())); }
static void Main(string[] args) { bool keepPlaying = true; while (keepPlaying == true) { QuizClass useClass = new QuizClass(); Console.WriteLine("Do you want to take a (Q)uiz or e(X)it?"); string answer = Console.ReadLine().ToUpper(); if (answer == "Q") { //get the username Console.WriteLine("Do you want to create a (N)ew user or choose an (E)xisting user?"); string kindOfuser = Console.ReadLine().ToUpper(); int userID = 0; if (kindOfuser == "N") //Making a new user { Console.WriteLine("What is the name that you would like to use?"); string newUserName = Console.ReadLine(); userID = useClass.makeNewUser(newUserName); } else if (kindOfuser == "E") //using an exiting user STILL need to work on associating the current user to the quiz { Console.WriteLine("Which user would you like to use?"); useClass.displayUsers(); Console.Write("What is the number of the user you want to use? "); userID = Convert.ToInt32(Console.ReadLine()); } else { Console.WriteLine($"{kindOfuser} is not a valid choice. Please type in N for a new user or E to choose an existing one."); } Console.WriteLine("What quiz would you like to take?"); //let the user pick what quiz they want int userQuizChoice = useClass.pickQuiz(); useClass.askQuestions(userQuizChoice, userID); }//END OF Q else if (answer == "X") { Console.WriteLine("Thank you for taking our quiz. Good bye!"); keepPlaying = false; }//END OF X else { Console.WriteLine($"{answer} was not a valid response. Please Try again."); } } //END OF MAIN LOOP } //END OF MAIN
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); }
public async Task <ActionResult> EditQuiz(Guid?id) { if (id == null) { return(Redirect("/Error/400")); } QuizClass quiz = await quizRepo.GetQuizByIdAsync(id ?? Guid.Empty); if (quiz == null) { return(Redirect("/Error/0")); } return(View(quiz)); }
public async Task <QuizClass> Update(QuizClass quiz) { try { context.Quizzes.Update(quiz); await context.SaveChangesAsync(); return(quiz); } catch (Exception exc) { Console.WriteLine(exc.Message); return(null); } }
public async Task <QuizClass> Create(QuizClass quiz) { try { var result = context.Quizzes.AddAsync(quiz); await context.SaveChangesAsync(); return(quiz); } catch (Exception exception) { Console.WriteLine(exception.Message); return(null); } }
public async Task <ActionResult> Questions(Guid?id) { if (id == null) { return(Redirect("/Error/400")); } //controleren of de quiz we lbestaat QuizClass quiz = await quizRepo.GetQuizByIdAsync(id ?? Guid.Empty); if (quiz == null) { return(Redirect("/Error/0")); } var result = await quizRepo.GetQuizQuestionsAsync(id ?? Guid.Empty); ViewBag.quizId = id; return(View(result)); }
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")); } }
public async Task <ActionResult> EditQuiz(Guid?id, IFormCollection collection, QuizClass quiz) { try { if (id == null) { return(Redirect("/Error/400")); } if (!ModelState.IsValid) { return(View()); } quiz.QuizId = id ?? Guid.Empty; QuizClass checkdouble = await quizRepo.GetQuizByNameAsync(quiz.Name); if (checkdouble != null && checkdouble.QuizId != id) { ModelState.AddModelError("", "This name is already in use"); return(View()); } //eerst controleren of er een aanpassing gebeurt is QuizClass original = await quizRepo.GetQuizByIdAsync(quiz.QuizId); if (original.Name == quiz.Name && original.Description == quiz.Description && original.Difficulty == quiz.Difficulty) { return(RedirectToAction(nameof(Index))); } if (await quizRepo.Update(quiz) == null) { throw new Exception("Couldn't create the quiz"); } 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()); } }
//[Authorize(AuthenticationSchemes = AuthSchemes, Roles = "Creator")] public async Task <IActionResult> Stats() { DateTime?date = new DateTime(2020, 5, 5); if (date == null) { return(BadRequest("Give a correct date format")); } var games = await gameRepo.GetGamesByDate(date); List <Scoreboard_DTO> gamedata = new List <Scoreboard_DTO>(); foreach (Game game in games) { QuizClass quiz = await quizRepo.GetQuizByIdAsync(game.QuizId); if (quiz != null) { User user = await userMgr.FindByIdAsync(game.UserId); var questions = await quizRepo.GetQuizQuestionsAsync(game.QuizId); Scoreboard_DTO vm = new Scoreboard_DTO() { GameId = game.GameId, QuizName = quiz.Name, UserEmail = user.Email, UserName = user.Name, correctanswers = game.CorrectAnswers, maxquestions = questions.Count(), completetime = game.TimeFinished.Value.Subtract(game.TimeStarted) }; gamedata.Add(vm); } } Stats_DTO stats = new Stats_DTO(); Stats_Mapper.ConvertTo_DTO(gamedata, ref stats); return(Ok(stats)); }
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; }
public async Task <ActionResult> Play(Game_VM vm, IFormCollection collection) { Guid vraagId = new Guid(collection["vraagId"].ToString()); int vraagNr = Int32.Parse(collection["vraagNr"].ToString()); Game currentgame = await gameRepo.GetGameByIdAsync(vm.GameId); Question currentquestion = await questionRepo.GetQuestionByIdAsync(vraagId); // controleren of het antwoord juist was bool correct = true; //controleren of ze wel iets hebben aangeduid bool filled = false; foreach (var input in vm.Options) { if (input.Value == true) { filled = true; } } if (!filled) { vm = convertGame(currentgame.GameId, currentquestion, vm.questionNr); ViewBag.questionNr = vraagNr; ViewBag.questionId = vraagId; ModelState.AddModelError("", "Je moet een optie kiezen"); return(View(vm)); } List <Option> histoptions = new List <Option>(); foreach (Option option in currentquestion.PossibleOptions) { //var guessedval = vm.Options[option.OptionDescription]; //var correctval = option.CorrectAnswer; if (vm.Options[option.OptionDescription] != option.CorrectAnswer) { correct = false; } //ondertussen options opslaan voor bij te houden voor de uitslag Option histoption = new Option() { OptionDescription = option.OptionDescription, CorrectAnswer = vm.Options[option.OptionDescription] }; histoptions.Add(histoption); } if (correct) { currentgame.CorrectAnswers += 1; if (await gameRepo.Update(currentgame) == null) { return(Redirect("/Error/0")); } } //question opslaan voor later als bij uitslag weer te geven Question histquestion = new Question() { PossibleOptions = histoptions }; if (await questionRepo.Create(histquestion) != null) { if (await gameRepo.AddQuestionToGameAsync(vm.GameId, histquestion.QuestionId, currentquestion.QuestionId) == null) { return(Redirect("/Error/0")); } } //getting next question var allquestions = await quizRepo.GetQuizQuestionsAsync(currentgame.QuizId); bool save = false; Question nextQuestion = new Question(); foreach (Question question in allquestions) { if (question.Description == currentquestion.Description) { save = true; } else if (save) { nextQuestion = question; save = false; break; } } //als save nog aanstaat betekend dat de we aan de laatste vraag zitten. if (save) { //finish the quiz QuizClass currentQuiz = await quizRepo.GetQuizByIdAsync(currentgame.QuizId); DateTime finishedtime = DateTime.Now; TimeSpan completetime = finishedtime.Subtract(currentgame.TimeStarted); //vm voor uitslag pagina opvullen var gameresults = await gameRepo.GetGameResults(vm.GameId); Finished_VM finished_vm = new Finished_VM() { QuizName = currentQuiz.Name, QuizDescription = currentQuiz.Description, userscore = currentgame.CorrectAnswers, maxscore = allquestions.Count(), completetime = finishedtime.Subtract(currentgame.TimeStarted), gameresults = gameresults }; currentgame.TimeFinished = finishedtime; if (await gameRepo.Update(currentgame) == null) { return(Redirect("/Error/0")); } return(View("Finished", finished_vm)); } vm = convertGame(currentgame.GameId, nextQuestion, vm.questionNr + 1); ViewBag.questionNr = vm.questionNr; ViewBag.questionId = nextQuestion.QuestionId; return(View("Play", vm)); }