private bool FindSubsetInDatabase(QuickstarterQuestion query, string category, string difficulty) { if (!string.IsNullOrEmpty(category) && (category != query.Category)) { return(false); } if (!string.IsNullOrEmpty(difficulty) && (category != query.Category)) { return(false); } return(true); }
public async Task GetQuickstarter(string roomName, string category, string difficulty) { using (var scope = _sp.CreateScope()) { var dbContext = scope.ServiceProvider.GetRequiredService <TriviaDbContext>(); var questions = new List <QuickstarterDTO>(); var numQuestions = 4; // for now assume category and difficulty are always sent ffs // fetch list of possible questions, randomly generate numbers and pull elements from list //var dataSet = dbContext.QuickstarterQuestions.Where(r => (r.Category == category && r.Difficulty == difficulty)).ToList(); var categories = JsonConvert.DeserializeObject <List <string> >(category); for (int i = 0; i < numQuestions; i++) { var dataSet = dbContext.QuickstarterQuestions.Where(r => (r.Category == categories[i] && r.Difficulty == difficulty)).ToList(); Random rand = new Random(); int randValue = rand.Next(0, dataSet.Count); QuickstarterQuestion question = dataSet.Skip(randValue).Take(1).First(); dataSet.RemoveAt(randValue); questions.Add(new QuickstarterDTO() { Id = question.Id, Category = question.Category, Difficulty = question.Difficulty, Question = question.Question, Answers = JsonConvert.DeserializeObject <List <string> >(question.Answers) }); } var session = dbContext.Sessions.FirstOrDefault(r => r.Id == roomName); if (session == null) { return; } session.CurrentQuestions = JsonConvert.SerializeObject(questions); dbContext.SaveChanges(); await Clients.Group(roomName).SendAsync("getQuickstarter", questions); } }
public List <QuickstarterDTO> Get([FromQuery] string category, [FromQuery] int numQuestions, [FromQuery] string difficulty) { if (numQuestions < 0) { throw new ArgumentException("Parameter must be a positive integer", "numQuestions"); } if (numQuestions == 0) { return(null); } //if (difficulty != ("easy" || "medium || "hard")) throw new ArgumentException("Parameter must be easy, medium, or hard", "difficulty"); var response = new List <QuickstarterDTO>(); // for now assume category and difficulty are always sent ffs // fetch list of possible questions, randomly generate numbers and pull elements from list var dataSet = this.context.QuickstarterQuestions.Where(r => (r.Category == category && r.Difficulty == difficulty)).ToList(); for (int i = 0; i < numQuestions; i++) { Random rand = new Random(); int randValue = rand.Next(0, dataSet.Count); QuickstarterQuestion question = dataSet.Skip(randValue).Take(1).First(); dataSet.RemoveAt(randValue); response.Add(new QuickstarterDTO() { Id = question.Id, Category = question.Category, Difficulty = question.Difficulty, Question = question.Question, Answers = JsonConvert.DeserializeObject <List <string> >(question.Answers) }); } return(response); }
private static void SeedData(TriviaDbContext context) { // Deletes the database context.Database.EnsureDeleted(); // Creates the database fresh context.Database.EnsureCreated(); // Seed Models var qq1 = new QuickstarterQuestion() { Id = 1, Category = "Science & Technology", Difficulty = "easy", Question = "What does CPU stand for?", CorrectAnswer = "Central Processing Unit", Answers = @"[""Central Process Unit"", ""Computer Personal Unit"", ""Central Processor Unit"", ""Central Processing Unit""]" }; var qq2 = new QuickstarterQuestion() { Id = 2, Category = "Science & Technology", Difficulty = "easy", Question = "What is the longest bone in the human body?", CorrectAnswer = "Femur", Answers = @"[""Scapula"", ""Femur"", ""Fibula"", ""Ulna""]" }; var qq3 = new QuickstarterQuestion() { Id = 3, Category = "Science & Technology", Difficulty = "easy", Question = "The element involved in making human blood red is which of the following?", CorrectAnswer = "Iron", Answers = @"[""Copper"", ""Iridium"", ""Cobalt"", ""Iron""]" }; var qq4 = new QuickstarterQuestion() { Id = 4, Category = "Science & Technology", Difficulty = "easy", Question = "The biggest distinction between a eukaryotic cell and a prokaryotic cell is:", CorrectAnswer = "The presence or absence of a nucleus", Answers = @"[""The presence or absence of certain organelles"", ""The overall size"", ""The presence or absence of a nucleus"", ""The mode of reproduction""]" }; var qq5 = new QuickstarterQuestion() { Id = 5, Category = "General Knowledge", Difficulty = "easy", Question = "What type of animal was Harambe, who was shot after a child fell into it's enclosure at the Cincinnati Zoo?", CorrectAnswer = "Gorilla", Answers = @"[""Gorilla"", ""Tiger"", ""Panda"", ""Crocodile""]" }; var qq6 = new QuickstarterQuestion() { Id = 6, Category = "General Knowledge", Difficulty = "easy", Question = "Which candy is NOT made by Mars?", CorrectAnswer = "Almond Joy", Answers = @"[""Snickers"", ""M&M's"", ""Almond Joy"", ""Twix""]" }; var qq7 = new QuickstarterQuestion() { Id = 7, Category = "General Knowledge", Difficulty = "easy", Question = "The Flag of the European Union has how many stars on it?", CorrectAnswer = "12", Answers = @"[""10"", ""12"", ""14"", ""16""]" }; var qq8 = new QuickstarterQuestion() { Id = 8, Category = "General Knowledge", Difficulty = "easy", Question = "What is the Zodiac symbol for Gemini?", CorrectAnswer = "Twins", Answers = @"[""Fish"", ""Maiden"", ""Twins"", ""Scales""]" }; var qq9 = new QuickstarterQuestion() { Id = 9, Category = "Entertainment", Difficulty = "easy", Question = "What is the homeworld of the Elites from Halo?", CorrectAnswer = "Sanghelios", Answers = @"[""Te"", ""Sanghelios"", ""Dosaic"", ""Eayn""]" }; var qq10 = new QuickstarterQuestion() { Id = 10, Category = "Entertainment", Difficulty = "easy", Question = "What is the name of the world that the MMO RuneScape takes place in?", CorrectAnswer = "Gielinor", Answers = @"[""Glindor"", ""Zaros"", ""Azeroth"", ""Gielinor""]" }; var qq11 = new QuickstarterQuestion() { Id = 11, Category = "Entertainment", Difficulty = "easy", Question = "What is the name of the game developer who created Call Of Duty: Zombies?", CorrectAnswer = "Treyarch", Answers = @"[""Sledgehammer Games"", ""Treyarch"", ""Infinity Ward"", ""Activision""]" }; var qq12 = new QuickstarterQuestion() { Id = 12, Category = "Entertainment", Difficulty = "easy", Question = "Who is the creator of the Super Smash Bros. Series?", CorrectAnswer = "Masahiro Sakurai", Answers = @"[""Masahiro Sakurai"", ""Reggie Fils-Aime"", ""Bill Trinen"", ""Hideo Kojima""]" }; var qq13 = new QuickstarterQuestion() { Id = 13, Category = "World Knowledge", Difficulty = "easy", Question = "Who is the creator of Filip Bicki?", CorrectAnswer = "Poland", Answers = @"[""Poland"", ""Reggie Fils-Aime"", ""Bill Trinen"", ""Hideo Kojima""]" }; IList <QuickstarterQuestion> quickstarterQuestions = new List <QuickstarterQuestion>() { qq1, qq2, qq3, qq4, qq5, qq6, qq7, qq8, qq9, qq10, qq11, qq12, qq13 }; context.QuickstarterQuestions.AddRange(quickstarterQuestions); // Saves changes context.SaveChanges(); }