feature #7: add function "find_comments_by_target"

This commit is contained in:
2019-08-26 10:39:51 +02:00
parent 0547cf5784
commit d6fda26fbd
3 changed files with 40 additions and 5 deletions

View File

@@ -7,7 +7,7 @@ create or replace function find_comments_by_citizen(
) language plpgsql as ) language plpgsql as
$$ $$
begin begin
select json_agg(t), (select count(id) from "comment") select json_agg(t), (select count(id) from "comment" where citizen_id = _citizen_id)
into resource, total into resource, total
from ( from (
select select

View File

@@ -0,0 +1,26 @@
create or replace function find_comments_by_target(
_target_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" com where com.target_id = _target_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
from "comment" as com
where com.target_id = _target_id
order by com.parents_ids nulls first, created_at desc,
com.created_at desc
limit "limit" offset "offset"
) as t;
end;
$$;
-- drop function if exists find_comments_by_target(uuid, int, int);

View File

@@ -30,6 +30,7 @@ declare
_comment_id_response2 uuid; _comment_id_response2 uuid;
_selected_comments json; _selected_comments json;
_selected_comments_total int; _selected_comments_total int;
_find_comments_by_target_result json;
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;
@@ -85,22 +86,30 @@ begin
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,
content => 'No is not exist'::text, content => 'God not exist'::text,
_parent_id => _comment_id::uuid parent_id => _comment_id::uuid
) into _comment_id_response; ) into _comment_id_response;
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,
content => 'No is not exist'::text, content => 'are you really sure ?'::text,
_parent_id => _comment_id_response::uuid parent_id => _comment_id_response::uuid
) into _comment_id_response2; ) into _comment_id_response2;
assert (select count(*) = 3 from "comment"), 'comment must be inserted'; assert (select count(*) = 3 from "comment"), 'comment must be inserted';
assert (select com.parents_ids @> ARRAY[_comment_id] from "comment" com where id = _comment_id_response), 'parents_ids not contain "' || _comment_id::text || '" ' || (select com.parents_ids::text[] from "comment" com where id = _comment_id_response); assert (select com.parents_ids @> ARRAY[_comment_id] from "comment" com where id = _comment_id_response), 'parents_ids not contain "' || _comment_id::text || '" ' || (select com.parents_ids::text[] from "comment" com where id = _comment_id_response);
assert (select com.parents_ids @> ARRAY[_comment_id_response] from "comment" com where id = _comment_id_response2), 'parents_ids not contain "' || _comment_id_response::text || '" ' || (select com.parents_ids::text[] from "comment" com where id = _comment_id_response2); assert (select com.parents_ids @> ARRAY[_comment_id_response] from "comment" com where id = _comment_id_response2), 'parents_ids not contain "' || _comment_id_response::text || '" ' || (select com.parents_ids::text[] from "comment" com where id = _comment_id_response2);
assert (select com.parents_ids @> ARRAY[_comment_id] from "comment" com where id = _comment_id_response2), 'parents_ids not contain "' || _comment_id::text || '" ' || (select com.parents_ids::text[] from "comment" com where id = _comment_id_response2); assert (select com.parents_ids @> ARRAY[_comment_id] from "comment" com where id = _comment_id_response2), 'parents_ids not contain "' || _comment_id::text || '" ' || (select com.parents_ids::text[] from "comment" com where id = _comment_id_response2);
select resource into _find_comments_by_target_result
from find_comments_by_target((created_article->>'id')::uuid);
assert json_array_length(_find_comments_by_target_result) = 3,
'the result should contain 3 comment, ' || json_array_length(_find_comments_by_target_result) || ' returned';
assert (_find_comments_by_target_result#>>'{0,content}') = 'edited content', 'the first content must contain "edited content", "' || (_find_comments_by_target_result#>>'{0,content}') || '" returned';
assert (_find_comments_by_target_result#>>'{1,content}') = 'God not exist', 'the second content must contain "God not exist", "' || (_find_comments_by_target_result#>>'{1,content}') || '" returned';
assert (_find_comments_by_target_result#>>'{2,content}') = 'are you really sure ?', 'the third content must contain "are you really sure ?", "' || (_find_comments_by_target_result#>>'{2,content}') || '" returned';
-- delete comment and context -- delete comment and context
delete from "comment"; delete from "comment";
delete from article; delete from article;