public void Post(int gameId, string user, int?age = null)
        {
            using (var ctx = new QuizBoxContext())
            {
                var game = ctx.Game
                           .Include(g => g.Category)
                           .Include(g => g.GameQuestion)
                           .ThenInclude(g => g.Answer).First(g => g.Id == gameId);
                int totalNrQuestions = game.GameQuestion.Count();
                int nrCorrectAnswers = game.GameQuestion.Count(q => q.Answer.IsCorrect);
                var scorePercent     = ((double)nrCorrectAnswers / (double)totalNrQuestions) * 100;

                ctx.Highscore.Add(new Highscore()
                {
                    Category           = game.Category,
                    ScorePercent       = scorePercent,
                    EndTime            = DateTime.Now,
                    TimeElapsedSeconds = (DateTime.Now - game.Start).Seconds,
                    UserId             = user,
                    Age = age
                }
                                  );
                ctx.SaveChanges();
            }
        }
        public QuestionDTO GetQuestion(int gameId, int questionNr)
        {
            using (var ctx = new QuizBoxContext())
            {
                var game = ctx.Game
                           .Include(g => g.GameQuestion)
                           .ThenInclude(g => g.Question)
                           .ThenInclude(g => g.Category)
                           .Include(g => g.GameQuestion)
                           .ThenInclude(g => g.Question)
                           .ThenInclude(g => g.Answer)
                           .FirstOrDefault(g => g.Id == gameId);

                var question = game.GameQuestion.FirstOrDefault(q => q.Order == questionNr);
                if (question == null)
                {
                    return(null);
                }

                return(new QuestionDTO(question.Id, gameId, question.Question.Text, game.Category.Name, questionNr,
                                       game.GameQuestion.Count(),
                                       question.Question.Answer.Select(
                                           a => new QuestionChoiceDTO(a.Id, a.Text)).ToList()));
            }
        }
Example #3
0
        private void InitializeDatabase(DbInitializer initializer)
        {
            using (var context = new QuizBoxContext())
            {
                context.Database.Migrate();
            }

            initializer.Seed();
        }
 public void SaveAnswer(int gameId, int questionNr, int selectedAnswer)
 {
     using (var ctx = new QuizBoxContext())
     {
         var game     = ctx.Game.Include(g => g.GameQuestion).ThenInclude(g => g.Answer).First(g => g.Id == gameId);
         var question = game.GameQuestion.First(q => q.Order == questionNr);
         question.Answer = ctx.Answer.First(a => a.Id == selectedAnswer);
         ctx.SaveChanges();
     }
 }
Example #5
0
 public IActionResult Get()
 {
     using (var ctx = new QuizBoxContext())
     {
         if (ctx.Category.Any())
         {
             return(StatusCode(200));
         }
     }
     return(StatusCode(500));
 }
 public IEnumerable <CategoryDTO> Get()
 {
     using (var ctx = new QuizBoxContext())
     {
         return(ctx.Category.Select(
                    c => new CategoryDTO()
         {
             Id = c.Id,
             Name = c.Name,
             Description = c.Description
         }).ToList());
     }
 }
 public IEnumerable <ScoreDTO> Get()
 {
     using (var ctx = new QuizBoxContext())
     {
         return(ctx.Highscore.Select(
                    c => new ScoreDTO()
         {
             Id = c.Id,
             User = c.UserId,
             CategoryName = c.Category.Name,
             CategoryId = c.Category.Id,
             ScorePercent = c.ScorePercent,
             Duration = c.TimeElapsedSeconds
         }).OrderByDescending(c => c.ScorePercent).ThenBy(c => c.Duration).Take(10).ToList());
     }
 }
        public GameDTO StartNewGame(string category, int nrQuestions)
        {
            int gameId;

            using (var ctx = new QuizBoxContext())
            {
                var selectedCategory = ctx.Category.FirstOrDefault(c => c.Name == category);
                var newGame          = new Game()
                {
                    Category = selectedCategory,
                    Start    = DateTime.Now,
                    UserId   = "1234567890",
                };

                var questionsInGame = ctx.Question.Include(c => c.Category).Where(q => q.Category.Id == selectedCategory.Id).AsEnumerable().OrderBy(
                    q => Guid.NewGuid()).Take(5).ToList();
                newGame.GameQuestion = new List <GameQuestion>();
                for (int i = 1; i <= questionsInGame.Count(); i++)
                {
                    newGame.GameQuestion.Add(
                        new GameQuestion()
                    {
                        Game     = newGame,
                        Question = questionsInGame[i - 1],
                        Order    = i
                    });
                }
                ctx.Game.Add(newGame);
                ctx.SaveChanges();
                gameId = newGame.Id;
            }

            using (var ctx = new QuizBoxContext())
            {
                var game = ctx.Game.First(g => g.Id == gameId);
                return(new GameDTO
                {
                    Id = game.Id,
                    Start = game.Start
                });
            }
        }
        public GameResultDTO Finish(int gameId)
        {
            using (var ctx = new QuizBoxContext())
            {
                var game             = ctx.Game.Include(g => g.GameQuestion).ThenInclude(g => g.Answer).First(g => g.Id == gameId);
                int totalNrQuestions = game.GameQuestion.Count();
                int nrCorrectAnswers = game.GameQuestion.Count(q => q.Answer.IsCorrect);

                var result = new GameResultDTO()
                {
                    GameId           = gameId,
                    CategoryId       = game.CategoryId,
                    TotalNrQuestions = totalNrQuestions,
                    CorrectNrAnswers = nrCorrectAnswers,
                    StartTime        = game.Start
                };
                result.ScoreMessage = GetScoreMessage((float)result.CorrectNrAnswers / (float)result.TotalNrQuestions);
                return(result);
            }
        }
        public IEnumerable <CategoryDTO> Get()
        {
            using (var context = new QuizBoxContext())
            {
                context.Database.Migrate();
            }

            initializer.Seed();

            IEnumerable <CategoryDTO> categories = new List <CategoryDTO>();

            using (var ctx = new QuizBoxContext())
            {
                categories = ctx.Category.Select(
                    c => new CategoryDTO()
                {
                    Id          = c.Id,
                    Name        = c.Name,
                    Description = c.Description
                }).ToList();
                return(categories);
            }
        }
 public DbInitializer(QuizBoxContext context)
 {
     _context = context;
 }