Add pagination on query find_articles_versions_by_*
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
create or replace function find_articles_versions_by_id(in _id uuid, out resource json) language plpgsql as
|
||||
create or replace function find_articles_versions_by_id(
|
||||
_id uuid,
|
||||
"limit" int default 50,
|
||||
"offset" int default 0,
|
||||
out resource json,
|
||||
out total int
|
||||
) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_version_id uuid = (select version_id from article where id = _id);
|
||||
begin
|
||||
select find_articles_versions_by_version_id(_version_id)
|
||||
into resource;
|
||||
select a.resource, a.total
|
||||
into resource, total
|
||||
from find_articles_versions_by_version_id(_version_id, "limit", "offset") a;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_articles_versions_by_id(uuid, out json);
|
||||
-- drop function if exists find_articles_versions_by_id(uuid, int, int, out json);
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
create or replace function find_articles_versions_by_version_id(in _version_id uuid, out resource json) language plpgsql as
|
||||
create or replace function find_articles_versions_by_version_id(
|
||||
_version_id uuid,
|
||||
"limit" int default 50,
|
||||
"offset" int default 0,
|
||||
out resource json,
|
||||
out total int
|
||||
) language plpgsql as
|
||||
$$
|
||||
begin
|
||||
select json_agg(t order by t.version_number desc)
|
||||
into resource
|
||||
select
|
||||
json_agg(t order by t.version_number desc),
|
||||
(select count(a) from article a where a.version_id = _version_id)
|
||||
into resource, total
|
||||
from (
|
||||
select
|
||||
a.*,
|
||||
@@ -10,8 +18,9 @@ begin
|
||||
from article as a
|
||||
where a.version_id = _version_id
|
||||
order by a.version_number desc
|
||||
limit "limit" offset "offset"
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_articles_versions_by_version_id(uuid, out json);
|
||||
-- drop function if exists find_articles_versions_by_version_id(uuid, int, int, out json);
|
||||
|
||||
@@ -7,6 +7,7 @@ declare
|
||||
created_citizen json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01"}';
|
||||
created_article json := '{"version_id":"933b6a1b-50c9-42b6-989f-c02a57814ef9", "title": "Love the world", "anonymous": false, "content": "bla bal bla", "tags": ["love", "test"]}';
|
||||
selected_article json;
|
||||
selected_total int;
|
||||
begin
|
||||
-- insert user for context
|
||||
select insert_user(created_user) into created_user;
|
||||
@@ -29,14 +30,16 @@ begin
|
||||
assert (created_article->>'version_number')::int = 2, format('version_number must be equal to 2, %s instead', created_article->>'version_number');
|
||||
|
||||
-- get articles versions by version_id
|
||||
select find_articles_versions_by_version_id((created_article->>'version_id')::uuid) into selected_article;
|
||||
select resource, total into selected_article, selected_total from find_articles_versions_by_version_id((created_article->>'version_id')::uuid);
|
||||
assert selected_article#>>'{0,title}' = 'Love the world', format('title must be "Love the world", %s', selected_article#>>'{0,title}');
|
||||
assert (selected_article#>>'{0,version_number}')::int = 2, format('version_id must be 2, %s instead', selected_article#>>'{0,version_number}');
|
||||
assert selected_total = 2, format('the total must be 2, %s instead', selected_total);
|
||||
|
||||
-- get articles versions by id
|
||||
select find_articles_versions_by_id((created_article->>'id')::uuid) into selected_article;
|
||||
select resource, total into selected_article, selected_total from find_articles_versions_by_id((created_article->>'id')::uuid);
|
||||
assert selected_article#>>'{0,title}' = 'Love the world', format('title must be "Love the world", %s', selected_article#>>'{0,title}');
|
||||
assert (selected_article#>>'{0,version_number}')::int = 2, format('version_id must be 2, %s instead', selected_article#>>'{0,version_number}');
|
||||
assert selected_total = 2, format('the total must be 2, %s instead', selected_total);
|
||||
|
||||
-- get article by id and check the title
|
||||
select find_article_by_id((created_article->>'id')::uuid) into selected_article;
|
||||
|
||||
Reference in New Issue
Block a user