public HttpResponseMessage PostLoginUser(UserModel model) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new OnlineBankContext(); using (context) { this.ValidateUsername(model.DisplayName); this.ValidateAuthCode(model.AuthCode); var usernameToLower = model.DisplayName.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() { DisplayName = user.Username, SessionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } }); return responseMsg; }
public HttpResponseMessage DepositCash(int id, decimal depositSum, 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."); } TransactionLog transactionLog = new TransactionLog() { Account = acc, LogDate = DateTime.Now, LogText = string.Format("{0} deposited {1} money in {2}", acc.Owner.FullName, depositSum, acc.Id) }; acc.Transactions.Add(transactionLog); context.TransactionLogs.Add(transactionLog); acc.Balance += depositSum; context.SaveChanges(); var responseMsg = this.Request.CreateResponse(HttpStatusCode.OK, "OK"); return responseMsg; }); return response; }
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; }