Refactoring of updateOpinions (route/repo/query)

Can nw be set multiple opinion on sigle query
fix OpinionVoter on CREATE
This commit is contained in:
2020-03-22 00:53:08 +01:00
parent 479793503c
commit 589b6f5245
10 changed files with 151 additions and 56 deletions

View File

@@ -1376,10 +1376,12 @@ components:
ArticleOpinionRequest:
type: object
properties:
opinion_choice:
type: string
format: uuid
example: 6e978eb5-3c48-0def-b093-e01f43983adb
ids:
type: array
items:
type: string
format: uuid
example: 6e978eb5-3c48-0def-b093-e01f43983adb
OpinionChoices:
description: Opinion Choice

View File

@@ -0,0 +1,11 @@
create or replace function find_opinion_choices_by_ids(_ids uuid[], out resource json)
language plpgsql as
$$
begin
select json_agg(ol) into resource
from opinion_choice ol
where (ol.deleted_at <= now()
or ol.deleted_at is null)
and ol.id = any(_ids);
end;
$$;

View File

@@ -0,0 +1,31 @@
create or replace function update_citizen_opinions_by_target_id(
_choices_ids uuid[],
_citizen_id uuid,
_target_id uuid,
_target_reference regclass,
out opinions json,
out ids_deleted uuid[]
) language plpgsql as
$$
begin
if _target_reference = 'article'::regclass then
insert into opinion_on_article (created_by_id, target_id, choice_id)
select _citizen_id, _target_id, _choice_id
from unnest(_choices_ids) _choice_id
on conflict (created_by_id, target_id, choice_id) do nothing;
with deleted as (
delete from opinion_on_article o
where o.created_by_id = _citizen_id
and o.target_id = _target_id
and (not array[o.choice_id]::uuid[] <@ _choices_ids or _choices_ids = '{}'::uuid[])
returning id
)
select array_agg(d.id) into ids_deleted from deleted d;
else
raise exception '% no implemented for opinion', _target_reference::text;
end if;
select find_citizen_opinions_by_target_id(_citizen_id, _target_id) into opinions;
end
$$;