create SQL function find_votes_by_citizen

This commit is contained in:
2019-10-01 11:30:07 +02:00
parent be03bc4df8
commit 47bdc349c5
3 changed files with 66 additions and 1 deletions

View File

@@ -37,4 +37,4 @@ begin
end;
$$;
-- drop function if exists find_comments_by_citizen(uuid, int, int);
-- drop function if exists find_comments_by_citizen(uuid, regclass, int, int);

View File

@@ -0,0 +1,40 @@
create or replace function find_votes_by_citizen(
_created_by_id uuid,
_reference regclass default null,
"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 vote
where
(_reference is null or _reference = target_reference)
and created_by_id = _created_by_id
)
into resource, total
from (
select
v.*,
find_reference_by_id(v.target_id, _reference) as target,
find_citizen_by_id(v.created_by_id) as created_by
from vote as v
where
(_reference is null or _reference = target_reference)
and created_by_id = _created_by_id
order by
v.created_at desc
limit "limit" offset "offset"
) as t;
end;
$$;
-- drop function if exists find_votes_by_citizen(uuid, regclass, int, int);

View File

@@ -2,7 +2,9 @@ do
$$
declare
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
created_user2 json := '{"username": "george2", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
_citizen_id uuid;
_citizen_id2 uuid;
created_citizen json := $json$
{
"name": {
@@ -12,6 +14,15 @@ declare
"birthday": "2001-01-01"
}
$json$;
created_citizen2 json := $json$
{
"name": {
"first_name": "George2",
"last_name": "MICHEL2"
},
"birthday": "2001-01-02"
}
$json$;
created_article json := $json$
{
"version_id": "933b6a1b-50c9-42b6-989f-c02a57814ef9",
@@ -26,14 +37,20 @@ declare
}
$json$;
votes jsonb;
votes_of_citizen json;
votes_total int;
begin
-- insert user for context
select insert_user(created_user) into created_user;
select insert_user(created_user2) into created_user2;
created_citizen := jsonb_set(created_citizen::jsonb, '{user}'::text[], jsonb_build_object('id', created_user->>'id'), true)::json;
created_citizen2 := jsonb_set(created_citizen2::jsonb, '{user}'::text[], jsonb_build_object('id', created_user2->>'id'), true)::json;
-- insert new citizen for context
select upsert_citizen(created_citizen) into created_citizen;
select upsert_citizen(created_citizen2) into created_citizen2;
_citizen_id := created_citizen->>'id';
_citizen_id2 := created_citizen2->>'id';
created_article := jsonb_set(created_article::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
assert created_article#>>'{created_by, id}' = _citizen_id::text, format('citizenId in article must be the same as citizen, %s != %s', created_article#>>'{created_by, id}', _citizen_id::text);
-- upsert article
@@ -72,6 +89,14 @@ begin
select count_vote('article', '933b6a1b-50c9-42b6-989f-c02a57814ef9') into votes;
assert ((votes->>'up')::int = 0), 'vote.up must be 0';
-- Test "find_votes_by_citizen"
select resource, total into votes_of_citizen, votes_total from find_votes_by_citizen(_citizen_id2);
assert (votes_total = 0), format('votes count for user %s must be 0, instead of %s', _citizen_id2, votes_total);
select resource, total into votes_of_citizen, votes_total from find_votes_by_citizen(_citizen_id);
assert (votes_total = 1), format('votes count for user %s must be 1, instead of %s', _citizen_id, votes_total);
assert ((votes_of_citizen#>>'{0,note}')::int = -1), format('the note must be -1, instead of %s', (votes_of_citizen#>>'{0,note}'));
-- delete vote and context
delete from vote;
delete from article;