public void Create(CreateGameBetViewModel model) { using (var transaction = _entityContext.Database.BeginTransaction()) { try { Player player = _playerLogic.GetById(model.PlayerId); if (player.Balance < model.Sum) { throw new ArgumentException("The player's balance is smaller and the sum provided"); } player.Balance -= model.Sum; player.NumberOfGamesPlayed += 1; player.TotalSpent += model.Sum; Transaction transactionModel = new Transaction { Sum = model.Sum, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow, Player = player }; GameBet bet = new GameBet { GameId = model.GameId, PlayerId = model.PlayerId, ChosenNumbers = JsonConvert.SerializeObject(model.Numbers), BetStatus = BetStatus.InProgress, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow, Transaction = transactionModel }; _entityContext.Update(player); _entityContext.Add(bet); _entityContext.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } }
public void Create(CreateGameBetViewModel model) { try { _gameBetsRepository.Create(model); } catch (MySqlException) { throw new ConnectionException(); } catch (Exception) { throw new OperationException("An error occured while creating new GameBet!"); } }
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)); }