diff --git a/src/main/resources/sql/functions/comment/find_comment_by_id.sql b/src/main/resources/sql/functions/comment/find_comment_by_id.sql index c1d48a2..40fe22d 100644 --- a/src/main/resources/sql/functions/comment/find_comment_by_id.sql +++ b/src/main/resources/sql/functions/comment/find_comment_by_id.sql @@ -9,9 +9,7 @@ begin from ( select com.*, --- TODO use generic object, not article --- json_build_object('id', com.target_id) as target, - find_article_by_id(com.target_id) as target, + 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 id = _id diff --git a/src/main/resources/sql/functions/comment/find_comments_by_citizen.sql b/src/main/resources/sql/functions/comment/find_comments_by_citizen.sql index 2c3ae04..3338b46 100644 --- a/src/main/resources/sql/functions/comment/find_comments_by_citizen.sql +++ b/src/main/resources/sql/functions/comment/find_comments_by_citizen.sql @@ -1,5 +1,6 @@ create or replace function find_comments_by_citizen( _created_by_id uuid, + _reference regclass default null, "limit" int default 50, "offset" int default 0, out resource json, @@ -7,17 +8,31 @@ create or replace function find_comments_by_citizen( ) language plpgsql as $$ begin - select json_agg(t), (select count(id) from "comment" where created_by_id = _created_by_id) + select + json_agg(t), + ( + select count(id) from "comment" + where + (_reference is null or _reference = target_reference) + and created_by_id = _created_by_id + ) into resource, total from ( select com.*, - json_build_object('id', com.target_id) as target, + find_reference_by_id(com.target_id, _reference) as target, find_citizen_by_id(com.created_by_id) as created_by + from "comment" as com - where created_by_id = _created_by_id - order by created_at desc, - com.created_at desc + + where + (_reference is null or _reference = target_reference) + and created_by_id = _created_by_id + + order by + created_at desc, + com.created_at desc + limit "limit" offset "offset" ) as t; end; diff --git a/src/main/resources/sql/functions/comment/find_comments_by_parent.sql b/src/main/resources/sql/functions/comment/find_comments_by_parent.sql index d2eac4f..4c1b25e 100644 --- a/src/main/resources/sql/functions/comment/find_comments_by_parent.sql +++ b/src/main/resources/sql/functions/comment/find_comments_by_parent.sql @@ -12,7 +12,7 @@ begin from ( select com.*, - json_build_object('id', com.target_id) as target, + 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.parents_ids @> array[_parent_id] diff --git a/src/main/resources/sql/functions/comment/find_comments_by_target.sql b/src/main/resources/sql/functions/comment/find_comments_by_target.sql index 453b50e..96aea91 100644 --- a/src/main/resources/sql/functions/comment/find_comments_by_target.sql +++ b/src/main/resources/sql/functions/comment/find_comments_by_target.sql @@ -12,7 +12,7 @@ begin from ( select com.*, - json_build_object('id', com.target_id) as target, + 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.target_id = _target_id diff --git a/src/main/resources/sql/functions/comment/find_comments_constitution_by_citizen.sql b/src/main/resources/sql/functions/comment/find_comments_constitution_by_citizen.sql deleted file mode 100644 index f883212..0000000 --- a/src/main/resources/sql/functions/comment/find_comments_constitution_by_citizen.sql +++ /dev/null @@ -1,27 +0,0 @@ -create or replace function find_comments_constitution_by_citizen( - _created_by_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 where target_reference = 'constitution'::regclass) - into resource, total - from ( - select - com.*, - find_constitution_by_id(com.target_id) as target, - find_citizen_by_id(com.created_by_id) as created_by - from comment as com - where created_by_id = _created_by_id - and target_reference = 'constitution'::regclass - order by created_at desc, - com.created_at desc - limit "limit" offset "offset" - ) as t; -end; -$$; - --- drop function if exists find_comments_constitution_by_citizen(uuid, int, int); diff --git a/src/main/resources/sql/functions/helpers/find_reference_by_id.sql b/src/main/resources/sql/functions/helpers/find_reference_by_id.sql new file mode 100644 index 0000000..ca42bb0 --- /dev/null +++ b/src/main/resources/sql/functions/helpers/find_reference_by_id.sql @@ -0,0 +1,21 @@ +create or replace function find_reference_by_id( + _id uuid, + _reference regclass default null, + out resource json +) language plpgsql as +$$ +begin + select + case _reference + when 'article'::regclass then + find_article_by_id(_id) + when 'constitution'::regclass then + find_article_by_id(_id) + else + json_build_object('id', _id) + end + into resource; +end; +$$; + +-- drop function if exists find_reference_by_id(uuid, regclass, out json);