public static void Main() { using (var db = new BillsPaymentDbContext()) { SeedData(db); UserInfoById(db); } }
private static void UserInfoById(BillsPaymentDbContext db) { var id = int.Parse(Console.ReadLine()); var user = db .Users .Where(u => u.Id == id) .Select(u => new { u.FirstName, u.LastName, u.Email, u.Password, PaymentMethods = u.PaymentMethods.Select(pm => pm.Type) }) .FirstOrDefault(); Console.WriteLine($"{user.FirstName} {user.LastName} - {user.Email}, {user.Password}"); foreach (var method in user.PaymentMethods) { Console.WriteLine(method); } }
public DbSeeder(BillsPaymentDbContext db) { this.db = db; this.random = new Random(); }
public decimal PayWithBankAsMuchAsPossible(IEnumerable <BankAccount> bankAccounts, decimal amount, BillsPaymentDbContext db) { foreach (var account in bankAccounts) { db.Entry(account).State = EntityState.Unchanged; if (account.Balance >= amount) { account.Withdraw(amount); amount = 0; break; } amount -= account.Balance; account.Withdraw(account.Balance); } return(amount); }
public UserService(BillsPaymentDbContext db) { this.db = db; }
public void PayWithCreditCards(decimal amount, IEnumerable <CreditCard> creditCards, BillsPaymentDbContext db) { if (creditCards.Select(c => c.LimitLeft).Sum() < amount) { throw new InvalidOperationException(AmountGreaterThanPossibilities); } foreach (var card in creditCards) { db.Entry(card).State = EntityState.Unchanged; if (card.LimitLeft >= amount) { card.Withdraw(amount); return; } amount -= card.LimitLeft; card.Withdraw(card.LimitLeft); } }
private static void SeedData(BillsPaymentDbContext db) { var bankAccounts = new List <BankAccount> { new BankAccount { Balance = 100m, BankName = "Shit Bank", SwiftCode = "1234" }, new BankAccount { Balance = 340m, BankName = "Shit Bank", SwiftCode = "1234" }, new BankAccount { Balance = 2300m, BankName = "Shit Bank", SwiftCode = "1234" }, }; var creditCards = new List <CreditCard> { new CreditCard { Limit = 1000m, ExpirationDate = DateTime.Now, MoneyOwed = 2000m } }; var users = new List <User> { new User { FirstName = "Pesho", LastName = "Goshov", Email = "*****@*****.**", Password = "******" }, new User { FirstName = "Minka", LastName = "Svirkata", Email = "*****@*****.**", Password = "******" }, new User { FirstName = "Vanq", LastName = "Miteva", Email = "*****@*****.**", Password = "******" } }; var paymentMethods = new List <PaymentMethod> { new PaymentMethod { BankAccountId = 8, CreditCardId = 3, Type = MethodType.BankAccount, UserId = 7 } }; db .Users .AddRange(users); db .CreditCards .AddRange(creditCards); db .BankAccounts .AddRange(bankAccounts); db .PaymentMethods .AddRange(paymentMethods); db.SaveChanges(); }