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

@@ -1029,6 +1029,7 @@ components:
- $ref: '#/components/schemas/CreatedAt'
- $ref: '#/components/schemas/lastVersion'
- $ref: '#/components/schemas/Votable'
- $ref: '#/components/schemas/Opinionable'
ArticleRequest:
$ref: '#/components/schemas/ArticleBase'
@@ -1210,6 +1211,15 @@ components:
properties:
votes:
$ref: '#/components/schemas/VoteAggregation'
Opinionable:
type: object
properties:
opinions:
type: string
additionalProperties: true
example:
Opinion1: 1
Opinion2: 55

View File

@@ -0,0 +1,29 @@
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;
insert into opinion_list (id, name, target)
select uuid_in(md5('opinion_list'||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)
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)
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;
raise notice '% opinion inserted', (select count(*) from opinion_on_article);
raise notice 'opinions fixtures done';
end;
$$;

View File

@@ -8,7 +8,8 @@ begin
select
a.*,
find_citizen_by_id(a.created_by_id) as created_by,
count_vote(a.id) as votes
count_vote(a.id) as votes,
count_opinion(a.id) as opinions
into resource
from article as a
where a.id = _id

View File

@@ -23,6 +23,7 @@ begin
a.*,
find_citizen_by_id(a.created_by_id) as created_by,
count_vote(a.id) as votes,
count_opinion(a.id) as opinions,
zdb.score(a.ctid) _score
from article as a
where (
@@ -60,3 +61,4 @@ end;
$$;
-- drop function if exists find_articles(text, json, text, text, int, int);
-- select find_article_by_id('d91aa0cd-61d6-83cc-41bb-8d5656e130f7');

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