feature #7: add query "find_comments_by_citizen"

This commit is contained in:
2019-08-26 00:41:30 +02:00
parent d911109cd2
commit f167cf02f9
2 changed files with 37 additions and 3 deletions

View File

@@ -0,0 +1,26 @@
create or replace function find_comments_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")
into resource, total
from (
select
com.*,
json_build_object('id', com.target_id) as target,
find_citizen_by_id(com.citizen_id) as citizen
from "comment" as com
where citizen_id = _citizen_id
order by created_at desc,
com.created_at desc
limit "limit" offset "offset"
) as t;
end;
$$;
-- drop function if exists find_comments_by_citizen(uuid, int, int);

View File

@@ -26,6 +26,8 @@ declare
} }
$json$; $json$;
_comment_id uuid; _comment_id uuid;
_selected_comments json;
_selected_comments_total int;
begin begin
-- insert user for context -- insert user for context
select insert_user(created_user) into created_user; select insert_user(created_user) into created_user;
@@ -41,7 +43,7 @@ begin
select upsert_article(created_article) into created_article; select upsert_article(created_article) into created_article;
select comment( select "comment"(
reference => 'article'::regclass, reference => 'article'::regclass,
target_id => (created_article->>'id')::uuid, target_id => (created_article->>'id')::uuid,
citizen_id => _citizen_id, citizen_id => _citizen_id,
@@ -52,10 +54,16 @@ begin
perform edit_comment( perform edit_comment(
reference => 'article'::regclass, reference => 'article'::regclass,
id => _comment_id, id => _comment_id,
content => 'edited'::text content => 'edited content'::text
); );
assert (select count(*) = 1 from "comment"), 'edit comment must not insert new comment'; assert (select count(*) = 1 from "comment"), 'edit comment must not insert new comment';
assert (select count(*) = 1 from "comment" where content = 'edited'), 'edit comment must not insert new comment'; assert (select count(*) = 1 from "comment" where content = 'edited content'), 'edit comment must not insert new comment';
select resource, total
into _selected_comments, _selected_comments_total
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#>>'{0,content}' = 'edited content'), 'the content of first comment for this citizen must be "edited content", "' || (_selected_comments#>>'{0,content}') || '" returned';
-- delete comment and context -- delete comment and context
delete from "comment"; delete from "comment";