Add opinion on find_article_by_id & find_articles request

create request for find_citizen_opinions_by_target_id
create fixture for opinions
This commit is contained in:
2020-02-07 18:56:01 +01:00
parent 2048c71881
commit f622dbeecd
14 changed files with 282 additions and 5 deletions

View File

@@ -3,7 +3,7 @@ create or replace function count_opinion(_target_id uuid, out resource json)
$$
declare
agg jsonb;
empty jsonb = '[]'::jsonb;
empty jsonb = '{}'::jsonb;
begin
select jsonb_object_agg(t.label, t.total)
into agg
@@ -18,10 +18,10 @@ begin
order by ol.name
) t;
resource = empty || coalesce(agg, empty);
resource = coalesce(agg, empty);
end;
$$;
-- drop function if exists count_opinion(uuid);
-- select * from count_opinion('ced1563f-ecf5-4f11-8518-8aeceff3c13a');
-- select * from count_opinion('d91aa0cd-61d6-83cc-41bb-8d5656e130f7');

View File

@@ -0,0 +1,28 @@
create or replace function find_citizen_opinions_by_target_id(
_citizen_id uuid,
_id uuid,
out resource json
) language plpgsql as
$$
begin
select
json_agg(t)
into resource
from (
select
o.*,
ol.name
from opinion as o
join opinion_list ol on o.opinion = ol.id
where target_id = _id
and created_by_id = _citizen_id
order by
ol.name
limit 100
) as t;
end;
$$;
-- drop function if exists find_citizen_votes_by_target_ids(uuid, uuid[], regclass);

View File

@@ -0,0 +1,18 @@
create or replace function find_citizen_opinions_by_target_ids(
_citizen_id uuid,
_ids uuid[],
out resource json
) language plpgsql as
$$
begin
select
jsonb_agg(find_citizen_opinions_by_target_id(_citizen_id, o)) into resource
from unnest(_ids) o
order by
_ids
limit 100;
end;
$$;
-- drop function if exists find_citizen_votes_by_target_ids(uuid, uuid[], regclass);