refactoring: move sql files
This commit is contained in:
18
resources/sql/functions/article/find_article_by_id.sql
Normal file
18
resources/sql/functions/article/find_article_by_id.sql
Normal file
@@ -0,0 +1,18 @@
|
||||
create or replace function find_article_by_id(in id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_id alias for id;
|
||||
begin
|
||||
select to_json(t)
|
||||
from (
|
||||
select
|
||||
a.*,
|
||||
find_citizen_by_id(a.created_by_id) as created_by
|
||||
into resource
|
||||
from article as a
|
||||
where a.id = _id
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_article_by_id(uuid, out json);
|
||||
@@ -0,0 +1,20 @@
|
||||
create or replace function find_last_article_by_version_id(in version_id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_version_id alias for version_id;
|
||||
begin
|
||||
select to_json(t)
|
||||
from (
|
||||
select
|
||||
a.*,
|
||||
find_citizen_by_id(a.created_by_id) as created_by
|
||||
into resource
|
||||
from article as a
|
||||
where a.version_id = _version_id
|
||||
order by a.version_number desc
|
||||
limit 1
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_last_article_by_version_id(uuid, inout json);
|
||||
32
resources/sql/functions/article/upsert_article.sql
Normal file
32
resources/sql/functions/article/upsert_article.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
create or replace procedure upsert_article(inout resource json)
|
||||
language plpgsql as
|
||||
$$
|
||||
declare
|
||||
new_id uuid;
|
||||
begin
|
||||
insert into article (version_id, created_by_id, title, annonymous, content, description, tags)
|
||||
select
|
||||
version_id,
|
||||
(resource#>>'{created_by, id}')::uuid,
|
||||
title,
|
||||
annonymous,
|
||||
content,
|
||||
description,
|
||||
tags
|
||||
from json_populate_record(null::article, resource)
|
||||
returning id into new_id;
|
||||
|
||||
if resource->>'relations' is not null then
|
||||
insert into article_relations (source_id, target_id, created_by_id)
|
||||
select
|
||||
(resource->>'id')::uuid,
|
||||
id,
|
||||
(resource#>>'{created_by, id}')::uuid
|
||||
from json_populate_recordset(null::article, resource->>'relations');
|
||||
end if;
|
||||
|
||||
select find_article_by_id(new_id) into resource;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop procedure if exists upsert_article(inout json);
|
||||
17
resources/sql/functions/citizen/find_citizen_by_id.sql
Normal file
17
resources/sql/functions/citizen/find_citizen_by_id.sql
Normal file
@@ -0,0 +1,17 @@
|
||||
create or replace function find_citizen_by_id(in id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_id alias for id;
|
||||
begin
|
||||
select to_json(t) into resource
|
||||
from (
|
||||
select
|
||||
z.*,
|
||||
find_user_by_id(z.user_id) as "user"
|
||||
from citizen as z
|
||||
where z.id = _id
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_citizen_by_id(uuid, inout json);
|
||||
17
resources/sql/functions/citizen/find_citizen_by_user_id.sql
Normal file
17
resources/sql/functions/citizen/find_citizen_by_user_id.sql
Normal file
@@ -0,0 +1,17 @@
|
||||
create or replace function find_citizen_by_user_id(in user_id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_user_id alias for user_id;
|
||||
begin
|
||||
select to_json(t) into resource
|
||||
from (
|
||||
select
|
||||
z.*,
|
||||
find_user_by_id(z.user_id) as "user"
|
||||
from citizen as z
|
||||
where z.user_id = _user_id
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_citizen_by_user_id(uuid, inout json);
|
||||
28
resources/sql/functions/citizen/upsert_citizen.sql
Normal file
28
resources/sql/functions/citizen/upsert_citizen.sql
Normal file
@@ -0,0 +1,28 @@
|
||||
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 find_citizen_by_id(new_id) into resource;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop procedure if exists insert_user(inout json);
|
||||
25
resources/sql/functions/comment/comment.sql
Normal file
25
resources/sql/functions/comment/comment.sql
Normal file
@@ -0,0 +1,25 @@
|
||||
create or replace function comment(reference regclass, target_id uuid, citizen_id uuid, content text, parent_id uuid default null, out id uuid)
|
||||
language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_citizen_id alias for citizen_id;
|
||||
_target_id alias for target_id;
|
||||
_content alias for content;
|
||||
_parent_id alias for parent_id;
|
||||
_id alias for id;
|
||||
begin
|
||||
if reference = 'article'::regclass then
|
||||
insert into comment_on_article (citizen_id, target_id, content, parent_id)
|
||||
values (_citizen_id, _target_id, _content, _parent_id)
|
||||
returning comment_on_article.id into _id;
|
||||
elseif reference = 'constitution'::regclass then
|
||||
insert into comment_on_constitution (citizen_id, target_id, content, parent_id)
|
||||
values (_citizen_id, _target_id, _content, _parent_id)
|
||||
returning comment_on_constitution.id into _id;
|
||||
else
|
||||
raise exception '% no implemented', reference::text;
|
||||
end if;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists comment(regclass, uuid, uuid, text, uuid);
|
||||
20
resources/sql/functions/comment/edit_comment.sql
Normal file
20
resources/sql/functions/comment/edit_comment.sql
Normal file
@@ -0,0 +1,20 @@
|
||||
create or replace function edit_comment(reference regclass, id uuid, content text) returns void
|
||||
language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_id alias for id;
|
||||
_content alias for content;
|
||||
begin
|
||||
if reference = 'article'::regclass then
|
||||
update comment_on_article c set
|
||||
content = _content
|
||||
where c.id = _id;
|
||||
elseif reference = 'constitution'::regclass then
|
||||
update comment_on_constitution c set
|
||||
content = _content
|
||||
where c.id = _id;
|
||||
end if;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists edit_comment(regclass, uuid, uuid, text, uuid);
|
||||
@@ -0,0 +1,35 @@
|
||||
create or replace function create_title_in_constitution(title json, constitution_id uuid default null, out resource json)
|
||||
language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_title alias for title;
|
||||
_constitution_id uuid = coalesce(constitution_id, (title#>>'{constitution_id}')::uuid);
|
||||
_author_id uuid = (title#>>'{created_by, id}')::uuid;
|
||||
new_id uuid;
|
||||
begin
|
||||
insert into title (created_by_id, name, rank, constitution_id)
|
||||
select
|
||||
_author_id,
|
||||
ti.name,
|
||||
row_number() OVER (),
|
||||
_constitution_id
|
||||
from json_populate_record(null::title, _title) ti
|
||||
returning id into new_id;
|
||||
|
||||
if (_title->'articles' is not null) then
|
||||
insert into article_in_title (created_by_id, rank, title_id, article_id, constitution_id)
|
||||
select
|
||||
_author_id,
|
||||
row_number() over (),
|
||||
new_id,
|
||||
id,
|
||||
coalesce ((_title->>'constitution_id')::uuid, _constitution_id)
|
||||
from json_populate_recordset(null::article, _title->'articles') ;
|
||||
end if;
|
||||
|
||||
select find_constitution_title_by_id(new_id)
|
||||
into resource;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists create_title_in_constitution(out json);
|
||||
@@ -0,0 +1,19 @@
|
||||
create or replace function find_constitution_by_id(in id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_id alias for id;
|
||||
begin
|
||||
select to_json(t)
|
||||
from (
|
||||
select
|
||||
c.*,
|
||||
find_citizen_by_id(c.created_by_id) as created_by,
|
||||
find_constitution_titles_by_id(c.id) as titles
|
||||
into resource
|
||||
from constitution as c
|
||||
where c.id = _id
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_constitution_by_id(uuid, out json);
|
||||
@@ -0,0 +1,24 @@
|
||||
create or replace function find_constitution_title_by_id(in id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_id alias for id;
|
||||
begin
|
||||
select to_json(t)
|
||||
from (
|
||||
select
|
||||
ti.id,
|
||||
ti.name,
|
||||
ti.rank,
|
||||
array_agg(a order by ait.rank) as articles
|
||||
into resource
|
||||
from title as ti
|
||||
left join article_in_title ait on ti.id = ait.title_id
|
||||
left join article a on ait.article_id = a.id
|
||||
where ti.id = _id
|
||||
group by ti.id
|
||||
order by ti.rank
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_constitution_title_by_id(uuid, out json);
|
||||
@@ -0,0 +1,20 @@
|
||||
create or replace function find_constitution_titles_by_id(in constitution_id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_constitution_id alias for constitution_id;
|
||||
begin
|
||||
select json_agg(t)
|
||||
from (
|
||||
select
|
||||
ti.id,
|
||||
ti.name,
|
||||
ti.rank
|
||||
into resource
|
||||
from title as ti
|
||||
where ti.constitution_id = _constitution_id
|
||||
order by ti.rank
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_constitution_titles_by_id(uuid, out json);
|
||||
33
resources/sql/functions/constitution/upsert_constitution.sql
Normal file
33
resources/sql/functions/constitution/upsert_constitution.sql
Normal file
@@ -0,0 +1,33 @@
|
||||
create or replace procedure upsert_constitution(inout resource json)
|
||||
language plpgsql as
|
||||
$$
|
||||
declare
|
||||
titles json;
|
||||
_title json;
|
||||
_citizen_id uuid = (resource#>>'{created_by, id}')::uuid;
|
||||
new_id uuid;
|
||||
begin
|
||||
insert into constitution (version_id, created_by_id, title, annonymous)
|
||||
select
|
||||
version_id,
|
||||
_citizen_id,
|
||||
title,
|
||||
annonymous
|
||||
from json_populate_record(null::constitution, resource)
|
||||
returning id into new_id;
|
||||
|
||||
titles := (resource->>'titles');
|
||||
|
||||
for _title in select json_array_elements(titles) loop
|
||||
if _title#>>'{created_by, id}' is null then
|
||||
_title := jsonb_set(_title::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
|
||||
end if;
|
||||
|
||||
perform create_title_in_constitution(_title, new_id);
|
||||
end loop;
|
||||
|
||||
select find_constitution_by_id(new_id) into resource;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop procedure if exists upsert_constitution(inout json);
|
||||
23
resources/sql/functions/follow/follow.sql
Normal file
23
resources/sql/functions/follow/follow.sql
Normal file
@@ -0,0 +1,23 @@
|
||||
create or replace function follow(reference regclass, _target_id uuid, _citizen_id uuid) returns void
|
||||
language plpgsql as
|
||||
$$
|
||||
begin
|
||||
if reference = 'article'::regclass then
|
||||
insert into follow_article (citizen_id, target_id)
|
||||
values (_citizen_id, _target_id)
|
||||
on conflict (citizen_id, target_id) do nothing;
|
||||
elseif reference = 'constitution'::regclass then
|
||||
insert into follow_constitution (citizen_id, target_id)
|
||||
values (_citizen_id, _target_id)
|
||||
on conflict (citizen_id, target_id) do nothing;
|
||||
elseif reference = 'citizen'::regclass then
|
||||
insert into follow_citizen (citizen_id, target_id)
|
||||
values (_citizen_id, _target_id)
|
||||
on conflict (citizen_id, target_id) do nothing;
|
||||
else
|
||||
raise exception '% no implemented', reference::text;
|
||||
end if;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists follow(regclass, uuid, uuid);
|
||||
16
resources/sql/functions/follow/unfollow.sql
Normal file
16
resources/sql/functions/follow/unfollow.sql
Normal file
@@ -0,0 +1,16 @@
|
||||
create or replace function unfollow(reference regclass, target_id uuid, citizen_id uuid) returns void
|
||||
language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_citizen_id alias for citizen_id;
|
||||
_target_id alias for target_id;
|
||||
begin
|
||||
delete
|
||||
from follow f
|
||||
where f.citizen_id = _citizen_id
|
||||
and f.target_id = _target_id
|
||||
and f.target_reference = reference;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists unfollow(regclass, uuid, uuid);
|
||||
18
resources/sql/functions/user/check_user.sql
Normal file
18
resources/sql/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_jsonb(u) - 'password'
|
||||
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/sql/functions/user/find_user_by_id.sql
Normal file
12
resources/sql/functions/user/find_user_by_id.sql
Normal file
@@ -0,0 +1,12 @@
|
||||
create or replace function find_user_by_id(in id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_id alias for id;
|
||||
begin
|
||||
select to_jsonb(u) - 'password' into resource
|
||||
from "user" as u
|
||||
where u.id = _id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_user_by_id(uuid, out json);
|
||||
12
resources/sql/functions/user/find_user_by_username.sql
Normal file
12
resources/sql/functions/user/find_user_by_username.sql
Normal file
@@ -0,0 +1,12 @@
|
||||
create or replace function find_user_by_username(in username text, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_username alias for username;
|
||||
begin
|
||||
select to_jsonb(u) - 'password' into resource
|
||||
from "user" as u
|
||||
where u.username = _username;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_user_by_username(text, out json);
|
||||
18
resources/sql/functions/user/insert_user.sql
Normal file
18
resources/sql/functions/user/insert_user.sql
Normal file
@@ -0,0 +1,18 @@
|
||||
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 find_user_by_id(new_id) into resource;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop procedure if exists insert_user(inout json);
|
||||
35
resources/sql/functions/vote/vote.sql
Normal file
35
resources/sql/functions/vote/vote.sql
Normal file
@@ -0,0 +1,35 @@
|
||||
create or replace function vote(reference regclass, _target_id uuid, _citizen_id uuid, _note int, _anonymous bool default true) returns void
|
||||
language plpgsql as
|
||||
$$
|
||||
begin
|
||||
if reference = 'article'::regclass then
|
||||
insert into vote_for_article (citizen_id, target_id, note, anonymous)
|
||||
values (_citizen_id, _target_id, _note, _anonymous)
|
||||
on conflict (citizen_id, target_id) do update set
|
||||
note = excluded.note,
|
||||
anonymous = excluded.anonymous;
|
||||
elseif reference = 'constitution'::regclass then
|
||||
insert into vote_for_constitution (citizen_id, target_id, note, anonymous)
|
||||
values (_citizen_id, _target_id, _note, _anonymous)
|
||||
on conflict (citizen_id, target_id) do update set
|
||||
note = excluded.note,
|
||||
anonymous = excluded.anonymous;
|
||||
elseif reference = 'comment_on_article'::regclass then
|
||||
insert into vote_for_comment_on_article (citizen_id, target_id, note, anonymous)
|
||||
values (_citizen_id, _target_id, _note, _anonymous)
|
||||
on conflict (citizen_id, target_id) do update set
|
||||
note = excluded.note,
|
||||
anonymous = excluded.anonymous;
|
||||
elseif reference = 'comment_on_constitution'::regclass then
|
||||
insert into vote_for_comment_on_constitution (citizen_id, target_id, note, anonymous)
|
||||
values (_citizen_id, _target_id, _note, _anonymous)
|
||||
on conflict (citizen_id, target_id) do update set
|
||||
note = excluded.note,
|
||||
anonymous = excluded.anonymous;
|
||||
else
|
||||
raise exception '% no implemented', reference::text;
|
||||
end if;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists vote(regclass,uuid,uuid,integer,boolean);
|
||||
Reference in New Issue
Block a user