public HttpResponseMessage PostRegisterUser(UserPostModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    this.ValidateUsername(model.Username);
                    this.ValidateName(model.Name);
                    this.ValidatePassword(model.Password);
                    var usernameToLower = model.Username.ToLower();
                    User user = this.Data.Users.All().FirstOrDefault(
                        x => x.Username == usernameToLower);

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

                    user = new User()
                    {
                        Username = usernameToLower,
                        Name = model.Name,
                        Password = model.Password,
                        SessionKey = string.Empty,
                        Level = this.Data.Levels.All().OrderBy(x => x.Rank).FirstOrDefault(),
                        UserRole = this.Data.UserRoles.All().Where(x => x.Role == RoleUser).FirstOrDefault()
                    };

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

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

                    var loggedModel = new UserLoggedModel()
                    {
                        SessionKey = user.SessionKey
                    };

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

            return responseMsg;
        }
        public HttpResponseMessage PostLoginUser(UserPostModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
              () =>
              {
                    this.ValidateUsername(model.Username);
                    this.ValidatePassword(model.Password);
                    var usernameToLower = model.Username.ToLower();
                    var user = this.Data.Users.All().FirstOrDefault(
                        x => x.Username == usernameToLower
                        && x.Password == model.Password);

                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid username or password");
                    }

                    if (user.SessionKey == string.Empty)
                    {
                        user.SessionKey = user.Id.ToString();
                        this.Data.SaveChanges();
                    }

                    var loggedModel = new UserLoggedModel()
                    {
                        SessionKey = user.SessionKey
                    };

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

            return responseMsg;
        }