Refactor SQL function Comment
This commit is contained in:
@@ -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
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
$$;
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user