feature #7: add function "find_comments_by_target"
This commit is contained in:
@@ -7,7 +7,7 @@ create or replace function find_comments_by_citizen(
|
||||
) language plpgsql as
|
||||
$$
|
||||
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
|
||||
from (
|
||||
select
|
||||
|
||||
@@ -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);
|
||||
@@ -30,6 +30,7 @@ declare
|
||||
_comment_id_response2 uuid;
|
||||
_selected_comments json;
|
||||
_selected_comments_total int;
|
||||
_find_comments_by_target_result json;
|
||||
begin
|
||||
-- insert user for context
|
||||
select insert_user(created_user) into created_user;
|
||||
@@ -85,22 +86,30 @@ begin
|
||||
reference => 'article'::regclass,
|
||||
target_id => (created_article->>'id')::uuid,
|
||||
citizen_id => _citizen_id,
|
||||
content => 'No is not exist'::text,
|
||||
_parent_id => _comment_id::uuid
|
||||
content => 'God not exist'::text,
|
||||
parent_id => _comment_id::uuid
|
||||
) into _comment_id_response;
|
||||
|
||||
select "comment"(
|
||||
reference => 'article'::regclass,
|
||||
target_id => (created_article->>'id')::uuid,
|
||||
citizen_id => _citizen_id,
|
||||
content => 'No is not exist'::text,
|
||||
_parent_id => _comment_id_response::uuid
|
||||
content => 'are you really sure ?'::text,
|
||||
parent_id => _comment_id_response::uuid
|
||||
) into _comment_id_response2;
|
||||
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_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);
|
||||
|
||||
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 from "comment";
|
||||
delete from article;
|
||||
|
||||
Reference in New Issue
Block a user