Ejemplo n.º 1
0
        public async Task <IActionResult> Create(NewGameViewModel newGameVM)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var game = _mapper.Map <Game>(newGameVM);
                    var user = _mapper.Map <User>(newGameVM);

                    _context.Games.Add(game);
                    await _context.SaveChangesAsync();

                    user.GameId     = game.GameId;
                    user.UserIsHost = true;
                    _context.Users.Add(user);
                    await _context.SaveChangesAsync();

                    var cookieOptions = new CookieOptions
                    {
                        Expires = DateTime.UtcNow.AddDays(1)
                    };

                    Response.Cookies.Append("user", user.ToJsonBase64String(), cookieOptions);
                    return(Ok(Url.Action("Start", "Game", new { id = game.GameId })));
                }
                return(PartialView("_NewGamePartial", newGameVM));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"An exception occurred in {MethodBase.GetCurrentMethod().Name}");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("QuestionId,Answer,CorrectAnswer,Id")] Answers answers)
        {
            if (ModelState.IsValid)
            {
                _context.Add(answers);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            //ViewData["QuestionId"] = new SelectList(_context.Questions, "Id", "Id", answers.QuestionId);
            ViewData["QuestionName"] = new SelectList(_context.Questions, "Id", "Question", answers.QuestionId);
            return(View(answers));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("CategoryId,Question,PointValue,Id")] Questions questions)
        {
            if (ModelState.IsValid)
            {
                _context.Add(questions);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            //ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Id", questions.CategoryId);
            ViewData["CategoryName"] = new SelectList(_context.Categories, "Id", "Name", questions.CategoryId);
            return(View(questions));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Complete(int id)
        {
            try
            {
                var answer = await _context.Answers.FirstOrDefaultAsync(x => x.AnswerId == id);

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

                answer.HasBeenRead = true;
                _context.Update(answer);
                await _context.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"An exception occurred in {MethodBase.GetCurrentMethod().Name}");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Create([Bind("Name,Id,Active")] Categories categories)
        {
            if (ModelState.IsValid)
            {
                _context.Add(categories);
                await _context.SaveChangesAsync();

                var questions = new List <Questions>
                {
                    new Questions {
                        Question = "Edit and Enter Question 1", PointValue = 100, Categories = categories
                    },
                    new Questions {
                        Question = "Edit and Enter Question 2", PointValue = 200, Categories = categories
                    },
                    new Questions {
                        Question = "Edit and Enter Question 3", PointValue = 300, Categories = categories
                    },
                    new Questions {
                        Question = "Edit and Enter Question 4", PointValue = 400, Categories = categories
                    },
                    new Questions {
                        Question = "Edit and Enter Question 5", PointValue = 500, Categories = categories
                    }
                };
                _context.Questions.AddRange(questions);
                await _context.SaveChangesAsync();

                var answers = new List <Answers>
                {
                    new Answers {
                        Answer = "Edit and Enter Answer a", Question = questions[0], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer b", Question = questions[0], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer c", Question = questions[0], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer d", Question = questions[0], CorrectAnswer = false
                    },

                    new Answers {
                        Answer = "Edit and Enter Answer a", Question = questions[1], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer b", Question = questions[1], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer c", Question = questions[1], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer d", Question = questions[1], CorrectAnswer = false
                    },

                    new Answers {
                        Answer = "Edit and Enter Answer a", Question = questions[2], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer b", Question = questions[2], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer c", Question = questions[2], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer d", Question = questions[2], CorrectAnswer = false
                    },

                    new Answers {
                        Answer = "Edit and Enter Answer a", Question = questions[3], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer b", Question = questions[3], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer c", Question = questions[3], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer d", Question = questions[3], CorrectAnswer = false
                    },

                    new Answers {
                        Answer = "Edit and Enter Answer a", Question = questions[4], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer b", Question = questions[4], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer c", Question = questions[4], CorrectAnswer = false
                    },
                    new Answers {
                        Answer = "Edit and Enter Answer d", Question = questions[4], CorrectAnswer = false
                    }
                };
                _context.Answers.AddRange(answers);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(categories));
        }