diff options
-rw-r--r-- | data/stuff/test/auth.js | 8 | ||||
-rw-r--r-- | src/guff/api/test.cr | 35 | ||||
-rw-r--r-- | src/guff/models/user.cr | 32 |
3 files changed, 38 insertions, 37 deletions
diff --git a/data/stuff/test/auth.js b/data/stuff/test/auth.js index 408010b..6f10fbf 100644 --- a/data/stuff/test/auth.js +++ b/data/stuff/test/auth.js @@ -5,15 +5,15 @@ jQuery(function($) { "<a ", "href='#' ", "class='list-group-item %{css|h}' ", - "title='Set user to \"%{name|h}.\"' ", - "data-user_id='%{id|h}' ", + "title='Set user to \"%{user_name|h}.\"' ", + "data-user_id='%{user_id|h}' ", ">", "<i class='fa fa-fw fa-spinner fa-spin hidden loading'></i>", "<i class='fa fa-fw fa-user loading'></i>", " ", - "%{name|h}", + "%{user_name|h}", "</a>", ], @@ -51,7 +51,7 @@ jQuery(function($) { }).done(function(r) { list.html($.map(r.users, function(row) { return TEMPLATES.run('user', $.extend({}, row, { - css: row.active ? 'active' : '', + css: (r.user_id == row.user_id) ? 'active' : '', })); }).join('')); }); diff --git a/src/guff/api/test.cr b/src/guff/api/test.cr index 448cadf..decc5ec 100644 --- a/src/guff/api/test.cr +++ b/src/guff/api/test.cr @@ -15,45 +15,22 @@ module Guff::API::TestAPI [{foo: "bar"}, {foo: "asdf"}].to_json end - MOCK_USERS = [{ - "id": "0", - "name": "Guest", - "active": true, - }, { - "id": "1", - "name": "Admin", - "active": false, - }, { - "id": "2", - "name": "Test User 1", - "active": false, - }, { - "id": "3", - "name": "Test User 2", - "active": false, - }, { - "id": "4", - "name": "Test User<<<<>>>>>&&&&&&2", - "active": false, - }] - private def do_test_get_users( context : HTTP::Server::Context, args : Hash(String, String) ) - user_id = "0" + # get user_id + user_id = 0_i64 with_session(context) do |session| - user_id = session["user_id"]? || "0" + user_id = (session["user_id"]? || "0").to_i64 false end - # build result - users = MOCK_USERS.map { |row| - row.merge({ "active": (row["id"] == user_id) }) - } + # get users + users = @models.user.get_users # return result - { "users": users } + { "users": users, "user_id": user_id } end private def do_test_set_user( diff --git a/src/guff/models/user.cr b/src/guff/models/user.cr index a76fc61..bdbb0de 100644 --- a/src/guff/models/user.cr +++ b/src/guff/models/user.cr @@ -2,6 +2,23 @@ require "crypto/bcrypt" class Guff::UserModel < Guff::Model SQL = TemplateCache.new({ + get_users: " + SELECT a.user_id, + a.created_at, + a.is_active, + a.user_name, + b.role_name, + c.email + + FROM users a + JOIN roles b + ON (b.role_id = a.role_id) + JOIN user_logins c + ON (c.user_id = a.user_id) + + ORDER BY LOWER(a.user_name) + ", + add_user: " INSERT INTO users(user_name) VALUES (:user_name) ", @@ -39,10 +56,17 @@ class Guff::UserModel < Guff::Model end def get_users - r = [] of Hash(String, String) - - all(:get_users) do |row| - r << row + r = [] of Hash(String, Int64 | String | Bool) + + all(:get_users, nil, nil) do |row| + r << { + "user_id": row["user_id"] as Int64, + "created_at": row["created_at"] as String, + "is_active": ((row["is_active"] as Int64) == 1), + "user_name": row["user_name"] as String, + "role_name": row["role_name"] as String, + "email": row["email"] as String, + } end r |