public async Task <IActionResult> CreateGameBet() { try { int gameId = _gameLogic.List().FirstOrDefault(g => g.GameProcessed == false)?.GameId ?? throw new NotFoundException(); User user = await _userLogic.GetCurrentUser(HttpContext.User); GameBetViewModel model = new GameBetViewModel { Numbers = new List <int>(7), PlayerId = user.PlayerId ?? 0, PlayersBalance = user.Player.Balance, GameId = gameId }; for (int i = 0; i < model.Numbers.Capacity; i++) { model.Numbers.Add(1); } return(View(model)); } catch (Exception ex) { _logger.Log(LogLevel.Error, $"The following error occurred: {ex.Message} @ {GetType().Name}"); ViewBag.ErrorMessage = ex.Message; return(RedirectToAction("Index")); } }
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)); }