Ejemplo n.º 1
0
        private static void Seed(PaymentSystemDBContex db)
        {
            var user = new User()
            {
                FirstName = "Guy",
                LastName  = "Gilbert",
                Email     = "*****@*****.**",
                Password  = "******"
            };

            var creditCard = new CreditCard()
            {
                Limit          = 800.00M,
                MoneyOwed      = 100.00M,
                ExpirationDate = DateTime.ParseExact("20.03.2020", "dd.MM.yyyy", null)
            };

            var bankAccounts = new BankAccount[]
            {
                new BankAccount()
                {
                    Balance   = 2000M,
                    BankName  = "Unicredit Bulbank",
                    SwiftCode = "UNCRBGSF"
                },

                new BankAccount()
                {
                    Balance   = 1000M,
                    BankName  = "First Investment Bank",
                    SwiftCode = "FINVBGSF"
                }
            };

            var paymentMethods = new PaymentMethod[]
            {
                new PaymentMethod()
                {
                    User       = user,
                    CreditCard = creditCard,
                    Type       = PaymentMethodType.CreditCard
                },

                new PaymentMethod()
                {
                    User        = user,
                    BankAccount = bankAccounts[0],
                    Type        = PaymentMethodType.BankAccount
                },

                new PaymentMethod()
                {
                    User        = user,
                    BankAccount = bankAccounts[1],
                    Type        = PaymentMethodType.BankAccount
                }
            };

            db.Users.Add(user);
            db.CreditCards.Add(creditCard);
            db.BankAccounts.AddRange(bankAccounts);
            db.PaymentMethods.AddRange(paymentMethods);

            db.SaveChanges();
        }
Ejemplo n.º 2
0
        private static void PayBills(int userId, decimal amount, PaymentSystemDBContex context)
        {
            var user = context.Users.Find(userId);

            if (user == null)
            {
                Console.WriteLine($"User with id {userId} not found!");
                return;
            }

            decimal usersMoney = 0.0m;

            var bankAccounts = context
                               .BankAccounts.Join(context.PaymentMethods,
                                                  (ba => ba.BankAccountId),
                                                  (p => p.BankAccountId),
                                                  (ba, p) => new
            {
                UserId        = p.UserId,
                BankAccountId = ba.BankAccountId,
                Balance       = ba.Balance
            })
                               .Where(ba => ba.UserId == userId)
                               .ToList();


            var creditCards = context
                              .CreditCards.Join(context.PaymentMethods,
                                                (c => c.CreditCardId),
                                                (p => p.CreditCardId),
                                                (c, p) => new
            {
                UserId       = p.UserId,
                CreditCardId = c.CreditCardId,
                LimitLeft    = c.LimitLeft
            })
                              .Where(c => c.UserId == userId)
                              .ToList();

            usersMoney += bankAccounts.Sum(b => b.Balance);
            usersMoney += creditCards.Sum(c => c.LimitLeft);

            if (usersMoney < amount)
            {
                throw new InvalidOperationException("Insufficient funds!");
            }

            bool isPayBills = false;

            foreach (var bankAccount in bankAccounts.OrderBy(b => b.BankAccountId))
            {
                var currentAccount = context.BankAccounts.Find(bankAccount.BankAccountId);

                if (amount <= currentAccount.Balance)
                {
                    currentAccount.Withdraw(amount);
                    isPayBills = true;
                }
                else
                {
                    amount -= currentAccount.Balance;
                    currentAccount.Withdraw(currentAccount.Balance);
                }

                if (isPayBills)
                {
                    context.SaveChanges();
                    return;
                }
            }

            foreach (var creditCard in creditCards.OrderBy(c => c.CreditCardId))
            {
                var currentCreditCard = context.CreditCards.Find(creditCard.CreditCardId);

                if (amount <= currentCreditCard.LimitLeft)
                {
                    currentCreditCard.Withdraw(amount);
                    isPayBills = true;
                }
                else
                {
                    amount -= currentCreditCard.LimitLeft;
                    currentCreditCard.Withdraw(currentCreditCard.LimitLeft);
                }

                if (isPayBills)
                {
                    context.SaveChanges();
                    return;
                }
            }
        }