Esempio n. 1
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);
            }
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        public async Task <MonthsTransactionsModelDTO> GetMonthsTransactions(DateTime timePeriod,
                                                                             string transactionType,
                                                                             int monthCount)
        {
            ServiceValidator.IsInputStringEmptyOrNull(transactionType);
            ServiceValidator.ValueIsBetween(monthCount, 1, 12);

            var dbQuery = await this.dbContext.Transactions
                          .Include(tt => tt.TransactionType)
                          .Where(t => t.TransactionType.Name == transactionType)
                          .ToListAsync();


            var resultModel = FiltarByMonth(timePeriod, monthCount, dbQuery);

            //CHECK FOR MONTH NUMBER !!

            return(resultModel);
        }