Database: Entity refactor & DB models fix

master
E. Almqvist 3 years ago
parent b76fccd3e3
commit 68b42d7ad9
  1. 7
      src/app.rb
  2. 10
      src/db_models.rb
  3. 6
      src/lib/database.rb

@ -19,7 +19,7 @@ require_relative "db_init.rb"
require_relative "db_models.rb" require_relative "db_models.rb"
enable :sessions enable :sessions
db = db_init db_init
# Routes # Routes
get "/style.css" do get "/style.css" do
@ -42,15 +42,12 @@ end
# API stuff # API stuff
post "/user" do post "/user" do
# create user
user = db.get_table :User
email = params[:email] email = params[:email]
name = params[:name] name = params[:name]
password = params[:password] password = params[:password]
password_confirm = params[:password_confirm] 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 Console::debug "STATUS: #{status}", ret
if !status then # if something went wrong then return to 0 if !status then # if something went wrong then return to 0
session[:error_msg] = ret session[:error_msg] = ret

@ -2,17 +2,17 @@
class User < Entity class User < Entity
# Find user by ID, returns multiple results if multiple IDs exist # Find user by ID, returns multiple results if multiple IDs exist
# (which wont happen since IDs are unique) # (which wont happen since IDs are unique)
def find_by_id(id) def self.find_by_id(id)
self.get("*", "id = ?", id) self.get("*", "id = ?", id)
end end
# Find user by email, same as above but for emails. # Find user by email, same as above but for emails.
# Also unique # Also unique
def find_by_email(email) def self.find_by_email(email)
self.get("*", "email = ?", email) self.get("*", "email = ?", email)
end end
private def validate_register_creds(email, name, password, password_confirm) def self.validate_register_creds(email, name, password, password_confirm)
# Field check # Field check
check_all_fields = email != "" && name != "" && password != "" && password_confirm != "" check_all_fields = email != "" && name != "" && password != "" && password_confirm != ""
@ -39,7 +39,7 @@ class User < Entity
# Register a new user # Register a new user
# Returns: success?, data # 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) check, errorstr = self.validate_register_creds(email, name, password, password_confirm)
if( check ) then if( check ) then
@ -59,7 +59,7 @@ class User < Entity
# Log in user # Log in user
# Returns: success?, auth token # Returns: success?, auth token
def login(email, password) def self.login(email, password)
user_query = self.find_by_email email # get the user info user_query = self.find_by_email email # get the user info
if user_query.length >= 1 then if user_query.length >= 1 then

@ -20,18 +20,18 @@ class Entity
end end
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 "= ?, " vars.join "= ?, "
end 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 ", "})" entstr = "(#{vars.join ", "})"
valstr = "(#{(["?"] * vars.length).join ", "})" valstr = "(#{(["?"] * vars.length).join ", "})"
return entstr, valstr return entstr, valstr
end end
private def self.apply_filter(query, filter) def self.apply_filter(query, filter)
if filter != "" then query += " WHERE #{filter}" end if filter != "" then query += " WHERE #{filter}" end
query query
end end

Loading…
Cancel
Save