Beispiel #1
0
        public TransactionService(CasinoContext dbContext, ICurrencyRateApiService currencyService)
        {
            ServiceValidator.ObjectIsNotEqualNull(dbContext);

            this.dbContext       = dbContext;
            this.currencyService = currencyService;
        }
        public async Task <Wallet> CreateWallet(string userId, int currencyId)
        {
            var wallet = await this.context.Wallets.FirstOrDefaultAsync(wa => wa.UserId == userId);

            var currency = await this.context.Currencies.FirstOrDefaultAsync(cu => cu.Id == currencyId);

            if (wallet != null)
            {
                throw new EntityAlreadyExistsException("The user already has wallet");
            }

            ServiceValidator.ObjectIsNotEqualNull(currency);

            var newWallet = new Wallet()
            {
                Id       = Guid.NewGuid().ToString(),
                UserId   = userId,
                Currency = currency
            };

            context.Wallets.Add(newWallet);
            await context.SaveChangesAsync();

            return(newWallet);
        }
Beispiel #3
0
        public async Task <Transaction> AddStakeTransaction(
            string userId,
            double amountInUserCurrency,
            string description)
        {
            ServiceValidator.IsInputStringEmptyOrNull(userId);
            ServiceValidator.IsInputStringEmptyOrNull(description);
            ServiceValidator.CheckStringLength(description, 10, 100);
            ServiceValidator.ValueIsBetween(amountInUserCurrency, 0, double.MaxValue);

            var userWin = await this.dbContext.Users
                          .Include(w => w.Wallet)
                          .ThenInclude(w => w.Currency)
                          .FirstOrDefaultAsync(u => u.Id == userId && u.IsDeleted != true);

            ServiceValidator.ObjectIsNotEqualNull(userWin);

            var userCurrency = userWin.Wallet.Currency.Name;
            var bankRates    = await this.currencyService.GetRatesAsync();

            double normalisedCurrency = 0;

            if (bankRates.ContainsKey(userCurrency))
            {
                double normalisedUserCurrency = bankRates[userCurrency];
                normalisedCurrency = amountInUserCurrency / normalisedUserCurrency;
            }
            else
            {
                throw new EntityCurrencyNotFoundException("Unknown user currency");
            }

            var newTransaction = new Transaction()
            {
                UserId            = userId,
                OriginalAmount    = amountInUserCurrency,
                Description       = description,
                TransactionTypeId = 2,
                NormalisedAmount  = normalisedCurrency
            };

            userWin.Wallet.NormalisedBalance -= normalisedCurrency;
            userWin.Wallet.DisplayBalance     = userWin.Wallet.NormalisedBalance * bankRates[userCurrency];

            if (userWin.Wallet.NormalisedBalance < 0)
            {
                throw new InsufficientFundsException("Insufficient funds for the requested operation");
            }
            else
            {
                await this.dbContext.Transactions.AddAsync(newTransaction);

                await this.dbContext.SaveChangesAsync();

                return(newTransaction);
            }
        }
Beispiel #4
0
        public async Task <Transaction> AddDepositTransaction(
            string userId,
            string cardId,
            double amountInUserCurrency,
            string description)
        {
            ServiceValidator.IsInputStringEmptyOrNull(userId);
            ServiceValidator.IsInputStringEmptyOrNull(cardId);
            ServiceValidator.IsInputStringEmptyOrNull(description);
            ServiceValidator.CheckStringLength(description, 10, 100);
            ServiceValidator.ValueIsBetween(amountInUserCurrency, 0, double.MaxValue);

            var userWin = await this.dbContext.Users
                          .Include(w => w.Wallet)
                          .ThenInclude(wall => wall.Currency)
                          .FirstOrDefaultAsync(u => u.Id == userId && u.IsDeleted != true);

            var bankCard = await this.dbContext.BankCards.FirstOrDefaultAsync(bc => bc.Id == cardId);

            ServiceValidator.ObjectIsNotEqualNull(userWin);
            ServiceValidator.ObjectIsNotEqualNull(bankCard);

            var userCurrency = userWin.Wallet.Currency.Name;
            var bankRates    = await this.currencyService.GetRatesAsync();

            double normalisedCurrency = 0;

            if (bankRates.ContainsKey(userCurrency))
            {
                double normalisedUserCurrency = bankRates[userCurrency];
                normalisedCurrency = amountInUserCurrency / normalisedUserCurrency;
            }
            else
            {
                throw new EntityCurrencyNotFoundException("Unknown user currency");
            }

            var newTransaction = new Transaction()
            {
                UserId            = userId,
                OriginalAmount    = amountInUserCurrency,
                Description       = description,
                TransactionTypeId = 3,
                NormalisedAmount  = normalisedCurrency,
                CardId            = bankCard.Id
            };

            userWin.Wallet.NormalisedBalance += normalisedCurrency;
            userWin.Wallet.DisplayBalance     = userWin.Wallet.NormalisedBalance * bankRates[userCurrency];
            bankCard.MoneyAdded += amountInUserCurrency;

            await this.dbContext.Transactions.AddAsync(newTransaction);

            await this.dbContext.SaveChangesAsync();

            return(newTransaction);
        }
