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);

View File

@@ -2,6 +2,10 @@
drop table if exists resource_view;
-- Extra resources
drop table if exists opinion_on_article;
drop table if exists opinion;
drop table if exists opinion_list;
drop table if exists follow_article;
drop table if exists follow_constitution;
drop table if exists follow_citizen;

View File

@@ -503,6 +503,32 @@ create table resource_view
ip cidr null
);
create table opinion_list
(
id uuid default uuid_generate_v4() not null primary key,
name text not null,
target text null,
created_at timestamptz default now() not null,
deleted_at timestamptz null
);
create table opinion
(
opinion uuid not null references opinion_list (id),
foreign key (created_by_id) references citizen (id),
primary key (id),
unique (created_by_id, target_id, opinion)
) inherits (extra);
create table opinion_on_article
(
target_reference regclass default 'article'::regclass not null,
foreign key (opinion) references opinion_list (id),
foreign key (target_id) references article (id),
foreign key (created_by_id) references citizen (id),
primary key (id),
unique (created_by_id, target_id, opinion)
) inherits (opinion);
--------------