public HttpResponseMessage PutDeposit(TransactionModel transactionModel, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var context = new BankContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); var entityAccount = context.Accounts.FirstOrDefault(a => a.User.Id == user.Id); entityAccount.Balance = entityAccount.Balance + transactionModel.Sum; var newTransaction = new Transaction() { Date = DateTime.Now, Sum = transactionModel.Sum, User = entityAccount.User, Account = entityAccount, Id = entityAccount.Id, TransactionType = transactionModel.TransactionType }; context.Transactions.Add(newTransaction); context.SaveChanges(); return this.Request.CreateResponse(HttpStatusCode.OK); } }
public HttpResponseMessage PutWithdraw(TransactionModel transactionModel, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var context = new BankContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); var entityAccount = context.Accounts.FirstOrDefault(a => a.User.Id == user.Id); if (entityAccount.Balance > transactionModel.Sum) { entityAccount.Balance = entityAccount.Balance - transactionModel.Sum; } else { throw new InvalidOperationException("There isn`t enough money!"); } var newTransaction = new Transaction() { Date = DateTime.Now, Sum = transactionModel.Sum, User = entityAccount.User, Account = entityAccount, Id = entityAccount.Id, TransactionType = transactionModel.TransactionType }; context.Transactions.Add(newTransaction); context.SaveChanges(); return this.Request.CreateResponse(HttpStatusCode.OK); } }
public HttpResponseMessage PostDepositMoney(TransactionInOut transaction, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { this.ValidateSessionKey(sessionKey); var context = new BankContext(); using (context) { User user = context.Users.Where(usr => usr.SessionKey == sessionKey).FirstOrDefault(); if (user == null) { throw new InvalidOperationException("Invalid sessionkey"); } var currentBalance = user.Balance + transaction.Amount; user.Balance = currentBalance; var newTransaction = new Transaction() { Type = transaction.Type, Amount = transaction.Amount }; user.Transactions.Add(newTransaction); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.Created, transaction); return response; } }); return responseMsg; }