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 IQueryable<TransactionInOut> GetTransactions( [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { this.ValidateSessionKey(sessionKey); var context = new BankContext(); var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid username or password"); } var transactions = (from t in context.Transactions select new TransactionInOut() { Type = t.Type, Amount = t.Amount }); return transactions; }); return responseMsg; }
public IQueryable<TransactionModel> GetAll( [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.ExceptionHandling( () => { var context = new BankContext(); var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid username or password!"); } var transactionsEntities = context.Transactions .Where(t => t.User.Id == user.Id); var models = (from tranEntity in transactionsEntities select new TransactionModel() { TransactionType = tranEntity.TransactionType, Date = tranEntity.Date, Sum = tranEntity.Sum }); return models.OrderByDescending(t => t.Date); }); return responseMsg; }
public UserAccountModel GetUserAccounts( [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new BankContext(); var user = context.Users.FirstOrDefault( u => u.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid sessionKey"); } var model = new UserAccountModel() { Username = user.Username, Accounts = (from accountEntity in user.Accounts select new AccountModel() { Id = accountEntity.Id, Balance = accountEntity.Balance }) }; return model; }); return responseMsg; }
public HttpResponseMessage PostCreateAccount(AccountModel accountModel, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.ExceptionHandling( () => { var context = new BankContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid username or password!"); } var post = new Account() { AccountNumber = accountModel.AccountNumber, Balance = accountModel.Balance, User = user }; context.Accounts.Add(post); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.Created); return response; } }); return responseMsg; }
public HttpResponseMessage PostLoginUser(UserModel model) { return this.PerformOperationAndHandleExceptions(() => { var context = new BankContext(); using (context) { var usernameToLower = model.Username.ToLower(); var entity = context.Users.SingleOrDefault(u => u.Username == usernameToLower && u.AuthCode == model.AuthCode); if (entity == null) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid username or password"); throw new HttpResponseException(errResponse); } entity.SessionKey = entity.Id.ToString(); //this.GenerateSessionKey(entity.Id); context.SaveChanges(); var responseModel = new UserLoggedModel() { Username = entity.Username, SessionKey = entity.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Accepted, responseModel); return response; } }); }
public HttpResponseMessage PostRegisterUser(UserModel model) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new BankContext(); using (context) { this.ValidateUserName(model.UserName); this.ValidateAuthCode(model.AuthCode); var userNameToLower = model.UserName.ToLower(); var user = context.Users.FirstOrDefault( usr => usr.UserName == userNameToLower); if (user != null) { throw new InvalidOperationException("User exists!"); } user = new User() { UserName = userNameToLower, AuthCode = model.AuthCode }; context.Users.Add(user); context.SaveChanges(); user.SessionKey = this.GenerateSessionKey(user.UserId); context.SaveChanges(); var loggedModel = new UserLoggedModel() { UserName = user.UserName, SessionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } }); return responseMsg; }
public AccountHomeModel GetHome() { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new BankContext(); var accountEntities = context.Accounts; var model = new AccountHomeModel() { NumberAccounts = accountEntities.Count(), BalanceAccounts = accountEntities.Sum(a => a.Balance) }; return model; }); return responseMsg; }
public HttpResponseMessage Login(UserModel model) { var responseMsg = this.ExceptionHandling( () => { var context = new BankContext(); using (context) { this.ValidateUsername(model.Username); this.ValidateAuthCode(model.AuthCode); var usernameToLower = model.Username.ToLower(); var user = context.Users.FirstOrDefault( usr => usr.Username == usernameToLower && usr.AuthCode == model.AuthCode); if (user == null) { throw new InvalidOperationException("Invalid username or password!"); } if (user.SessionKey == null) { user.SessionKey = this.GenerateSessionKey(user.Id); context.SaveChanges(); } var loggedModel = new LoggedUserModel() { Username = user.Username, SessionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } }); return responseMsg; }
public AccountFullModel GetLogs(int id, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new BankContext(); var accountEntity = context.Accounts.FirstOrDefault(x => x.Id == id); if (accountEntity == null) { throw new InvalidOperationException("Invalid id"); } var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); //var user = context.Users.FirstOrDefault(u => u.Id == 2); if (user == null) { throw new InvalidOperationException("Invalid sessionKey"); } var model = new AccountFullModel() { Id = accountEntity.Id, Balance = accountEntity.Balance, Logs = (from logEntity in accountEntity.Logs select new LogFullModel() { Amount = logEntity.Amount + "", Date = logEntity.Date }).OrderByDescending(l => l.Date) }; return model; }); return responseMsg; }
public CreateModel(Bank.Data.BankContext context) { _context = context; }
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; }
public HttpResponseMessage PutLogoutUser( [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new BankContext(); using (context) { var user = context.Users.FirstOrDefault( u => u.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid sessionKey"); } user.SessionKey = null; context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; } }); return responseMsg; }
public HttpResponseMessage PostRegisterUser(UserModel model) { return this.PerformOperationAndHandleExceptions(() => { var usernameToLower = model.Username.ToLower(); var context = new BankContext(); using (context) { var entity = context.Users.FirstOrDefault(u => u.Username == usernameToLower); if (entity != null) { string responseMessage = "Username already taken"; HttpResponseMessage errResponse = this.Request.CreateErrorResponse(HttpStatusCode.Conflict, responseMessage); throw new HttpResponseException(errResponse); } var user = new User() { Username = model.Username.ToLower(), AuthCode = model.AuthCode }; context.Users.Add(user); context.SaveChanges(); return this.PostLoginUser(model); } }); }
public UserDetailsModel GetUser( [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { this.ValidateSessionKey(sessionKey); var context = new BankContext(); var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid username or password"); } var transactions = (from t in user.Transactions select new TransactionInOut() { Type = t.Type, Amount = t.Amount }); var userDetailed = new UserDetailsModel() { Balance = user.Balance, UserName = user.UserName, Transactions = transactions }; return userDetailed; }); return responseMsg; }
public HttpResponseMessage PutWithdraw(int id, LogModel model, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new BankContext(); using (context) { var account = context.Accounts.FirstOrDefault(x => x.Id == id); if (account == null) { throw new InvalidOperationException("Invalid id"); } var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); //var user = context.Users.FirstOrDefault(u => u.Id == 2); if (user == null) { throw new InvalidOperationException("Invalid sessionKey"); } decimal amount = decimal.Parse(model.Amount); if (amount <= 0 || amount > account.Balance) { throw new ArgumentNullException( "Cannot withdraw non-positive or bigger than current balance amount"); } account.Balance -= amount; var log = new Log() { Account = account, Amount = -amount, Date = DateTime.Now }; account.Logs.Add(log); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK, "NULL"); return response; } }); return responseMsg; }
public EditModel(Bank.Data.BankContext context) { _context = context; }
public IndexModel(Bank.Data.BankContext context) { _context = context; }
public CustomerRepository(BankContext context) { }
public DetailsModel(Bank.Data.BankContext context) { _context = context; }
public HttpResponseMessage Logout([ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.ExceptionHandling( () => { var context = new BankContext(); using (context) { var user = context.Users.FirstOrDefault(x => x.SessionKey == sessionKey); user.SessionKey = null; context.SaveChanges(); } var response = this.Request.CreateResponse(HttpStatusCode.NoContent); return response; }); return responseMsg; }
public DeleteModel(Bank.Data.BankContext context) { _context = context; }
public CityRepository(BankContext context) { }