Example #1
0
        public IActionResult CreateGameBet(GameBetViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Game currentGame = _gameLogic.GetGameById(model.GameId);

                    // Check if there are duplicate numbers; throw if there are any
                    if (model.Numbers.Distinct().Count() != model.Numbers.Count)
                    {
                        throw new FormatException("The numbers can't be the same");
                    }

                    CreateGameBetViewModel gameBet = new CreateGameBetViewModel
                    {
                        Numbers  = model.Numbers,
                        Sum      = model.Sum,
                        GameId   = model.GameId,
                        PlayerId = model.PlayerId
                    };

                    _gameBetsLogic.Create(gameBet);

                    decimal totalPot = 0;
                    _gameLogic.UpdatePot(currentGame, model.Sum, ref totalPot);

                    _logger.Log(LogLevel.Information, $"The pot has been risen to {totalPot}");

                    return
                        (RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    _logger.Log(LogLevel.Error, $"The following error occurred: {ex.Message} @ {GetType().Name}");
                    ModelState.AddModelError("", ex.Message);
                }
            }
            else
            {
                ModelState.AddModelError("", "Model State is not valid");
            }

            return(View(model));
        }