update postgresjson
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
create or replace function comment(reference regclass, target_id uuid, citizen_id uuid, content text, parent_id uuid default null, out id uuid)
|
||||
create or replace function comment(reference regclass, target_id uuid, created_by_id uuid, content text, parent_id uuid default null, out id uuid)
|
||||
language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_citizen_id alias for citizen_id;
|
||||
_created_by_id alias for created_by_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)
|
||||
insert into comment_on_article (created_by_id, target_id, content, parent_id)
|
||||
values (_created_by_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)
|
||||
insert into comment_on_constitution (created_by_id, target_id, content, parent_id)
|
||||
values (_created_by_id, _target_id, _content, _parent_id)
|
||||
returning comment_on_constitution.id into _id;
|
||||
else
|
||||
raise exception 'comment with target as "%", is no implemented', reference::text;
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
create or replace function find_comment_by_id(
|
||||
_id uuid,
|
||||
out resource json
|
||||
) language plpgsql as
|
||||
$$
|
||||
begin
|
||||
select to_json(t)
|
||||
into resource
|
||||
from (
|
||||
select
|
||||
com.*,
|
||||
json_build_object('id', com.target_id) as target,
|
||||
find_citizen_by_id(com.created_by_id) as created_by
|
||||
from "comment" as com
|
||||
where id = _id
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_comment_by_id(uuid, out json);
|
||||
@@ -1,5 +1,5 @@
|
||||
create or replace function find_comments_article_by_citizen(
|
||||
_citizen_id uuid,
|
||||
_created_by_id uuid,
|
||||
"limit" int default 50,
|
||||
"offset" int default 0,
|
||||
out resource json,
|
||||
@@ -13,9 +13,9 @@ begin
|
||||
select
|
||||
com.*,
|
||||
find_article_by_id(com.target_id) as target,
|
||||
find_citizen_by_id(com.citizen_id) as citizen
|
||||
find_citizen_by_id(com.created_by_id) as created_by
|
||||
from comment as com
|
||||
where citizen_id = _citizen_id
|
||||
where created_by_id = _created_by_id
|
||||
and target_reference = 'article'::regclass
|
||||
order by created_at desc,
|
||||
com.created_at desc
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
create or replace function find_comments_by_citizen(
|
||||
_citizen_id uuid,
|
||||
_created_by_id uuid,
|
||||
"limit" int default 50,
|
||||
"offset" int default 0,
|
||||
out resource json,
|
||||
@@ -7,15 +7,15 @@ create or replace function find_comments_by_citizen(
|
||||
) language plpgsql as
|
||||
$$
|
||||
begin
|
||||
select json_agg(t), (select count(id) from "comment" where citizen_id = _citizen_id)
|
||||
select json_agg(t), (select count(id) from "comment" where created_by_id = _created_by_id)
|
||||
into resource, total
|
||||
from (
|
||||
select
|
||||
com.*,
|
||||
json_build_object('id', com.target_id) as target,
|
||||
find_citizen_by_id(com.citizen_id) as citizen
|
||||
find_citizen_by_id(com.created_by_id) as created_by
|
||||
from "comment" as com
|
||||
where citizen_id = _citizen_id
|
||||
where created_by_id = _created_by_id
|
||||
order by created_at desc,
|
||||
com.created_at desc
|
||||
limit "limit" offset "offset"
|
||||
|
||||
@@ -13,7 +13,7 @@ begin
|
||||
select
|
||||
com.*,
|
||||
json_build_object('id', com.target_id) as target,
|
||||
find_citizen_by_id(com.citizen_id) as citizen
|
||||
find_citizen_by_id(com.created_by_id) as created_by
|
||||
from "comment" as com
|
||||
where com.parents_ids @> array[_parent_id]
|
||||
order by com.parents_ids nulls first, created_at desc,
|
||||
|
||||
@@ -13,7 +13,7 @@ begin
|
||||
select
|
||||
com.*,
|
||||
json_build_object('id', com.target_id) as target,
|
||||
find_citizen_by_id(com.citizen_id) as citizen
|
||||
find_citizen_by_id(com.created_by_id) as created_by
|
||||
from "comment" as com
|
||||
where com.target_id = _target_id
|
||||
order by com.parents_ids nulls first, created_at desc,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
create or replace function find_comments_constitution_by_citizen(
|
||||
_citizen_id uuid,
|
||||
_created_by_id uuid,
|
||||
"limit" int default 50,
|
||||
"offset" int default 0,
|
||||
out resource json,
|
||||
@@ -13,9 +13,9 @@ begin
|
||||
select
|
||||
com.*,
|
||||
find_constitution_by_id(com.target_id) as target,
|
||||
find_citizen_by_id(com.citizen_id) as citizen
|
||||
find_citizen_by_id(com.created_by_id) as created_by
|
||||
from comment as com
|
||||
where citizen_id = _citizen_id
|
||||
where created_by_id = _created_by_id
|
||||
and target_reference = 'constitution'::regclass
|
||||
order by created_at desc,
|
||||
com.created_at desc
|
||||
|
||||
Reference in New Issue
Block a user