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"
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

@ -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

@ -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

Loading…
Cancel
Save