diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/guff/api/test.cr | 35 | ||||
-rw-r--r-- | src/guff/models/user.cr | 32 |
2 files changed, 34 insertions, 33 deletions
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 |