public static void Seed(BillPaymentSystemContext context) { InsertUsers(context); InserCreditCards(context); InsertBankAccounts(context); InsertPaymentMethods(context); }
public string Execute(string[] args, BillPaymentSystemContext context) { decimal amount = decimal.Parse(args[1]); int id = int.Parse(args[0]); var output = "Insufficient funds!"; var user = context .Users .Include(x => x.PaymentMethods) .ThenInclude(x => x.BankAccount) .FirstOrDefault(x => x.UserId == id); if (user == null) { throw new ArgumentException($"User with id: {id} does not exist!"); } var firstFoundAcc = user.PaymentMethods.FirstOrDefault(x => x.BankAccount.Balance >= amount).BankAccount; if (firstFoundAcc != null) { firstFoundAcc.Balance -= amount; output = $"Successfuly withdrawed: {amount:f2}"; context.SaveChanges(); } return(output); }
static void PrintUserDetails(BillPaymentSystemContext dbContext, int userId) { var userInfo = dbContext.Users .Where(u => u.UserId == userId) .Select(u => new { Name = $"{u.FirstName} {u.LastName}", BankAccounts = u.PaymentMethods .Where(pm => pm.Type == PaymentMethodType.BankAccount) .Select(pm => pm.BankAccount) .ToArray(), CreditCards = u.PaymentMethods .Where(pm => pm.Type == PaymentMethodType.CreditCard) .Select(pm => pm.CreditCard) .ToArray() }) .FirstOrDefault(); if (userInfo == null) { Console.WriteLine($"User with id {userId} not found!"); } else { Console.WriteLine("User: "******"Bank Accounts:"); PrintBankAccounts(userInfo.BankAccounts); Console.WriteLine("Credit Cards:"); PrintCreditCards(userInfo.CreditCards); } }
private void SeedPaymentMethods(BillPaymentSystemContext context, Random rng) { ICollection <PaymentMethod> methods = new List <PaymentMethod>(); int cardsCount = context.CreditCards.Count(); int bankAccountsCount = context.BankAccounts.Count(); int usersCount = context.Users.Count(); var users = context.Users.ToList(); for (int i = 0; i < 20; i++) { PaymentMethod paymentMethod = new PaymentMethod(); var randomId = rng.Next(1, usersCount + 1); paymentMethod.UserId = randomId; if (i % 2 == 0) { paymentMethod.Type = PaymentType.CreditCard; randomId = rng.Next(1, cardsCount + 1); paymentMethod.CreditCardId = randomId; } else if (i % 3 == 0) { paymentMethod.Type = PaymentType.CreditCard; randomId = rng.Next(1, cardsCount + 1); paymentMethod.CreditCardId = randomId; randomId = rng.Next(1, bankAccountsCount + 1); paymentMethod.BankAccountId = randomId; } else { paymentMethod.Type = PaymentType.BankAccount; randomId = rng.Next(1, bankAccountsCount + 1); paymentMethod.BankAccountId = randomId; } if (!IsValid(paymentMethod)) { continue; } methods.Add(paymentMethod); } context.PaymentMethods.AddRange(methods); context.SaveChanges(); ; }
public string Execute(string[] args, BillPaymentSystemContext context) { StringBuilder builder = new StringBuilder(); int desiredId = int.Parse(args[0]); var user = context .Users .Include(x => x.PaymentMethods) .ThenInclude(x => x.BankAccount) .Include(x => x.PaymentMethods) .ThenInclude(x => x.CreditCard) .FirstOrDefault(x => x.UserId == desiredId); if (user != null) { builder.AppendLine($"User {user.FirstName} {user.LastName}"); builder.AppendLine($"Bank Accounts:"); var bankAccs = user.PaymentMethods .Where(x => x.Type == PaymentType.BankAccount) .ToList(); bankAccs.ForEach(x => { builder.AppendLine($"-- ID: {x.BankAccountId}"); builder.AppendLine($"--- Balance: {x.BankAccount.Balance:F2}"); builder.AppendLine($"--- Bank: {x.BankAccount.BankName}"); builder.AppendLine($"--- SWIFT: {x.BankAccount.SWIFT}"); }); builder.AppendLine($"Credit Cards:"); var cards = user.PaymentMethods .Where(x => x.Type == PaymentType.CreditCard) .ToList(); cards.ForEach(x => { builder.AppendLine($"-- ID: {x.CreditCardId}"); builder.AppendLine($"--- Limit: {x.CreditCard.Limit:F2}"); builder.AppendLine($"--- Money Owed: {x.CreditCard.MoneyOwed:F2}"); builder.AppendLine($"--- Limit Left: {x.CreditCard.LimitLeft:F2}"); builder.AppendLine($"--- Expiration Date: {x.CreditCard.ExpirationDate.ToString("yyyy/MM")}"); }); } else { builder.AppendLine($"User with id: {desiredId} not found!"); } return(builder.ToString()); }
public string ReadCommand(string[] args, BillPaymentSystemContext context) { var commandName = args[0]; var command = Assembly.GetExecutingAssembly() .GetTypes() .FirstOrDefault(x => x.Name == $"{commandName}Command"); var instance = (ICommand)Activator.CreateInstance(command); var output = instance.Execute(args.Skip(1).ToArray(), context); return(output); }
private static void InsertUsers(BillPaymentSystemContext context) { var users = UserInitializer.GetUsers(); for (int i = 0; i < users.Length; i++) { if (IsValid(users[i])) { context.Users.Add(users[i]); } } context.SaveChanges(); }
static void Main() { BillPaymentSystemContext dbContext = new BillPaymentSystemContext(); dbContext.Database.EnsureCreated(); // 02. Seed Some Dat Seed(dbContext); // 3. User Details int userId = int.Parse(Console.ReadLine()); PrintUserDetails(dbContext, userId); }
private static void InserCreditCards(BillPaymentSystemContext context) { var creditCards = CreditCardInitializer.GetCreditCards(); for (int i = 0; i < creditCards.Length; i++) { if (IsValid(creditCards[i])) { context.CreditCards.Add(creditCards[i]); } } context.SaveChanges(); }
private static void InsertBankAccounts(BillPaymentSystemContext context) { var bankAccounts = BankAccountInitializer.GetBankAccounts(); for (int i = 0; i < bankAccounts.Length; i++) { if (IsValid(bankAccounts[i])) { context.BankAccounts.Add(bankAccounts[i]); } } context.SaveChanges(); }
private static void InsertPaymentMethods(BillPaymentSystemContext context) { var patymentMethods = PaymentMethodInitializer.GetPaymentMethods(); for (int i = 0; i < patymentMethods.Length; i++) { if (IsValid(patymentMethods[i])) { context.PaymentMethods.Add(patymentMethods[i]); } } context.SaveChanges(); }
private void SeedBankAccounts(BillPaymentSystemContext context, Random rng) { ICollection <BankAccount> accounts = new List <BankAccount>(); var bankNames = new[] { "Pireus Bank", "OBB", "DSK Bank", "FiBank", "Ebaligo", "KTB", "VTB", "Raifaisen" }; var swiftCodes = new[] { "AAAA BB CC DDD", "BFAD 4E FC", "BBBB BB BB BBB", "IDKK ID ID IDK", "1234 56 78 900", "A3F2 3D 4A FKK" }; for (int i = 0; i < 9; i++) { BankAccount account = new BankAccount(); account.Balance = (decimal)rng.NextDouble() * 100M; account.BankName = bankNames[rng.Next(0, bankNames.Length)]; account.SWIFT = swiftCodes[rng.Next(0, swiftCodes.Length)]; if (!IsValid(account)) { continue; } accounts.Add(account); } context.BankAccounts.AddRange(accounts); context.SaveChanges(); }
private static void ConfigureDatabaase() { // Change connection string in Configure file. try { using (BillPaymentSystemContext context = new BillPaymentSystemContext()) { context.Database.EnsureCreated(); Console.WriteLine("Database created!"); Initializer.Seed(context); Console.WriteLine("Data insert success!"); } } catch (Exception e) { Console.WriteLine(e.Message); } }
private void SeedCreditCards(BillPaymentSystemContext context, Random rng) { ICollection <CreditCard> cards = new List <CreditCard>(); for (int i = 0; i < 9; i++) { CreditCard creditCard = new CreditCard(); creditCard.ExpirationDate = DateTime.Now.AddDays(rng.Next(-10, 51)); creditCard.Limit = (decimal)rng.NextDouble() * 100M; creditCard.MoneyOwed = (decimal)rng.NextDouble() * 10; if (!IsValid(creditCard)) { continue; } cards.Add(creditCard); } context.CreditCards.AddRange(cards); context.SaveChanges(); }
public void Run() { while (true) { try { string[] inputParams = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries); using (BillPaymentSystemContext context = new BillPaymentSystemContext()) { var command = this.commandInterpreter.InterpretCommand(inputParams); var result = command.Execute(); Console.WriteLine(result); } } catch (Exception e) { Console.WriteLine(e.Message); } } }
public void Run() { while (true) { string[] inputArgs = Console.ReadLine() .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); try { using (BillPaymentSystemContext context = new BillPaymentSystemContext()) { var output = this._cmdInterpretator.ReadCommand(inputArgs, context); Console.WriteLine(output); } } catch (Exception) { throw; } } }
private void SeedUsers(BillPaymentSystemContext context) { ICollection <User> users = new List <User>(); string[] firstNames = new[] { "Lilly", "Mirela", "Antonia", "Cvetan", "Pesho", "Gosho", null, "", "Vyara", "Maria" }; string[] secondNames = new[] { "Alexandrova", "Dimova", "Elenova", "Dimitrov", "Petkov", "Goshov", "", "Dobromirova", "Atanasova", "Blagova" }; string[] emails = new[] { "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "", "*****@*****.**", "*****@*****.**", "*****@*****.**" }; string[] passwords = new[] { "123456789", "asdasdas", "eeeeeeee", "12233444556", "wwwwwwawd3#d", "123456789", "123456789", "123", "12345678", "idkdkdkdk" }; for (int i = 0; i < firstNames.Length; i++) { var user = new User(); user.FirstName = firstNames[i]; user.LastName = secondNames[i]; user.Password = passwords[i]; user.Email = emails[i]; bool isUserValid = IsValid(user); if (isUserValid == false) { continue; } users.Add(user); } context.Users.AddRange(users); context.SaveChanges(); }
public DbInitializer(BillPaymentSystemContext context) { this._rng = new Random(); this._context = context; }
public BankService(BillPaymentSystemContext context) { this.Context = context; }
static void Seed(BillPaymentSystemContext dbContext) { BankAccount[] bankAccounts = new BankAccount[] { new BankAccount { Balance = 2000m, BankName = "Unicredit Bulbank", SwiftCode = "UNCRBGSF" }, new BankAccount { Balance = 1000m, BankName = "First Investment Bank", SwiftCode = "FINVBGSF" }, }; CreditCard[] creditCards = new CreditCard[] { new CreditCard { ExpirationDate = new DateTime(2050, 5, 10), Limit = 2000000m, MoneyOwned = 563594m } }; User[] users = new User[] { new User { FirstName = "Boiko", LastName = "NeBorisoff", Password = "******", Email = "*****@*****.**" } }; PaymentMethod[] paymentMethods = new PaymentMethod[] { new PaymentMethod { Type = PaymentMethodType.BankAccount, UserId = 1, BankAccountId = 1, }, new PaymentMethod { Type = PaymentMethodType.CreditCard, UserId = 1, CreditCardId = 1 } }; dbContext.AddRange(users); dbContext.SaveChanges(); dbContext.AddRange(bankAccounts); dbContext.SaveChanges(); dbContext.AddRange(creditCards); dbContext.SaveChanges(); dbContext.AddRange(paymentMethods); dbContext.SaveChanges(); }