feature #4: create procedure for user
This commit is contained in:
18
resources/functions/user/check_user.sql
Normal file
18
resources/functions/user/check_user.sql
Normal file
@@ -0,0 +1,18 @@
|
||||
create or replace function check_user(in username text, in plain_password text, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_username alias for username;
|
||||
begin
|
||||
select
|
||||
case when count(u) = 1
|
||||
then to_json(json_populate_record(null::user_lite, to_json(u))) -- TODO refactor this !
|
||||
else null end
|
||||
into resource
|
||||
from "user" as u
|
||||
where u.username = lower(_username)
|
||||
and u.password = crypt(plain_password, u.password)
|
||||
group by u;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists check_user(text, text, out json);
|
||||
12
resources/functions/user/find_user_by_id.sql
Normal file
12
resources/functions/user/find_user_by_id.sql
Normal file
@@ -0,0 +1,12 @@
|
||||
create or replace procedure find_user_by_id(in id uuid, inout resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_id alias for id;
|
||||
begin
|
||||
select to_json(u) into resource
|
||||
from "user" as u
|
||||
where u.id = _id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop procedure if exists find_user_by_id(inout json);
|
||||
12
resources/functions/user/find_user_by_username.sql
Normal file
12
resources/functions/user/find_user_by_username.sql
Normal file
@@ -0,0 +1,12 @@
|
||||
create or replace procedure find_user_by_username(in username text, inout resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_username alias for username;
|
||||
begin
|
||||
select to_json(u) into resource
|
||||
from "user" as u
|
||||
where u.username = _username;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop procedure if exists find_user_by_username(text, inout json);
|
||||
20
resources/functions/user/insert_user.sql
Normal file
20
resources/functions/user/insert_user.sql
Normal file
@@ -0,0 +1,20 @@
|
||||
create or replace procedure insert_user(inout resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
new_id uuid;
|
||||
begin
|
||||
insert into "user" (username, password, blocked_at)
|
||||
select
|
||||
username,
|
||||
crypt(resource->>'plain_password', gen_salt('bf', 8)),
|
||||
case when blocked_at is not null then now() else null end
|
||||
from json_populate_record(null::"user", resource)
|
||||
returning id into new_id;
|
||||
|
||||
select to_json(u) into resource
|
||||
from "user" as u
|
||||
where u.id = new_id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop procedure if exists insert_user(inout json);
|
||||
Reference in New Issue
Block a user