Beispiel #5
0
        public async Task <User> RetrieveUser(string id)
        {
            var user = await this.context.Users
                       .Include(us => us.Wallet)
                       .ThenInclude(wa => wa.Currency)
                       .FirstOrDefaultAsync(us => us.Id == id);

            ServiceValidator.ObjectIsNotEqualNull(user);
            return(user);
        }
Beispiel #6
0
        public async Task <User> EditUserAlias(string alias, string id)
        {
            ServiceValidator.IsInputStringEmptyOrNull(alias);
            var user = await this.context.Users.FirstOrDefaultAsync(us => us.Id == id && !us.IsDeleted);

            ServiceValidator.ObjectIsNotEqualNull(user);
            user.Alias = alias;
            await this.context.SaveChangesAsync();

            return(user);
        }
Beispiel #7
0
        //TO DO: FIX THIS
        public async Task <BankCard> GetCard(string cardNumber)
        {
            ServiceValidator.IsInputStringEmptyOrNull(cardNumber);
            //ServiceValidator.ValidateCardNumber(cardNumber);

            var bankCardQuery = await this.dbContext.BankCards
                                .FirstOrDefaultAsync(c => c.Id == cardNumber && c.IsDeleted == false);

            ServiceValidator.ObjectIsNotEqualNull(bankCardQuery);

            return(bankCardQuery);
        }
Beispiel #8
0
        public async Task <Transaction> RetrieveUserTransaction(string id)
        {
            ServiceValidator.IsInputStringEmptyOrNull(id);

            var user = await this.dbContext.Transactions
                       .Include(u => u.User)
                       .Include(tt => tt.TransactionType)
                       .Include(uc => uc.User.Wallet.Currency)
                       .FirstOrDefaultAsync(t => t.Id == id);

            ServiceValidator.ObjectIsNotEqualNull(user);

            return(user);
        }
        public async Task <Wallet> RetrieveWallet(string userId)
        {
            //TO DO: OPTIMIZE!!!
            var wallet = await this.context.Wallets
                         .Include(wa => wa.User)
                         .ThenInclude(us => us.Cards)
                         .Include(wa => wa.User)
                         .ThenInclude(us => us.Transactions)
                         .ThenInclude(tr => tr.TransactionType)
                         .FirstOrDefaultAsync(wa => wa.UserId == userId);

            ServiceValidator.ObjectIsNotEqualNull(wallet);
            return(wallet);
        }
Beispiel #10
0
        public async Task <User> UnLockUser(string id)
        {
            var user = await this.context.Users.FirstOrDefaultAsync(us => us.Id == id && !us.IsDeleted);

            ServiceValidator.ObjectIsNotEqualNull(user);
            var role = context.UserRoles.FirstOrDefault(ur => ur.UserId == id);

            ServiceValidator.ObjectIsNotEqualNull(role);

            user.Locked = false;
            await this.context.SaveChangesAsync();

            return(user);
        }
Beispiel #11
0
        public async Task <double> Deposit(string cardNumber, double amount)
        {
            ServiceValidator.IsInputStringEmptyOrNull(cardNumber);
            ServiceValidator.ValueIsBetween(amount, 0, double.MaxValue);

            var bankCardQuery = await this.dbContext.BankCards
                                .FirstOrDefaultAsync(c => c.Id == cardNumber && c.IsDeleted == false);

            ServiceValidator.ObjectIsNotEqualNull(bankCardQuery);
            ServiceValidator.CheckCardExpirationDate(bankCardQuery.Expiration);

            bankCardQuery.MoneyAdded += amount;
            await this.dbContext.SaveChangesAsync();


            return(amount);
        }
Beispiel #12
0
        public async Task <User> LockUser(string id)
        {
            var user = await this.context.Users.FirstOrDefaultAsync(us => us.Id == id && !us.IsDeleted);

            ServiceValidator.ObjectIsNotEqualNull(user);
            var role = context.UserRoles.FirstOrDefault(ur => ur.UserId == id);

            ServiceValidator.ObjectIsNotEqualNull(role);
            if (role.RoleId == "1")
            {
                throw new InvalidAdministratorOperationException("You cannot lock other administrators");
            }
            user.Locked = true;
            await this.context.SaveChangesAsync();

            return(user);
        }
Beispiel #13
0
        public async Task <User> PromoteUser(string id)
        {
            var user = await this.context.Users.FirstOrDefaultAsync(us => us.Id == id && !us.IsDeleted);

            var userRole = await this.context.UserRoles.FirstOrDefaultAsync(ur => ur.UserId == id);

            ServiceValidator.ObjectIsNotEqualNull(user);
            ServiceValidator.ObjectIsNotEqualNull(userRole);

            this.context.UserRoles.Remove(userRole);
            this.context.UserRoles.Add(new IdentityUserRole <string>()
            {
                UserId = id, RoleId = "1"
            });

            await this.context.SaveChangesAsync();

            return(user);
        }
Beispiel #14
0
        public CardService(CasinoContext dbContext)
        {
            ServiceValidator.ObjectIsNotEqualNull(dbContext);

            this.dbContext = dbContext;
        }