From 5aad900122d718119cf023e9d72249d3609477bc Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 9 Mar 2022 11:38:59 +0100 Subject: [PATCH] User: Settings page & style refactor --- src/app.rb | 12 +++++++++++- src/db_models.rb | 5 ++--- src/lib/database.rb | 5 +++-- src/views/stylesheets/style.sass | 8 +++++++- src/views/user/settings.slim | 9 ++++++++- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/app.rb b/src/app.rb index 68d68aa..476f473 100755 --- a/src/app.rb +++ b/src/app.rb @@ -119,8 +119,18 @@ post "/login" do end end -post "/logout" do +get "/logout" do session.clear redirect "/" end + +post "/user/update" do + data = { + bio_text: params["bio"], + avatar_url: params["avatar_url"] + } + User.update(data, "id = ?", session[:userid]) + redirect "/settings" +end + diff --git a/src/db_models.rb b/src/db_models.rb index 8d246b2..6dbe882 100644 --- a/src/db_models.rb +++ b/src/db_models.rb @@ -46,7 +46,7 @@ class User < EntityModel return "#{sign}#{@reputation}" end - def reputation= val + def reputation=(val) val = val.clamp MIN_REP, MAX_REP @reputation = val self.update({reputation: val}, "id = ?", @id) @@ -113,7 +113,6 @@ class User < EntityModel end end - # Log in user # Returns: success?, user id def self.login(email, password) @@ -158,7 +157,7 @@ class Role < EntityModel end def self.edit(roleid, data) - self.update data, "id = ?", roleid + self.update data, "id = #{roleid}" end end diff --git a/src/lib/database.rb b/src/lib/database.rb index 3e7a5bc..455effd 100644 --- a/src/lib/database.rb +++ b/src/lib/database.rb @@ -25,7 +25,8 @@ class EntityModel end def self.gen_update_query(vars) # generates part of the update query string - vars.join "= ?, " + out = vars.join " = ?, " + out += " = ?" end def self.gen_insert_query(vars) # generates part of the insert query string @@ -41,6 +42,7 @@ class EntityModel end def self.query(q, *args) # query table with query string + Console.debug("Running SQL -> #{q}", *args) db.execute( q, *args ) end @@ -54,7 +56,6 @@ class EntityModel def self.update(data, filter="", *args) # Updates the table with specified data hash q = "UPDATE #{self.name} SET #{self.gen_update_query(data.keys)}" q = apply_filter(q, filter) - self.query(q, *data.values, *args) end diff --git a/src/views/stylesheets/style.sass b/src/views/stylesheets/style.sass index df70ded..0ee6b80 100644 --- a/src/views/stylesheets/style.sass +++ b/src/views/stylesheets/style.sass @@ -180,6 +180,10 @@ ul.button-container max-width: 400px border-radius: .4rem + label + color: $fg_alt_clr + opacity: .8 + a color: $special_clr padding: .2rem 0 @@ -287,7 +291,7 @@ ul.button-container width: 70% max-width: 12rem height: auto - border: 2px solid $special_clr + border: 2px solid $gray_clr #roles margin-bottom: 1.75rem @@ -312,3 +316,5 @@ ul.button-container margin-bottom: 4rem padding-bottom: 2rem + +#settings diff --git a/src/views/user/settings.slim b/src/views/user/settings.slim index d21fb7c..42dc4ac 100644 --- a/src/views/user/settings.slim +++ b/src/views/user/settings.slim @@ -1 +1,8 @@ -h1 Settings +h1.tcenter = "#{session_user.name}'s Settings" +.form-container + form action="/user/update" method="post" + label Avatar URL + input type="text" name="avatar_url" placeholder="/avatars/default.png" value="#{session_user.avatar_url}" + label Biography + input type="text" name="bio" placeholder="User Biography" value="#{session_user.bio_text}" + input type="submit" value="Update"