diff --git a/src/app.rb b/src/app.rb index 79831ea..9b9c9be 100755 --- a/src/app.rb +++ b/src/app.rb @@ -19,7 +19,7 @@ require_relative "db_init.rb" require_relative "db_models.rb" enable :sessions -db = db_init +db_init # Routes get "/style.css" do @@ -42,15 +42,12 @@ end # API stuff post "/user" do - # create user - user = db.get_table :User - email = params[:email] name = params[:name] password = params[:password] password_confirm = params[:password_confirm] - status, ret = user.register(email, name, password, password_confirm) + status, ret = User.register(email, name, password, password_confirm) Console::debug "STATUS: #{status}", ret if !status then # if something went wrong then return to 0 session[:error_msg] = ret diff --git a/src/db_models.rb b/src/db_models.rb index cf57934..80f26e2 100644 --- a/src/db_models.rb +++ b/src/db_models.rb @@ -2,17 +2,17 @@ class User < Entity # Find user by ID, returns multiple results if multiple IDs exist # (which wont happen since IDs are unique) - def find_by_id(id) + def self.find_by_id(id) self.get("*", "id = ?", id) end # Find user by email, same as above but for emails. # Also unique - def find_by_email(email) + def self.find_by_email(email) self.get("*", "email = ?", email) end - private def validate_register_creds(email, name, password, password_confirm) + def self.validate_register_creds(email, name, password, password_confirm) # Field check check_all_fields = email != "" && name != "" && password != "" && password_confirm != "" @@ -39,7 +39,7 @@ class User < Entity # Register a new user # Returns: success?, data - def register(email, name, password, password_confirm) + def self.register(email, name, password, password_confirm) check, errorstr = self.validate_register_creds(email, name, password, password_confirm) if( check ) then @@ -59,7 +59,7 @@ class User < Entity # Log in user # Returns: success?, auth token - def login(email, password) + def self.login(email, password) user_query = self.find_by_email email # get the user info if user_query.length >= 1 then diff --git a/src/lib/database.rb b/src/lib/database.rb index 9e4cf48..432cb08 100644 --- a/src/lib/database.rb +++ b/src/lib/database.rb @@ -20,18 +20,18 @@ class Entity end end - private def self.gen_update_query(vars) # generates part of the update query string + def self.gen_update_query(vars) # generates part of the update query string vars.join "= ?, " end - private def self.gen_insert_query(vars) # generates part of the insert query string + def self.gen_insert_query(vars) # generates part of the insert query string entstr = "(#{vars.join ", "})" valstr = "(#{(["?"] * vars.length).join ", "})" return entstr, valstr end - private def self.apply_filter(query, filter) + def self.apply_filter(query, filter) if filter != "" then query += " WHERE #{filter}" end query end