Add opinion Tables and request

This commit is contained in:
2020-02-07 02:05:28 +01:00
parent a4dbd43cfb
commit de6ca63165
5 changed files with 163 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
create or replace function count_opinion(_target_id uuid, out resource json)
language plpgsql as
$$
declare
agg jsonb;
empty jsonb = '[]'::jsonb;
begin
select jsonb_object_agg(t.label, t.total)
into agg
from (
select
count(o.opinion) as total,
ol.name as label
from opinion o
join opinion_list ol on o.opinion = ol.id
where o.target_id = _target_id
group by ol.name
order by ol.name
) t;
resource = empty || coalesce(agg, empty);
end;
$$;
-- drop function if exists count_opinion(uuid);
-- select * from count_opinion('ced1563f-ecf5-4f11-8518-8aeceff3c13a');

View File

@@ -0,0 +1,17 @@
create or replace function opinion(reference regclass, _target_id uuid, _created_by_id uuid, _opinion uuid, out resource json)
language plpgsql as
$$
begin
if reference = 'article'::regclass then
insert into opinion_on_article (created_by_id, target_id, opinion)
values (_created_by_id, _target_id, _opinion)
on conflict (created_by_id, target_id, opinion) do nothing;
else
raise exception '% no implemented for opinion', reference::text;
end if;
select count_opinion(_target_id) into resource;
end;
$$;
-- drop function if exists vote(regclass,uuid,uuid,integer,boolean);