public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new BankContext();
                    using (context)
                    {
                        this.ValidateUsername(model.Username);
                        //this.ValidateFullName(model.FullName);
                        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.Id);
                        context.SaveChanges();

                        var loggedModel = new LoggedUserModel()
                        {
                            FullName = user.Username,
                            SessionKey = user.SessionKey
                        };

                        var response =
                            this.Request.CreateResponse(HttpStatusCode.Created,
                                            loggedModel);
                        return response;
                    }
                });

            return responseMsg;
        }
        public HttpResponseMessage GetAccountsBySessionKey(string sessionKey)
        {
            var response = this.PerformOperationAndHandleExceptions(() =>
            {
                BankContext context = new BankContext();
                this.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.FullName
                                });

                var responseMsg = this.Request.CreateResponse(HttpStatusCode.OK, accounts);

                return responseMsg;
            });

            return response;
        }
        public HttpResponseMessage GetDetailedInformationAboutAnAccount(int id, string sessionKey)
        {
            var response = this.PerformOperationAndHandleExceptions(() =>
            {
                BankContext context = new BankContext();
                this.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()
                               {
                                   FullName = account.Owner.FullName,
                                   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 HttpResponseMessage PutLogoutUser(string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  if (sessionKey.Length != 50)
                  {
                      throw new ArgumentException("The sessionkey is of invalid length!");
                  }

                  var context = new BankContext();
                  using (context)
                  {
                      var user = (from u in context.Users
                                  where u.SessionKey == sessionKey
                                  select u).FirstOrDefault();

                      if (user == null)
                      {
                          throw new InvalidOperationException("Session key not found!");
                      }
                      user.SessionKey = null;
                      context.SaveChanges();

                      return new HttpResponseMessage(HttpStatusCode.OK);
                  }
              });

            return responseMsg;
        }
        public HttpResponseMessage DepositCash(int id, decimal depositSum, string sessionKey)
        {
            var response = this.PerformOperationAndHandleExceptions(() =>
            {
                BankContext context = new BankContext();
                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);

                return responseMsg;
            });

            return response;
        }