feature #4: create procedure for citizen

This commit is contained in:
2019-07-26 14:29:48 +02:00
parent 0a92316128
commit fc06965cda
8 changed files with 110 additions and 11 deletions

View File

@@ -0,0 +1,12 @@
create or replace procedure find_citizen_by_id(in id uuid, inout resource json) language plpgsql as
$$
declare
_id alias for id;
begin
select to_json(z) into resource
from citizen as z
where z.id = _id;
end;
$$;
-- drop procedure if exists find_citizen_by_id(uuid, inout json);

View File

@@ -0,0 +1,12 @@
create or replace procedure find_citizen_by_user_id(in id uuid, inout resource json) language plpgsql as
$$
declare
_id alias for id;
begin
select to_json(z) into resource
from citizen as z
where z.user_id = _id;
end;
$$;
-- drop procedure if exists find_citizen_by_user_id(uuid, inout json);

View File

@@ -0,0 +1,31 @@
create or replace procedure upsert_citizen(inout resource json)
language plpgsql as
$$
declare
new_id uuid;
begin
insert into citizen (id, name, birthday, user_id, vote_annonymous, follow_annonymous)
select
coalesce(id, uuid_generate_v4()),
name,
birthday,
(resource#>>'{user, id}')::uuid,
coalesce(vote_annonymous, true),
coalesce(follow_annonymous, true)
from json_populate_record(null::citizen, resource)
on conflict (id) do update set
name = excluded.name,
birthday = excluded.birthday,
user_id = excluded.user_id,
vote_annonymous = excluded.vote_annonymous,
follow_annonymous = excluded.follow_annonymous
returning id into new_id;
select to_json(z)
into resource
from citizen as z
where z.id = new_id;
end;
$$;
-- drop procedure if exists insert_user(inout json);

View File

@@ -5,7 +5,7 @@ declare
begin
select
case when count(u) = 1
then to_json(json_populate_record(null::user_lite, to_json(u))) -- TODO refactor this !
then to_jsonb(u) - 'password'
else null end
into resource
from "user" as u

View File

@@ -9,4 +9,4 @@ begin
end;
$$;
-- drop procedure if exists find_user_by_id(inout json);
-- drop procedure if exists find_user_by_id(uuid, inout json);