Database: refactor

master
E. Almqvist 3 years ago
parent 7978c640c9
commit 9d6376b6b2
  1. 18
      src/app.rb
  2. 28
      src/lib/database.rb
  3. 7
      src/views/stylesheets/style.sass
  4. 1
      src/views/user/login.slim
  5. 2
      src/views/user/register.slim

@ -10,10 +10,15 @@ require "sassc"
require "colorize"
require_relative "debug.rb"
require_relative "database.rb"
require_relative "db_models.rb"
require_relative "lib/database.rb"
require_relative "func.rb"
load_tables = [
"User",
"Role"
]
db = Database.new("main", load_tables)
enable :sessions
get "/style.css" do
@ -33,12 +38,13 @@ get "/register" do
end
# API stuff
post "/user" do
# create user
redirect "/login"
end
post "/user/login" do
# login user
redirect "/"
end
post "/user/new" do
# create user
redirect "/login"
end

@ -26,9 +26,20 @@ class Table
def insert(data, filter="")
@db.insert(@name, data, filter)
end
end
require_relative "db_models.rb"
def update(data, filter="")
@db.update(@name, data, filter)
end
# sets or updates a specific field in the table
def set(attr, data, filter="") # slower but more lazy
if @db.get(@name, attr, filter).length > 0 then
@db.update(@name, data, filter)
else
@db.insert(@name, data, filter)
end
end
end
class Database # Database class
attr_reader :name, :path
@ -39,6 +50,10 @@ class Database # Database class
@tables = []
# generate table objects
tables_names.each do |name|
tbl = Table.new(self, name, "sql/tables/#{name}.sql")
@tables << tbl
end
end
private def db
@ -85,13 +100,4 @@ class Database # Database class
entstr, valstr = gen_insert_query data.keys
self.query( "INSERT INTO #{table} #{entstr} VALUES #{valstr}", *data.values )
end
# sets or updates a specific field in the table
def set(table, attr, data, filter="") # slower but more lazy
if self.get(table, attr, filter).length > 0 then
self.update(table, data, filter)
else
self.insert(table, data, filter)
end
end
end

@ -1,11 +1,11 @@
@import "fonts"
$font_stack: "Space Mono", monospace
$bg_clr: #272a34
$bg_alt_clr: #232730
$fg_clr: #fff
$fg_alt_clr: #eee
$fg_error: #f00
$shadow_clr: #4b5263
@ -89,6 +89,11 @@ a.button
a.button:hover
opacity: .6
label.error_msg
color: $fg_error
font-size: 1.2rem
font-weight: bold
.form-container
display: flex
justify-content: center

@ -1,6 +1,7 @@
.form-container
form action="/user/login" method="post"
h2 Log in
label.error_msg = session[:error_msg]
input type="text" name="email" placeholder="Email"
input type="password" name="password" placeholder="Password"
input type="submit" value="Log in"

@ -1,5 +1,5 @@
.form-container
form action="/user/new" method="post"
form action="/user" method="post"
h2 Register Account
input type="text" name="email" placeholder="Email"
input type="text" name="name" placeholder="Name"

Loading…
Cancel
Save