feature #7: add query "find_comments_article_by_citizen" and "find_comments_constitution_by_citizen"
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
create or replace function find_comments_article_by_citizen(
|
||||||
|
_citizen_id uuid,
|
||||||
|
"limit" int default 50,
|
||||||
|
"offset" int default 0,
|
||||||
|
out resource json,
|
||||||
|
out total int
|
||||||
|
) language plpgsql as
|
||||||
|
$$
|
||||||
|
begin
|
||||||
|
select json_agg(t), (select count(id) from comment where target_reference = 'article'::regclass)
|
||||||
|
into resource, total
|
||||||
|
from (
|
||||||
|
select
|
||||||
|
com.*,
|
||||||
|
find_article_by_id(com.target_id) as target,
|
||||||
|
find_citizen_by_id(com.citizen_id) as citizen
|
||||||
|
from comment as com
|
||||||
|
where citizen_id = _citizen_id
|
||||||
|
and target_reference = 'article'::regclass
|
||||||
|
order by created_at desc,
|
||||||
|
com.created_at desc
|
||||||
|
limit "limit" offset "offset"
|
||||||
|
) as t;
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
-- drop function if exists find_comments_article_by_citizen(uuid, int, int);
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
create or replace function find_comments_constitution_by_citizen(
|
||||||
|
_citizen_id uuid,
|
||||||
|
"limit" int default 50,
|
||||||
|
"offset" int default 0,
|
||||||
|
out resource json,
|
||||||
|
out total int
|
||||||
|
) language plpgsql as
|
||||||
|
$$
|
||||||
|
begin
|
||||||
|
select json_agg(t), (select count(id) from comment where target_reference = 'constitution'::regclass)
|
||||||
|
into resource, total
|
||||||
|
from (
|
||||||
|
select
|
||||||
|
com.*,
|
||||||
|
find_constitution_by_id(com.target_id) as target,
|
||||||
|
find_citizen_by_id(com.citizen_id) as citizen
|
||||||
|
from comment as com
|
||||||
|
where citizen_id = _citizen_id
|
||||||
|
and target_reference = 'constitution'::regclass
|
||||||
|
order by created_at desc,
|
||||||
|
com.created_at desc
|
||||||
|
limit "limit" offset "offset"
|
||||||
|
) as t;
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
-- drop function if exists find_comments_constitution_by_citizen(uuid, int, int);
|
||||||
@@ -62,8 +62,21 @@ begin
|
|||||||
select resource, total
|
select resource, total
|
||||||
into _selected_comments, _selected_comments_total
|
into _selected_comments, _selected_comments_total
|
||||||
from find_comments_by_citizen(_citizen_id);
|
from find_comments_by_citizen(_citizen_id);
|
||||||
assert (_selected_comments_total = 1), 'the number of comments for this citizen must be 1';
|
assert (_selected_comments_total = 1), 'the number of comments for this citizen must be 1, "' || _selected_comments_total || '" returned';
|
||||||
assert (_selected_comments#>>'{0,content}' = 'edited content'), 'the content of first comment for this citizen must be "edited content", "' || (_selected_comments#>>'{0,content}') || '" returned';
|
assert (_selected_comments#>>'{0,content}' = 'edited content'),
|
||||||
|
'the content of first comment for this citizen must be "edited content", "' || (_selected_comments#>>'{0,content}') || '" returned';
|
||||||
|
|
||||||
|
select resource, total
|
||||||
|
into _selected_comments, _selected_comments_total
|
||||||
|
from find_comments_article_by_citizen(_citizen_id);
|
||||||
|
assert (_selected_comments_total = 1), 'the number of comments for this citizen must be 1, "' || _selected_comments_total || '" returned';
|
||||||
|
assert (_selected_comments#>>'{0,content}' = 'edited content'),
|
||||||
|
'the content of first comment for this citizen must be "edited content", "' || (_selected_comments#>>'{0,content}') || '" returned';
|
||||||
|
|
||||||
|
select resource, total
|
||||||
|
into _selected_comments, _selected_comments_total
|
||||||
|
from find_comments_constitution_by_citizen(_citizen_id);
|
||||||
|
assert (_selected_comments_total = 0), 'the number of comments for this citizen must be 0, "' || _selected_comments_total || '" returned';
|
||||||
|
|
||||||
-- delete comment and context
|
-- delete comment and context
|
||||||
delete from "comment";
|
delete from "comment";
|
||||||
|
|||||||
Reference in New Issue
Block a user