public async Task <ActionResult <WebApiResponse> > CreateBet(long roundId, BetCreateDTO betCreateDTO) { UserAccount userAccount = await GetUserAccountOrAbortIfAuthUserIsNotAccountOwner(betCreateDTO.AccountId); await AbortIfAccountHasInsufficientFundsOrInputAmountIsZero(userAccount, betCreateDTO.Amount); Round round = await GetRoundOrAbortIfRoundStatusClosed(roundId); RouletteRule rouletteRule = await GetRouletteRuleOrAbortIfNotFound(betCreateDTO, round); await AppDbContext.BeginTransactionAsync(); AccountTransaction accountTransaction = await GetAndSaveAccountTransaction(userAccount, betCreateDTO.Amount); Bet bet = new Bet { AccountTransaction = accountTransaction, RouletteRule = rouletteRule, PaymentRatio = rouletteRule.Pay, Round = round, UserRegister = accountTransaction.UserRegister, State = await AppDbContext.FindGenericElementByIdAsync <BetState>((long)BetStates.Active) }; await CreateFromEntityAsync(bet); await SaveBetNumbersList(bet, betCreateDTO.Numbers); await AppDbContext.CommitTransactionAsync(); return(MapEntityAndMakeResponse(bet)); }
private async Task <RouletteRule> GetRouletteRuleOrAbortIfNotFound(BetCreateDTO betCreateDTO, Round round) { RouletteRule rouletteRule = await AppDbContext.Set <RouletteRule>() .Include(x => x.Roulette.Type) .Include(x => x.Roulette.State) .FirstOrDefaultAsync( x => x.Id == betCreateDTO.RouletteRuleId && x.Roulette.Id == round.Roulette.Id && x.DeletedAt == null); if (rouletteRule == null) { throw new WebApiException(System.Net.HttpStatusCode.Forbidden, "the rule not found!"); } return(rouletteRule); }