public async Task <bool> WithdrawAsync(decimal amount) { using (BankEntities context = new BankEntities()) { Account query = await GetAccountAsync(context); var balance = query.Balance; var newBalance = balance - amount; var balanceDiff = amount * -1; if (newBalance <= 0) { Console.WriteLine("You don't have enough funds."); return(false); } var transaction = new Transaction() { AccountNumber = _accountNum, TransactionType = "Withdrawal", BalanceDifference = balanceDiff, TransactionDate = DateTime.Now }; query.Balance = newBalance; context.Transactions.Add(transaction); return(await context.SaveChangesAsync() == 1); } }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, TraceWriter log) { log.Info("Received new account request"); HttpResponseMessage response; try { var user = await req.Content.ReadAsAsync <User>(); if (user != null) { log.Info("Saving to database"); using (var db = new BankEntities()) { db.Users.Add(user); await db.SaveChangesAsync(); } response = req.CreateResponse(HttpStatusCode.OK); } else { var errorMessage = "Failed to parse user"; log.Error(errorMessage); response = req.CreateErrorResponse(HttpStatusCode.BadRequest, errorMessage); } } catch (Exception e) { log.Error(e.Message, e); response = req.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } return(response); }
public async Task <ResponseModel> Transfer([FromBody] RequestModel requestModel) { //performs validation on request's data //get merchant account details from database var account = new Account(); using (BankEntities db = new BankEntities()) { account = await db.Accounts.FindAsync(requestModel.MerchantAccountID); } //check if account exists if (account == null) { return(new ResponseModel(ResponseStatuses.Error, "Invalid Merchant account ID")); } /* * contact issuing bank to request money transfer * check if response from issuing bank is as expected and return appropriate response to this caller */ //if everything is as expected: //update account balance using (BankEntities db = new BankEntities()) { account.Amount += requestModel.Amount; db.Entry(account).State = EntityState.Modified; await db.SaveChangesAsync(); } //return successful response return(new ResponseModel(ResponseStatuses.Successful, string.Empty)); }
public async Task <bool> TransferAsync(int target, decimal amount) { using (BankEntities context = new BankEntities()) { var origin = await context .Accounts .Where(e => e.AccountNumber == _accountNum) .SingleOrDefaultAsync(); var destination = await context .Accounts .Where(e => e.AccountNumber == target) .SingleOrDefaultAsync(); var originBalance = origin.Balance; var destinationBalance = destination.Balance; var newOriginBalance = originBalance - amount; var newDestinationBalance = destinationBalance + amount; var originDifference = amount * -1; var destinationDifference = amount; var originTransaction = new Transaction() { AccountNumber = _accountNum, TransactionType = "Transfer", BalanceDifference = originDifference, TransactionDate = DateTime.Now }; var destinationTransaction = new Transaction() { AccountNumber = target, TransactionType = "Transfer", BalanceDifference = destinationDifference, TransactionDate = DateTime.Now }; origin.Balance = newOriginBalance; destination.Balance = newDestinationBalance; context.Transactions.Add(originTransaction); context.Transactions.Add(destinationTransaction); return(await context.SaveChangesAsync() == 1); } }
public async Task <bool> ChangePinAsync(int newPin) { if (newPin < 1000 || newPin > 9999) { return(false); } using (BankEntities context = new BankEntities()) { var query = await context .Accounts .Where(e => e.AccountNumber == _accountNum) .SingleOrDefaultAsync(); query.Pin = newPin; return(await context.SaveChangesAsync() == 1); } }
public async Task <bool> DepositAsync(decimal amount) { using (BankEntities context = new BankEntities()) { Account query = GetAccountAsync(context).Result; var balance = query.Balance; var newBalance = balance + amount; var balanceDiff = amount; var transaction = new Transaction() { AccountNumber = _accountNum, TransactionType = "Deposit", BalanceDifference = balanceDiff, TransactionDate = DateTime.Now }; query.Balance += amount; context.Transactions.Add(transaction); return(await context.SaveChangesAsync() == 1); } }