public HttpResponseMessage GetTransactions([ ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMessage = this.PerformOperationAndHandleExceptions(() => { var context = new BankContext(); using (context) { this.ValidateSessionKey(context, sessionKey); var user = context.Users.SingleOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new ServerErrorException("Identification failure"); } var transactions = context.Transactions.Include("Owner").Include("Account"). Where(t => t.Owner.Id == user.Id).ToList(); var transactionModels = (from transaction in transactions select new TransactionModel() { AccountId = transaction.Account.Id, Amount = transaction.Amount, TransactionType = (transaction.TransactionType == TransactionType.Deposit) ? "deposit" : "withdrawal" }); return Request.CreateResponse(HttpStatusCode.OK, transactionModels); } }); return responseMessage; }
public HttpResponseMessage GetAccountsByUser( [ValueProvider(typeof(HeaderValueProviderFactory<string>))]string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { BankContext context = new BankContext(); var user = context.Users.SingleOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new ServerErrorException("Identification failure"); } var accounts = context.Accounts.Include("Owner").Where(a => a.Owner.Id == user.Id); var accountModels = (from account in accounts select new AccountModel() { Id = account.Id, Balance = account.Balance }); return Request.CreateResponse(HttpStatusCode.OK, accountModels); }); return responseMsg; }
public HttpResponseMessage UpdateAccount([FromBody] SumModel sum, int accountId, [ValueProvider(typeof(HeaderValueProviderFactory<string>))]string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { BankContext context = new BankContext(); var user = context.Users.SingleOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new ServerErrorException("Identification failure"); } var account = context.Accounts.Include("Owner").SingleOrDefault(a => a.Id == accountId); if (account == null) { throw new ServerErrorException("Account does not exist"); } if (account.Owner.Id != user.Id) { throw new ServerErrorException("Identification does not match account"); } if ((sum.Sum < 0) && (account.Balance + sum.Sum < 0)) { throw new ServerErrorException("Amount of money insufficient"); } account.Balance += sum.Sum; context.SaveChanges(); var transaction = new Transaction(); transaction.Amount = sum.Sum; transaction.Owner = user; transaction.Account = account; if (sum.Sum < 0) { transaction.TransactionType = TransactionType.Withdrawal; } else { transaction.TransactionType = TransactionType.Deposit; } context.Transactions.Add(transaction); context.SaveChanges(); return Request.CreateResponse(HttpStatusCode.OK); }); return responseMsg; }
public HttpResponseMessage GetById(int id, [ValueProvider(typeof(HeaderValueProviderFactory<string>))]string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { BankContext context = new BankContext(); var user = context.Users.SingleOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new ServerErrorException("Identification failure"); } var account = context.Accounts.Include("Owner").SingleOrDefault(a => a.Id == id); if (account == null) { throw new ServerErrorException("Account does not exist"); } if (account.Owner.Id != user.Id) { throw new ServerErrorException("Identification does not match account"); } var accountToReturn = new AccountModel() { Id= account.Id, Balance = account.Balance }; return Request.CreateResponse(HttpStatusCode.OK, accountToReturn); }); return responseMsg; }