static void Main() { Database.SetInitializer(new MigrateDatabaseToLatestVersion <OnlineBankContext, Configuration>()); using (var context = new OnlineBankContext()) { context.Database.Initialize(true); } }
public HttpResponseMessage PutLogoutUser(string sessionKey) { var context = new OnlineBankContext(); var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); user.SessionKey = null; context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return(response); }
public HttpResponseMessage PostRegisterUser(UserModel model) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new OnlineBankContext(); using (context) { this.ValidateUsername(model.DisplayName); this.ValidateAuthCode(model.AuthCode); var displayNameToLower = model.DisplayName.ToLower(); var user = context.Users.FirstOrDefault( usr => usr.Username == displayNameToLower || usr.AuthCode == model.AuthCode); if (user != null) { throw new InvalidOperationException("User exists"); } user = new User() { Username = displayNameToLower, AuthCode = model.AuthCode }; context.Users.Add(user); context.SaveChanges(); user.SessionKey = this.GenerateSessionKey(user.Id); context.SaveChanges(); var loggedModel = new LoggedUserModel() { DisplayName = user.Username, SessionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return(response); } }); return(responseMsg); }
protected void ValidateSessionKey(string sessionKey, OnlineBankContext context) { if (sessionKey.Length != SessionKeyLength) { throw new ArgumentException("The session key is of invalid length!"); } var user = from u in context.Users where u.SessionKey == sessionKey select u; if (user == null) { throw new InvalidOperationException("Session key not found!"); } }
public HttpResponseMessage WithdrawCash(int id, decimal withdrawSum, string sessionKey) { var response = this.PerformOperationAndHandleExceptions(() => { OnlineBankContext context = new OnlineBankContext(); this.ValidateSessionKey(sessionKey, context); var acc = (from account in context.Accounts.Include("Owner") where account.Id == id && account.Owner.SessionKey == sessionKey select account).FirstOrDefault(); if (acc == null) { throw new ArgumentException("Account not found."); } if (acc.Balance - withdrawSum < 0) { throw new ArgumentException("Not enough cash."); } TransactionLog transactionLog = new TransactionLog() { Account = acc, LogDate = DateTime.Now, LogText = string.Format("{0} withdrawed {1} money in {2}", acc.Owner.FullName, withdrawSum, acc.Id) }; acc.Transactions.Add(transactionLog); context.TransactionLogs.Add(transactionLog); acc.Balance -= withdrawSum; context.SaveChanges(); var responseMsg = this.Request.CreateResponse(HttpStatusCode.OK, "OK"); return(responseMsg); }); return(response); }
public HttpResponseMessage GetAccountsBySessionKey(string sessionKey) { var response = this.PerformOperationAndHandleExceptions(() => { OnlineBankContext context = new OnlineBankContext(); ValidateSessionKey(sessionKey, context); var accounts = (from account in context.Accounts.Include("Owner") where account.Owner.SessionKey == sessionKey select new AccountModel() { Id = account.Id, Balance = account.Balance, OwnerName = account.Owner.Username }); var responseMsg = Request.CreateResponse(HttpStatusCode.OK, accounts); return(responseMsg); }); return(response); }
public HttpResponseMessage GetDetailedInformationAboutAnAccount(int id, string sessionKey) { var response = this.PerformOperationAndHandleExceptions(() => { OnlineBankContext context = new OnlineBankContext(); ValidateSessionKey(sessionKey, context); var acc = (from account in context.Accounts.Include("Owner") where account.Id == id && account.Owner.SessionKey == sessionKey select new FullAccountModel() { Id = account.Id, Balance = account.Balance, CreatedOn = account.CreatedOn, ExpireDate = account.ExpireDate, Owner = new LoggedUserModel() { DisplayName = account.Owner.Username, SessionKey = account.Owner.SessionKey }, Transactions = from transaction in account.Transactions select new TransactionLogModel() { AccountId = account.Id, LogDate = transaction.LogDate, LogText = transaction.LogText, UserFullName = account.Owner.FullName } }); var responseMsg = this.Request.CreateResponse(HttpStatusCode.OK, acc); return(responseMsg); }); return(response); }
public TransactionTypeRepository(OnlineBankContext context) { this.context = context; transactionTypeEntity = context.Set <TransactionType>(); }
public AccountTypeRepository(OnlineBankContext context) { this.context = context; accountTypeEntity = context.Set <AccountType>(); }
public BankBranchRepository(OnlineBankContext context) { this.context = context; bankBranchEntity = context.Set <BankBranch>(); }
public AccountTransactionRepository(OnlineBankContext context) { this.context = context; accountTransactionEntity = context.Set <AccountTransaction>(); }