private string AddInterest(string[] input) { if (input.Length != 1) { throw new ArgumentException("Invalid input"); } if (!AuthenticationManager.IsAuthenticated()) { throw new InvalidOperationException("User should log in first"); } // AddInterest <Account number> string accountNumber = input[0]; decimal currentBalance; using (BankingSystemContext context = new BankingSystemContext()) { User user = context.Users.Attach(AuthenticationManager.GetCurrentUser()); SavingAccount savingAccount = user.SavingAccounts.FirstOrDefault(a => a.AccountNumber == accountNumber); if (savingAccount == null) { throw new ArgumentException($"Account {accountNumber} does not exist"); } savingAccount.AddInterest(); context.SaveChanges(); currentBalance = savingAccount.Balance; } return($"Interest added to account {accountNumber}, current balance = {currentBalance:f2}"); }
public async Task Create_An_Account_Existing_Customer_Success() { // ARRANGE var options = GetInMemoryOptions("Create_An_Account_Existing_Customer_Success"); var customer = new Customer() { FirstName = "John", LastName = "Wick" }; using (var context = new BankingSystemContext(options)) { Seed_Create_An_Account_Existing_Customer_Success(context, customer); SeedIBANMasterData(context); var accountService = new AccountService(context); var controller = new AccountController(context, accountService); // ACT var jsonResult = await controller.CreateAccount(customer, 1500) as JsonResult; // ASSERT Assert.NotNull(jsonResult); Assert.Equal(200, jsonResult.StatusCode.GetValueOrDefault()); var value = jsonResult.Value as Response; Assert.NotNull(value.Result); var createAccountResponse = value.Result as CreateAccountResponse; Assert.NotNull(createAccountResponse.IBAN); Assert.Equal(1500, createAccountResponse.TotalAmount); var totalCustomer = context.Customers.Count(); Assert.Equal(1, totalCustomer); } }
private string ListAccounts() { if (!AuthenticationManager.IsAuthenticated()) { throw new InvalidOperationException("User should log in first"); } StringBuilder builder = new StringBuilder(); using (BankingSystemContext context = new BankingSystemContext()) { User user = context.Users.Attach(AuthenticationManager.GetCurrentUser()); builder.AppendLine("Saving Accounts:"); foreach (SavingAccount userSavingAccount in user.SavingAccounts) { builder.AppendLine($"--{userSavingAccount.AccountNumber} {userSavingAccount.Balance:f2}"); } builder.AppendLine("Checking Accounts:"); foreach (CheckingAccount checkingAccount in user.CheckingAccounts) { builder.AppendLine($"--{checkingAccount.AccountNumber} {checkingAccount.Balance:f2}"); } } return(builder.ToString().Trim()); }
public async Task Transfer_Not_Enough_Money_Error() { // ARRANGE var options = GetInMemoryOptions("Transfer_Not_Enough_Money_Error"); var transferIBAN = "NL92ABNA8946051078"; var receiveIBAN = "NL05INGB6289099205"; using (var context = new BankingSystemContext(options)) { Seed_Transfer_Success(context, transferIBAN, receiveIBAN); // ACT var accountService = new AccountService(context); var actionService = new ActionService(context, accountService); var controller = new ActionController(context, accountService, actionService); // ACT var jsonResult = await controller.Transfer(new TransferReqeuest() { FromIBAN = transferIBAN, ToIBAN = receiveIBAN, Amount = 1000000 }) as JsonResult; // ASSERT Assert.NotNull(jsonResult); Assert.Equal(400, jsonResult.StatusCode.GetValueOrDefault()); var value = jsonResult.Value as Response; Assert.Equal(Entity.Constant.TRANSFER_MONEY_NOT_ENOUGH, value.Error); } }
private string LoginUser(string[] input) { if (input.Length != 2) { throw new ArgumentException("Invalid input"); } if (AuthenticationManager.IsAuthenticated()) { throw new InvalidOperationException("Current user should logout first"); } // Login <username> <password> string username = input[0]; string password = input[1]; using (BankingSystemContext context = new BankingSystemContext()) { User user = context.Users.FirstOrDefault(u => u.Username == username && u.Password == password); if (user == null) { throw new ArgumentException("Invalid username or password"); } AuthenticationManager.Login(user); } return($"User {username} logged in successfully"); }
public DepositController(IAsyncRepository <Deposit> deposit, IAsyncRepository <BankAccount> bankaccount, IMediator mediator, BankingSystemContext context) { _deposit = deposit; _context = context; Mediator = mediator; _bankaccount = bankaccount; }
public void SeedIBANMasterData(BankingSystemContext context) { if (context == null) { return; } DbInitializer.Initialize(context); }
public async Task Transfer_Success() { // ARRANGE var options = GetInMemoryOptions("Transfer_Success"); var transferIBAN = "NL92ABNA8946051078"; var receiveIBAN = "NL05INGB6289099205"; var remark = "Transfer 2000 money to Tony account."; decimal transferMoney = 2000; using (var context = new BankingSystemContext(options)) { SeedIBANMasterData(context); Seed_Transfer_Success(context, transferIBAN, receiveIBAN); // ACT var accountService = new AccountService(context); var actionService = new ActionService(context, accountService); var controller = new ActionController(context, accountService, actionService); // ACT var jsonResult = await controller.Transfer(new TransferReqeuest() { FromIBAN = transferIBAN, ToIBAN = receiveIBAN, Amount = transferMoney, Remark = remark }) as JsonResult; // ASSERT Assert.NotNull(jsonResult); Assert.Equal(200, jsonResult.StatusCode.GetValueOrDefault()); var value = jsonResult.Value as Response; Assert.NotNull(value.Result); // assert response object var transferResponse = value.Result as TransferResponse; Assert.NotNull(transferResponse); Assert.NotEqual(0, transferResponse.TransactionId); Assert.Equal(transferIBAN, transferResponse.TransferIBAN); Assert.Equal(receiveIBAN, transferResponse.ReceiveIBAN); Assert.Equal("Tony Stark", transferResponse.ReceiveCustomerFullName); Assert.Equal(transferMoney, transferResponse.Amount); Assert.Equal(0, transferResponse.Fee); // assert accounts in db var transferAccount = await context.Accounts.FirstAsync(a => a.IBAN == transferIBAN); Assert.Equal(1000, transferAccount.TotalAmount); var receiveAccount = await context.Accounts.FirstAsync(a => a.IBAN == receiveIBAN); Assert.Equal(3000, receiveAccount.TotalAmount); // assert transaction in db var transaction = await context.Transactions.FirstAsync(t => t.Id == transferResponse.TransactionId); Assert.Equal(Entity.Enums.TransactionType.TRANSFER, transaction.Type); Assert.Equal(transferIBAN, transaction.TransferIBAN); Assert.Equal(receiveIBAN, transaction.ReceiveIBAN); Assert.Equal(transferMoney, transaction.Amount); Assert.Equal(0, transaction.Fee); Assert.Equal(remark, transaction.Remark); } }
private void Seed_Deposit_Success(BankingSystemContext context, string iban) { // add account Account account = new Account() { IBAN = iban, CustomerId = 1, IsActive = true, CreatedOn = DateTime.Now, TotalAmount = 500 }; context.Accounts.Add(account); context.SaveChanges(); }
private string WithdrawMoney(string[] input) { if (input.Length != 2) { throw new ArgumentException("Invlaid input"); } if (!AuthenticationManager.IsAuthenticated()) { throw new InvalidOperationException("User should log in first"); } // withdraw <Account number> <money> string accountNumber = input[0]; decimal amount = decimal.Parse(input[1]); if (amount <= 0) { throw new ArgumentException("Amount should be positive"); } decimal currentBalance; using (BankingSystemContext context = new BankingSystemContext()) { User user = context.Users.Attach(AuthenticationManager.GetCurrentUser()); SavingAccount savingAccount = user.SavingAccounts.FirstOrDefault(a => a.AccountNumber == accountNumber); CheckingAccount checkingAccount = user.CheckingAccounts.FirstOrDefault(a => a.AccountNumber == accountNumber); if (savingAccount == null && checkingAccount == null) { throw new ArgumentException($"Account {accountNumber} does not exist"); } if (savingAccount != null) { savingAccount.WithdrawMoney(amount); context.SaveChanges(); currentBalance = savingAccount.Balance; } else { checkingAccount.WithdrawMoney(amount); context.SaveChanges(); currentBalance = checkingAccount.Balance; } } return($"Account {accountNumber} current balance = {currentBalance:f2}"); }
public async Task Deposit_Success() { // ARRANGE var options = GetInMemoryOptions("Deposit_Success"); var iban = "NL92ABNA8946051078"; var remark = "Put 1000 money into account."; decimal depositMoney = 1000; using (var context = new BankingSystemContext(options)) { SeedIBANMasterData(context); Seed_Deposit_Success(context, iban); // ACT var accountService = new AccountService(context); var actionService = new ActionService(context, accountService); var controller = new ActionController(context, accountService, actionService); // ACT var jsonResult = await controller.Deposit(new DepositRequest() { IBAN = iban, DepositMoney = depositMoney, Remark = remark }) as JsonResult; // ASSERT Assert.NotNull(jsonResult); Assert.Equal(200, jsonResult.StatusCode.GetValueOrDefault()); var value = jsonResult.Value as Response; Assert.NotNull(value.Result); // assert response object var depositResponse = value.Result as DepositResponse; Assert.NotNull(depositResponse); Assert.NotEqual(0, depositResponse.TransactionId); Assert.Equal(depositMoney, depositResponse.Deposit); Assert.Equal(1, depositResponse.Fee); Assert.Equal(1499, depositResponse.TotalAmount); // assert account in db var account = await context.Accounts.FirstAsync(a => a.IBAN == iban); Assert.Equal(1499, account.TotalAmount); // assert transaction in db var transaction = await context.Transactions.FirstAsync(t => t.Id == depositResponse.TransactionId); Assert.Equal(Entity.Enums.TransactionType.DEPOSIT, transaction.Type); Assert.Equal(iban, transaction.ReceiveIBAN); Assert.Equal(depositMoney, transaction.Amount); Assert.Equal(1, transaction.Fee); Assert.Equal(remark, transaction.Remark); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, BankingSystemContext dbContext) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } // create db in case not exist, Also seed IBAN master data DbInitializer.Initialize(dbContext); app.UseHttpsRedirection(); app.UseMvc(); }
private string RegisterUser(string[] input) { if (input.Length != 3) { throw new ArgumentException("Invalid input"); } if (AuthenticationManager.IsAuthenticated()) { throw new InvalidOperationException("Current user should log out before another user could log in"); } // Register <username> <password> <email> string username = input[0]; string password = input[1]; string email = input[2]; User user = new User() { Username = username, Password = password, Email = email }; this.ValidateUser(user); using (BankingSystemContext context = new BankingSystemContext()) { if (context.Users.Any(u => u.Username == username)) { throw new ArgumentException("Username already taken"); } if (context.Users.Any(u => u.Email == email)) { throw new ArgumentException("Email already taken"); } context.Users.Add(user); context.SaveChanges(); } return($"User {username} registered successfully"); }
private void Seed_Transfer_Success(BankingSystemContext context, string transferIBAN, string receiveIBAN, bool skipTransfer = false, bool skipReceive = false) { // customers List <Customer> customers = new List <Customer>(); if (!skipTransfer) { customers.Add(new Customer() { Id = 1, FirstName = "John", LastName = "Wick", IsActive = true, CreatedOn = DateTime.Now }); } if (!skipReceive) { customers.Add(new Customer() { Id = 2, FirstName = "Tony", LastName = "Stark", IsActive = true, CreatedOn = DateTime.Now }); } context.Customers.AddRange(customers); context.SaveChanges(); // add accounts List <Account> accounts = new List <Account>(); if (!skipTransfer) { accounts.Add(new Account() { IBAN = transferIBAN, CustomerId = 1, IsActive = true, CreatedOn = DateTime.Now, TotalAmount = 3000 }); } if (!skipReceive) { accounts.Add(new Account() { IBAN = receiveIBAN, CustomerId = 2, IsActive = true, CreatedOn = DateTime.Now, TotalAmount = 1000 }); } context.Accounts.AddRange(accounts); context.SaveChanges(); }
public async Task Create_An_Account_Customer_Null_Error() { // ARRANGE var options = GetInMemoryOptions("Create_An_Account_Customer_Null_Error"); using (var context = new BankingSystemContext(options)) { SeedIBANMasterData(context); var accountService = new AccountService(context); var controller = new AccountController(context, accountService); // ACT var jsonResult = await controller.CreateAccount(null) as JsonResult; // ASSERT Assert.NotNull(jsonResult); Assert.Equal(400, jsonResult.StatusCode.GetValueOrDefault()); var value = jsonResult.Value as Response; Assert.Equal(Entity.Constant.CUSTOMER_IS_NULL, value.Error); } }
public async Task Create_An_Account_No_IBAN_Left_Error() { // ARRANGE var options = GetInMemoryOptions("Create_An_Account_No_IBAN_Left_Error"); var customer = new Customer() { FirstName = "John", LastName = "Wick" }; using (var context = new BankingSystemContext(options)) { var accountService = new AccountService(context); var controller = new AccountController(context, accountService); // ACT var jsonResult = await controller.CreateAccount(customer, 500) as JsonResult; // ASSERT Assert.NotNull(jsonResult); Assert.Equal(500, jsonResult.StatusCode.GetValueOrDefault()); var value = jsonResult.Value as Response; Assert.Equal(Entity.Constant.NO_IBAN_LEFT, value.Error); } }
public async Task Transfer_IBAN_Is_Null_Error() { // ARRANGE var options = GetInMemoryOptions("Transfer_IBAN_Is_Null_Error"); using (var context = new BankingSystemContext(options)) { // ACT var accountService = new AccountService(context); var actionService = new ActionService(context, accountService); var controller = new ActionController(context, accountService, actionService); // ACT var jsonResult = await controller.Transfer(new TransferReqeuest() { FromIBAN = null, ToIBAN = null, Amount = 20000 }) as JsonResult; // ASSERT Assert.NotNull(jsonResult); Assert.Equal(400, jsonResult.StatusCode.GetValueOrDefault()); var value = jsonResult.Value as Response; Assert.Equal(Entity.Constant.IBAN_IS_NULL, value.Error); } }
public async Task Deposit_Account_Not_Found_Error() { // ARRANGE var options = GetInMemoryOptions("Deposit_Account_Not_Found_Error"); using (var context = new BankingSystemContext(options)) { // ACT var accountService = new AccountService(context); var actionService = new ActionService(context, accountService); var controller = new ActionController(context, accountService, actionService); // ACT var jsonResult = await controller.Deposit(new DepositRequest() { IBAN = "NL92ABNA8946051078", DepositMoney = 1000 }) as JsonResult; // ASSERT Assert.NotNull(jsonResult); Assert.Equal(400, jsonResult.StatusCode.GetValueOrDefault()); var value = jsonResult.Value as Response; Assert.Equal(Entity.Constant.ACCOUNT_NOT_FOUND, value.Error); } }
public ActionController(BankingSystemContext context, IAccountService accountService, IActionService actionService) : base(context) { _accountService = accountService; _actionService = actionService; }
public BaseService(BankingSystemContext context) { _context = context; }
public ScopedСurrencyService(BankingSystemContext context, ILogger <ScopedСurrencyService> logger) { _context = context; _logger = logger; }
public AccountService(BankingSystemContext context) : base(context) { }
public BankAccountEfRepository(BankingSystemContext context) => _context = context;
public BaseController(BankingSystemContext context) { _context = context; }
private string AddAccount(string[] input) { if (input.Length != 3) { throw new ArgumentException("Invalid input"); } // Add SavingAccount <initial balance> <interest rate> if (!AuthenticationManager.IsAuthenticated()) { throw new InvalidOperationException("User should log in first"); } string accountType = input[0]; string accountNumber = AccountGenerator.GenerateAccountNumber(); decimal balance = decimal.Parse(input[1]); decimal rateOrFee = decimal.Parse(input[2]); if (accountType.Equals("CheckingAccount", StringComparison.OrdinalIgnoreCase)) { CheckingAccount checkingAccount = new CheckingAccount() { AccountNumber = accountNumber, Balance = balance, Fee = rateOrFee }; this.ValidateCheckingAccount(checkingAccount); using (BankingSystemContext context = new BankingSystemContext()) { User user = AuthenticationManager.GetCurrentUser(); context.Users.Attach(user); checkingAccount.User = user; context.CheckingAccounts.Add(checkingAccount); context.SaveChanges(); } } else if (accountType.Equals("SavingAccount", StringComparison.OrdinalIgnoreCase)) { SavingAccount savingAccount = new SavingAccount() { AccountNumber = accountNumber, Balance = balance, InterestRate = rateOrFee }; this.ValidateSavingAccount(savingAccount); using (BankingSystemContext context = new BankingSystemContext()) { User user = AuthenticationManager.GetCurrentUser(); context.Users.Attach(user); savingAccount.User = user; context.SavingAccounts.Add(savingAccount); context.SaveChanges(); } } else { throw new ArgumentException($"Invalid account type {accountType}"); } return($"Account number {accountNumber} successfully added"); }
private void Seed_Create_An_Account_Existing_Customer_Success(BankingSystemContext context, Customer customer) { // create customer context.Customers.Add(customer); context.SaveChanges(); }
public EfRepository(BankingSystemContext context, ApplicationDbContext usContext) { Context = context; _context = usContext; }
// initial IBAN master data for using in api public static void Initialize(BankingSystemContext context) { context.Database.EnsureCreated(); if (context.MasterIBANs.Any()) { return; // IBAN has been seeded } List <MasterIBAN> masterIBANs = new List <MasterIBAN>() { new MasterIBAN() { IBAN = "NL92ABNA8946051078", Used = false }, new MasterIBAN() { IBAN = "NL05INGB6289099205", Used = false }, new MasterIBAN() { IBAN = "NL35ABNA7806242643", Used = false }, new MasterIBAN() { IBAN = "NL82RABO7030136047", Used = false }, new MasterIBAN() { IBAN = "NL34ABNA6918258974", Used = false }, new MasterIBAN() { IBAN = "NL80INGB7434587830", Used = false }, new MasterIBAN() { IBAN = "NL07ABNA5350244469", Used = false }, new MasterIBAN() { IBAN = "NL66ABNA1272753638", Used = false }, new MasterIBAN() { IBAN = "NL50RABO8386803843", Used = false }, new MasterIBAN() { IBAN = "NL12RABO5111110259", Used = false }, new MasterIBAN() { IBAN = "NL47ABNA3627647424", Used = false }, new MasterIBAN() { IBAN = "NL30RABO8877477636", Used = false }, new MasterIBAN() { IBAN = "NL10INGB9763136946", Used = false }, new MasterIBAN() { IBAN = "NL64INGB8247360527", Used = false }, new MasterIBAN() { IBAN = "NL59ABNA2825057568", Used = false }, new MasterIBAN() { IBAN = "NL38ABNA4824538831", Used = false }, new MasterIBAN() { IBAN = "NL80INGB6371362585", Used = false }, new MasterIBAN() { IBAN = "NL27INGB2760150151", Used = false }, new MasterIBAN() { IBAN = "NL02ABNA5136679077", Used = false }, new MasterIBAN() { IBAN = "NL06INGB2764204256", Used = false }, }; context.MasterIBANs.AddRange(masterIBANs); context.SaveChanges(); }