From 094cbeedb81afde03d8f30f8928f98c13f1aa00a Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Fri, 1 Apr 2016 00:02:30 -0400 Subject: add user methods --- src/guff/models/user.cr | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'src/guff/models/user.cr') 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) -- cgit v1.2.3