Refactor SQL function Comment

This commit is contained in:
2019-10-06 23:49:20 +02:00
parent 9cbba66a36
commit 6d4339f2a5
4 changed files with 46 additions and 34 deletions

View File

@@ -70,9 +70,7 @@ abstract class Comment <T: UuidEntity>(override var requester: Requester): Repos
.getFunction("comment")
.sendQuery(
"reference" to reference,
"target_id" to comment.target.id,
"created_by_id" to comment.createdBy?.id,
"content" to comment.content
"resource" to comment
)
}

View File

@@ -1,24 +1,23 @@
create or replace function comment(reference regclass, target_id uuid, created_by_id uuid, content text, parent_comment_id uuid default null, out id uuid)
create or replace function comment(reference regclass, resource json, out _id uuid)
language plpgsql as
$$
declare
_created_by_id alias for created_by_id;
_target_id alias for target_id;
_content alias for content;
_parent_comment_id alias for parent_comment_id;
_id alias for id;
_created_by_id uuid = resource#>>'{created_by,id}';
_target_id uuid = resource#>>'{target,id}';
_content text = resource#>>'{content}';
_parent_comment_id uuid = resource#>>'{parent,id}';
_new_id uuid = coalesce((resource->>'id')::uuid, uuid_generate_v4());
begin
if reference = 'article'::regclass then
insert into comment_on_article (created_by_id, target_id, content, parent_comment_id)
values (_created_by_id, _target_id, _content, _parent_comment_id)
returning comment_on_article.id into _id;
insert into comment_on_article (id, created_by_id, target_id, content, parent_comment_id)
values (_new_id, _created_by_id, _target_id, _content, _parent_comment_id);
elseif reference = 'constitution'::regclass then
insert into comment_on_constitution (created_by_id, target_id, content, parent_comment_id)
values (_created_by_id, _target_id, _content, _parent_comment_id)
returning comment_on_constitution.id into _id;
insert into comment_on_constitution (id, created_by_id, target_id, content, parent_comment_id)
values (_new_id, _created_by_id, _target_id, _content, _parent_comment_id);
else
raise exception 'comment with target as "%", is no implemented', reference::text;
end if;
_id = _new_id;
end;
$$;

View File

@@ -7,7 +7,7 @@ create or replace function find_comments_by_target(
) language plpgsql as
$$
begin
select json_agg(t), (select count(id) from "comment" c3 where c3.parent_id = _target_id)
select json_agg(t), (select count(id) from "comment" c3 where c3.target_id = _target_id)
into resource, total
from (
select
@@ -16,7 +16,7 @@ begin
find_reference_by_id(com.target_id, com.target_reference) as target,
find_citizen_by_id(com.created_by_id) as created_by
from "comment" as com
where com.parent_id = _target_id
where com.target_id = _target_id
order by created_at asc,
com.created_at desc
limit "limit" offset "offset"

View File

@@ -34,6 +34,7 @@ declare
_find_comments_by_target_result json;
_find_comments_by_parent_result json;
_find_comments_by_id_result json;
_comment json;
begin
-- insert user for context
select insert_user(created_user) into created_user;
@@ -49,17 +50,22 @@ begin
select upsert_article(created_article) into created_article;
_comment = json_build_object(
'id', 'a2962f49-74e6-4f20-9c13-36f3ccbc4ad7',
'target', jsonb_build_object('id', created_article->>'id'),
'created_by', jsonb_build_object('id', _citizen_id),
'content', 'Ho my god !'
);
select "comment"(
reference => 'article'::regclass,
target_id => (created_article->>'id')::uuid,
created_by_id => _citizen_id,
content => 'Ho my god !'::text
resource => _comment
) into _comment_id;
assert (select count(*) = 1 from "comment"), 'comment must be inserted, "' || (select count(*) from "comment") || '" exist';
assert (select com.content = 'Ho my god !' from "comment" com), 'the content of comment must be "Ho my god !" instead of "' || (select com.content from "comment" as com) || '"';
select find_comment_by_id(_comment_id) into _find_comments_by_id_result;
assert (_find_comments_by_id_result->>'content' = 'Ho my god !'), 'content of comment must be "Ho my god !"';
assert (_find_comments_by_id_result->>'content' = 'Ho my god !'), format('content of comment must be "Ho my god !" instead of %s', _find_comments_by_id_result->>'content');
perform edit_comment(
_id => _comment_id,
@@ -88,20 +94,29 @@ begin
assert (_selected_comments_total = 0), 'the number of comments for this citizen must be 0, "' || _selected_comments_total || '" returned';
_comment = json_build_object(
'id', '50962646-07b6-42a3-9798-d756b9b6e2ba',
'target', jsonb_build_object('id', created_article->>'id'),
'created_by', jsonb_build_object('id', _citizen_id),
'content', 'God not exist',
'parent', json_build_object('id', _comment_id)
);
select "comment"(
reference => 'article'::regclass,
target_id => (created_article->>'id')::uuid,
created_by_id => _citizen_id,
content => 'God not exist'::text,
parent_comment_id => _comment_id::uuid
resource => _comment
) into _comment_id_response;
_comment = json_build_object(
'id', 'ce82e683-23a8-4977-92fb-8d61a3ec995a',
'target', jsonb_build_object('id', created_article->>'id'),
'created_by', jsonb_build_object('id', _citizen_id),
'content', 'are you really sure ?',
'parent', json_build_object('id', _comment_id_response)
);
select "comment"(
reference => 'article'::regclass,
target_id => (created_article->>'id')::uuid,
created_by_id => _citizen_id,
content => 'are you really sure ?'::text,
parent_comment_id => _comment_id_response::uuid
resource => _comment
) into _comment_id_response2;
assert (select count(*) = 3 from "comment"), 'response 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);
@@ -116,12 +131,12 @@ begin
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';
raise notice '%', (_find_comments_by_target_result#>>'{0,id}');
select resource into _find_comments_by_parent_result
from find_comments_by_parent((_find_comments_by_target_result#>>'{0,id}')::uuid);
assert json_array_length(_find_comments_by_parent_result) = 2,
'the result should contain 2 comment, ' || json_array_length(_find_comments_by_parent_result) || ' returned';
assert (_find_comments_by_parent_result#>>'{0,content}') = 'God not exist', 'the second content must contain "God not exist", "' || (_find_comments_by_parent_result#>>'{0,content}') || '" returned';
assert (_find_comments_by_parent_result#>>'{1,content}') = 'are you really sure ?', 'the third content must contain "are you really sure ?", "' || (_find_comments_by_parent_result#>>'{1,content}') || '" returned';
from find_comments_by_parent((_find_comments_by_target_result#>>'{1,id}')::uuid);
assert json_array_length(_find_comments_by_parent_result) = 1,
'the result should contain 1 comment, ' || json_array_length(_find_comments_by_parent_result) || ' returned';
assert (_find_comments_by_parent_result#>>'{0,content}') = 'are you really sure ?', 'the third content must contain "are you really sure ?", "' || (_find_comments_by_parent_result#>>'{1,content}') || '" returned';
-- delete comment and context
delete from "comment";