public async Task ChangeBet(string betId, int[] numbers, int price) { if (price <= 0) { throw new ArgumentException("Price must be positive number."); } using (var session = Casino.GetSessionAsync) { var bet = await session.LoadAsync <Bet>(betId).ConfigureAwait(false); var user = await session.LoadAsync <User>(Id).ConfigureAwait(false); await Lottery.ValidateOpen(session, bet.LotteryId).ConfigureAwait(false); user.Credit += bet.Price; if (user.Credit < price) { throw new InsufficientFunds("Not enough credit"); } bet.Price = price; bet.Numbers = numbers; user.Credit -= price; await session.StoreAsync(bet).ConfigureAwait(false); await session.StoreAsync(user).ConfigureAwait(false); await session.SaveChangesAsync().ConfigureAwait(false); } }
public async Task PlaceBet(string lotteryId, int[] numbers, int price) { if (price <= 0) { throw new ArgumentException("Price must be positive number."); } using (var session = Casino.GetSessionAsync) { var user = await session.LoadAsync <User>(Id).ConfigureAwait(false); if (user.Credit < price) { throw new InsufficientFunds("Not enough credit"); } var bet = new Bet { UserId = Id, LotteryId = lotteryId, Numbers = numbers, Price = price, BetStatus = Bet.Status.Active }; await session.StoreAsync(bet).ConfigureAwait(false); session.Advanced.GetMetadataFor(bet)[Constants.Documents.Metadata.Expires] = DateTime.UtcNow.AddMinutes(10); user.Credit -= price; user.Bets.Add(bet.Id); await Lottery.ValidateOpen(session, lotteryId).ConfigureAwait(false); session.CountersFor(lotteryId).Increment(DateTime.UtcNow.ToString("yyyy MMMM dd hh:mm")); await session.SaveChangesAsync().ConfigureAwait(false); } }
public async Task DeleteBet(string betId) { using (var session = Casino.GetSessionAsync) { var user = await session.LoadAsync <User>(Id).ConfigureAwait(false); var bet = await session.LoadAsync <Bet>(betId).ConfigureAwait(false); await Lottery.ValidateOpen(session, bet.LotteryId).ConfigureAwait(false); bet.BetStatus = Bet.Status.Deleted; if (user.Bets.Remove(betId)) { user.Credit += bet.Price; await session.StoreAsync(bet).ConfigureAwait(false); await session.StoreAsync(user).ConfigureAwait(false); await session.SaveChangesAsync().ConfigureAwait(false); } } }