diff --git a/src/app.rb b/src/app.rb index a352ff6..81f3504 100755 --- a/src/app.rb +++ b/src/app.rb @@ -20,11 +20,6 @@ require_relative "db_models.rb" enable :sessions db = db_init -def init_params(params={}) - g = Hash.new "" - g.merge(params) -end - # Routes get "/style.css" do @@ -32,26 +27,33 @@ get "/style.css" do end get "/" do - slim :index, locals: {params: init_params} + slim :index, locals: {data: init_data} end get "/login" do - slim :"user/login", locals: {params: init_params} + slim :"user/login", locals: {data: init_data} end get "/register" do - slim :"user/register", locals: {params: init_params} + slim :"user/register", locals: {data: init_data} 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] - redirect "/login" + status, data = user.register(email, name, password, password_confirm) + if !status then # if something went wrong then return to 0 + redirect "/register", locals: {data: init_data(data)} + else # if everything went right then continue + redirect "/login", locals: {data: init_data(data)} + end end post "/user/login" do diff --git a/src/db_models.rb b/src/db_models.rb index 8e35473..60937fb 100644 --- a/src/db_models.rb +++ b/src/db_models.rb @@ -6,21 +6,22 @@ class User < Table # Find user by ID, returns multiple results if multiple IDs exist # (which wont happen since IDs are unique) def find_by_id(id) - self.get("*", "id = #{id}") + resp = self.get("*", "id = ?", id) end # Find user by email, same as above but for emails. # Also unique def find_by_email(email) - self.get("*", "email = #{email}") + resp = self.get("*", "email = ?", email) end # Register a new user # Returns: success?, data def register(email, name, password, password_confirm) - if( self.find_by_email(email).length > 0 ) then + check_email = self.find_by_email(email) + if( check_email.length > 0 ) then # Email taken - return false, "Email already in use!" + return false, {error_msg: "Email already in use!"} else if( password == password_confirm ) then pw_hash = BCrypt::Password.create(password) @@ -30,10 +31,10 @@ class User < Table pw_hash: pw_hash } - resp = self.insert(@name, data) # insert into the db - return true, resp + resp = self.insert(data) # insert into the db + return true, {resp: resp} else - return false, "Password mismatch!" + return false, {error_msg: "Password mismatch!"} end end end diff --git a/src/func.rb b/src/func.rb index eee09a3..3bd1c4a 100644 --- a/src/func.rb +++ b/src/func.rb @@ -2,3 +2,9 @@ def get_random_subtitle subtitles = File.readlines "misc/subtitles.txt" subtitles.sample.chomp end + +def init_data(data={}) + g = Hash.new "" + g.merge(data) +end + diff --git a/src/lib/database.rb b/src/lib/database.rb index ac0df10..d8bf015 100644 --- a/src/lib/database.rb +++ b/src/lib/database.rb @@ -21,12 +21,12 @@ class Table # are intended to be accessed through a # "Table Model". # See "db_models.rb" - private def get(attr, filter="") - @db.get(@name, attr, filter) + private def get(attr, filter="", *args) + @db.get(@name, attr, filter, *args) end - private def insert(data, filter="") - @db.insert(@name, data, filter) + private def insert(data) + @db.insert(@name, data) end private def update(data, filter="") @@ -84,11 +84,11 @@ class Database # Database class db.execute( q, *args ) end - def get(table, attr, filter="") # get data from table + def get(table, attr, filter="", *args) # get data from table q = "SELECT #{attr} FROM #{table}" # create the query string q = apply_filter(q, filter) - self.query query # execute query + self.query q, *args # execute query end def update(table, data, filter="") # Updates the table with specified data hash @@ -102,4 +102,8 @@ class Database # Database class entstr, valstr = gen_insert_query data.keys self.query( "INSERT INTO #{table} #{entstr} VALUES #{valstr}", *data.values ) end + + def get_table(tablesym) + @tables[tablesym] + end end