create SQL function for get citizen votes with multiple target ids

add updated_at field on vote table
This commit is contained in:
2019-10-03 15:43:39 +02:00
parent 51cc5b640d
commit c5a2f92b19
8 changed files with 134 additions and 31 deletions

View File

@@ -0,0 +1,32 @@
create or replace function find_citizen_votes_by_target_ids(
_citizen_id uuid,
_ids uuid[],
_reference regclass default null,
out resource json
) language plpgsql as
$$
begin
select
json_agg(t)
into resource
from (
select
v.*,
find_reference_by_id(v.target_id, _reference) as target,
find_citizen_by_id(v.created_by_id) as created_by
from vote as v
where
(_reference is null or _reference = target_reference)
and target_id = any(_ids)
and created_by_id = _citizen_id
order by
_ids
limit 100
) as t;
end;
$$;
-- drop function if exists find_citizen_votes_by_target_ids(uuid, uuid[], regclass);

View File

@@ -7,25 +7,29 @@ begin
values (_created_by_id, _target_id, _note, _anonymous)
on conflict (created_by_id, target_id) do update set
note = excluded.note,
anonymous = excluded.anonymous;
anonymous = excluded.anonymous,
updated_at = now();
elseif reference = 'constitution'::regclass then
insert into vote_for_constitution (created_by_id, target_id, note, anonymous)
values (_created_by_id, _target_id, _note, _anonymous)
on conflict (created_by_id, target_id) do update set
note = excluded.note,
anonymous = excluded.anonymous;
anonymous = excluded.anonymous,
updated_at = now();
elseif reference = 'comment_on_article'::regclass then
insert into vote_for_comment_on_article (created_by_id, target_id, note, anonymous)
values (_created_by_id, _target_id, _note, _anonymous)
on conflict (created_by_id, target_id) do update set
note = excluded.note,
anonymous = excluded.anonymous;
anonymous = excluded.anonymous,
updated_at = now();
elseif reference = 'comment_on_constitution'::regclass then
insert into vote_for_comment_on_constitution (created_by_id, target_id, note, anonymous)
values (_created_by_id, _target_id, _note, _anonymous)
on conflict (created_by_id, target_id) do update set
note = excluded.note,
anonymous = excluded.anonymous;
anonymous = excluded.anonymous,
updated_at = now();
else
raise exception '% no implemented', reference::text;
end if;

View File

@@ -450,8 +450,9 @@ execute procedure set_comment_parents_ids();
create table vote
(
anonymous boolean default true not null,
note int not null check ( note >= -1 and note <= 1 ),
updated_at timestamptz default now() not null check ( updated_at >= created_at ),
anonymous boolean default true not null,
note int not null check ( note >= -1 and note <= 1 ),
foreign key (created_by_id) references citizen (id),
primary key (id),
unique (created_by_id, target_id)