Create route for Opinions

create OpinionRepository
create OpinionVoter
create OpinionChoiceRef
create extention String.toUUID() and List<String>.toUUID()
create OpinionAggregation
create interface RequestBuilderWithCreator for create entity by request
rename opinion_list to opinion_choice
create sql function find_citizen_opinions
fix sql function find_citizen_opinions_by_target_id
fix sql funciton find_opinion_choices
This commit is contained in:
2020-02-12 14:46:36 +01:00
parent ec6e39b130
commit 4a2d18ff87
24 changed files with 411 additions and 45 deletions

View File

@@ -2,22 +2,22 @@ do
$$
declare
article_count int = (select count(*) from article);
_citizensIds uuid[] = (select array_agg(id) from citizen);
begin
delete from opinion_on_article;
delete from opinion_list;
delete from opinion_choice;
insert into opinion_list (id, name, target)
select uuid_in(md5('opinion_list'||row_number() over ())::cstring), 'Opinion'||row_number() over (), '{article}'
insert into opinion_choice (id, name, target)
select uuid_in(md5('opinion_choice'||row_number() over ())::cstring), 'Opinion'||row_number() over (), '{article}'
from generate_series(0,20);
for i in 0..9 loop
insert into opinion_on_article (id, created_by_id, target_id, opinion)
insert into opinion_on_article (id, created_by_id, target_id, choice_id, created_at)
select
uuid_in(md5('opinion_on_article'||rn+(article_count*i))::cstring),
z.id,
a.id,
uuid_in(md5('opinion_list'||((rn+i) % 5 +1))::cstring)
uuid_in(md5('opinion_choice'||((rn+i) % 5 +1))::cstring),
now() + ((rn+i) || ' minute')::interval
from (select *, row_number() over ()+i+5 % 5 rn from citizen) z
join (select *, row_number() over () rn from article) a using (rn);
end loop;

View File

@@ -9,10 +9,10 @@ begin
into agg
from (
select
count(o.opinion) as total,
count(o) as total,
ol.name as label
from opinion o
join opinion_list ol on o.opinion = ol.id
join opinion_choice ol on o.choice_id = ol.id
where o.target_id = _target_id
group by ol.name
order by ol.name

View File

@@ -0,0 +1,46 @@
create or replace function find_citizen_opinions(
_citizen_id uuid,
direction text default 'desc',
sort text default 'created_at',
"limit" int default 50,
"offset" int default 0,
out resource json,
out total int
) language plpgsql as
$$
begin
select json_agg(t), (
select count(o.id)
from opinion o
where o.created_by_id = _citizen_id
)
into resource, total
from (
select
o.*,
to_json(ol) as choice
from opinion as o
join opinion_choice ol on o.choice_id = ol.id
where created_by_id = _citizen_id
order by
case direction when 'asc' then
case sort
when 'created_at' then o.created_at::text
else null
end
end,
case direction when 'desc' then
case sort
when 'created_at' then o.created_at::text
end
end
desc,
o.created_at desc
limit "limit" offset "offset"
) t;
end
$$;
-- select * from find_citizen_opinions('6434f4f9-f570-f22a-c134-8668350651ff', null, null, 2, 2);

View File

@@ -11,9 +11,9 @@ begin
from (
select
o.*,
ol.name
to_json(ol) as choice
from opinion as o
join opinion_list ol on o.opinion = ol.id
join opinion_choice ol on o.choice_id = ol.id
where target_id = _id
and created_by_id = _citizen_id

View File

@@ -9,9 +9,9 @@ begin
from (
select
o.*,
ol.name
to_json(ol) as choice
from opinion as o
join opinion_list ol on o.opinion = ol.id
join opinion_choice ol on o.choice_id = ol.id
where target_id = any(_ids)
and created_by_id = _citizen_id

View File

@@ -3,7 +3,7 @@ create or replace function find_opinion_choice_by_id(_id uuid, out resource json
$$
begin
select to_json(ol) into resource
from opinion_list ol
from opinion_choice ol
where (ol.deleted_at <= now()
or ol.deleted_at is null)
and (ol.id = _id);

View File

@@ -6,10 +6,9 @@ begin
into resource
from (
select ol.*
from opinion_list ol
where (ol.deleted_at <= now()
or ol.deleted_at is null)
and (ol.target is null or array_length(targets) = 0 or ol.target = any(targets))
from opinion_choice ol
where (ol.deleted_at is null or ol.deleted_at > now())
and (ol.target is null or targets is null or array_length(targets, 1) = 0 or ol.target && targets)
order by ol.name
) t;

View File

@@ -3,9 +3,9 @@ create or replace function opinion(reference regclass, _target_id uuid, _created
$$
begin
if reference = 'article'::regclass then
insert into opinion_on_article (created_by_id, target_id, opinion)
insert into opinion_on_article (created_by_id, target_id, choice_id)
values (_created_by_id, _target_id, _opinion)
on conflict (created_by_id, target_id, opinion) do nothing;
on conflict (created_by_id, target_id, choice_id) do nothing;
else
raise exception '% no implemented for opinion', reference::text;
end if;

View File

@@ -4,7 +4,7 @@ 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 opinion_choice;
drop table if exists follow_article;
drop table if exists follow_constitution;

View File

@@ -503,7 +503,7 @@ create table resource_view
ip cidr null
);
create table opinion_list
create table opinion_choice
(
id uuid default uuid_generate_v4() not null primary key,
name text not null unique,
@@ -514,20 +514,20 @@ create table opinion_list
create table opinion
(
opinion uuid not null references opinion_list (id),
choice_id uuid not null references opinion_choice (id),
foreign key (created_by_id) references citizen (id),
primary key (id),
unique (created_by_id, target_id, opinion)
unique (created_by_id, target_id, choice_id)
) inherits (extra);
create table opinion_on_article
(
target_reference regclass default 'article'::regclass not null,
foreign key (opinion) references opinion_list (id),
foreign key (choice_id) references opinion_choice (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)
unique (created_by_id, target_id, choice_id)
) inherits (opinion);