public HttpResponseMessage PostRegisterUser([FromBody]UserModel userModel)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new StoreContext();
                    using (context)
                    {
                        this.ValidateUsername(userModel.Username);
                        this.ValidateAuthCode(userModel.Password);
                        var usernameToLower = userModel.Username.ToLower();
                        var user = context.Users.FirstOrDefault(
                            usr => usr.Username.ToLower() == usernameToLower);

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

                        user = new User()
                        {
                            Username = usernameToLower,
                            Password = userModel.Password,
                            Email = userModel.Email,
                            Role = userModel.Role,
                            FirstName = userModel.FirstName,
                            LastName = userModel.LastName,
                        };

                        context.Users.Add(user);
                        context.SaveChanges();

                        user.SessionKey = this.GenerateSessionKey(user.UserId);
                        context.SaveChanges();

                        var loggedModel = new LoggedUserModel()
                        {
                            DisplayName = user.FirstName + " " + user.LastName,
                            SessionKey = user.SessionKey,
                            Role = user.Role
                        };

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

                        return response;
                    }
                });

            return responseMsg;
        }
        public HttpResponseMessage PostLoginUser(UserModel userModel)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  var context = new StoreContext();
                  using (context)
                  {
                      this.ValidateUsername(userModel.Username);
                      this.ValidateAuthCode(userModel.Password);
                      var usernameToLower = userModel.Username.ToLower();
                      var user = context.Users.FirstOrDefault(
                          usr => usr.Username == usernameToLower
                          && usr.Password == userModel.Password);

                      if (user == null)
                      {
                          throw new InvalidOperationException("Invalid Username or password");
                      }
                      if (user.SessionKey == null)
                      {
                          user.SessionKey = this.GenerateSessionKey(user.UserId);
                          context.SaveChanges();
                      }

                      var loggedModel = new LoggedUserModel()
                      {
                          DisplayName = user.FirstName + " " + user.LastName,
                          SessionKey = user.SessionKey,
                          Role = user.Role
                      };

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

                      return response;
                  }
              });

            return responseMsg;
        }