public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    this.ValidateUsername(model.Username);
                    this.ValidateDisplayName(model.DisplayName);
                    this.ValidateAuthCode(model.AuthCode);
                    var usernameToLower = model.Username.ToLower();
                    var displayNameToLower = model.DisplayName.ToLower();
                    var user = this.Data.Users.All().FirstOrDefault(
                        usr => usr.Username == usernameToLower
                        || usr.DisplayName.ToLower() == displayNameToLower);

                    if (user != null)
                    {
                        throw new InvalidOperationException("Users exists");
                    }

                    user = new User()
                    {
                        Username = usernameToLower,
                        DisplayName = model.DisplayName,
                        AuthCode = model.AuthCode
                    };

                    this.Data.Users.Add(user);
                    this.AddRootUserItem(user);
                    this.Data.SaveChanges();

                    user.SessionKey = this.GenerateSessionKey(user.Id);
                    this.Data.SaveChanges();

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

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

                });

            return responseMsg;
        }
        public HttpResponseMessage PostLoginUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
              () =>
              {

                  this.ValidateUsername(model.Username);
                  this.ValidateAuthCode(model.AuthCode);
                  var usernameToLower = model.Username.ToLower();
                  var user = this.Data.Users.All().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);
                      this.Data.SaveChanges();
                  }

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

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

            return responseMsg;
        }