aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/guff/api/test.cr41
-rw-r--r--src/guff/handlers/api.cr2
-rw-r--r--src/guff/models/user.cr43
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)