diff options
author | Paul Duncan <pabs@pablotron.org> | 2016-04-01 00:02:30 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2016-04-01 00:02:30 -0400 |
commit | 094cbeedb81afde03d8f30f8928f98c13f1aa00a (patch) | |
tree | 31195843a8d38c172f8539df062a442b43f38cca /src/guff | |
parent | ec78a9b0fed0d54d6cc6695f80484e34286921ab (diff) | |
download | old-guff-094cbeedb81afde03d8f30f8928f98c13f1aa00a.tar.bz2 old-guff-094cbeedb81afde03d8f30f8928f98c13f1aa00a.zip |
add user methods
Diffstat (limited to 'src/guff')
-rw-r--r-- | src/guff/api/test.cr | 41 | ||||
-rw-r--r-- | src/guff/handlers/api.cr | 2 | ||||
-rw-r--r-- | src/guff/models/user.cr | 43 |
3 files changed, 75 insertions, 11 deletions
diff --git a/src/guff/api/test.cr b/src/guff/api/test.cr index 79d2d2e..71d614a 100644 --- a/src/guff/api/test.cr +++ b/src/guff/api/test.cr @@ -76,6 +76,47 @@ module Guff ) raise "some random error" end + + private def do_test_add_user( + context : HTTP::Server::Context, + args : Hash(String, String) + ) + user_id = @models.user.add_user(args["name"]) + { "ok": true, "user_id": user_id } + end + + private def do_test_update_user( + context : HTTP::Server::Context, + args : Hash(String, String) + ) + @models.user.update_user(args["user_id"].to_i, + name: args["name"]? ? args["name"] : nil, + active: args["active"]? ? args["active"] == "t" : nil, + role: args["role"]? ? args["role"] : nil + ) + + { "ok": true } + end + + private def do_test_add_login( + context : HTTP::Server::Context, + args : Hash(String, String) + ) + @models.user.add_login(args["user_id"].to_i, + email: args["name"], + password: args["password"], + ) + + { "ok": true } + end + + private def do_test_remove_login( + context : HTTP::Server::Context, + args : Hash(String, String) + ) + @models.user.remove_login(args["user_id"].to_i) + { "ok": true } + end end end end diff --git a/src/guff/handlers/api.cr b/src/guff/handlers/api.cr index 6f5c528..0f7f963 100644 --- a/src/guff/handlers/api.cr +++ b/src/guff/handlers/api.cr @@ -138,6 +138,8 @@ class Guff::Handlers::APIHandler < Guff::Handler error, get_users, set_user, + add_user, + update_user, ], }) rescue e diff --git a/src/guff/models/user.cr b/src/guff/models/user.cr index 4dc6d0a..1862e29 100644 --- a/src/guff/models/user.cr +++ b/src/guff/models/user.cr @@ -24,9 +24,19 @@ class Guff::UserModel < Guff::Model super(models, SQL) end - def add_user(user_name : String) + def get_users + r = [] of Hash(String, String) + + all(:get_users) do |row| + r << row + end + + r + end + + def add_user(name : String) query(:add_user, { - "user_name": user_name + "user_name": name }, nil) # return user id @@ -34,26 +44,37 @@ class Guff::UserModel < Guff::Model end def update_user( - user_id : Int32, - user_name : String? = nil, - active : Boolean? = nil, + user_id : Int32, + name : String? = nil, + active : Bool? = nil, + role : String? = nil, ) sets = [] of String args = { "user_id": user_id.to_s } - if user_name != nil - sets << "user_name = :user_name" - args["user_name"] = user_name + if name != nil + args["name"] = name.not_nil! + sets << "user_name = :name" end if active != nil + args["is_active"] = active.not_nil! ? "1" : "0" sets << "is_active = :is_active" - args["is_active"] = active ? "1" : "0" end + if role != nil + args["role_name"] = role.not_nil! + sets << " + role_id = (SELECT role_id + FROM roles + WHERE role_name = :role) + " + end + + # exec query query(:update_user, args, { - "sets": sets.join(","), - }) if sets.length > 0 + "sets": sets.join(", "), + }) if sets.size > 0 end def delete_login(user_id : Int32) |