Refactors Articles and Voter

- Move files into components (article)
- Split articles routes
- Refactoring for remove ktor-voter (ArticleVoter)
- Remove mutability
- Move DataConversion to separate file (Converter.kt)
- Add Schemas for Articles routes
- Fix SQL Query for Workgroup roles
- rename container_name in docker-compose
This commit is contained in:
2021-01-14 11:23:27 +01:00
parent 03401f711e
commit a1c1accc87
124 changed files with 2026 additions and 1828 deletions

View File

@@ -1342,8 +1342,6 @@ components:
properties:
user:
$ref: '#/components/schemas/UserResponse'
workgroups:
$ref: '#/components/schemas/WorkgroupSimple'
CitizenBase:
type: object
properties:
@@ -1375,8 +1373,6 @@ components:
properties:
user:
$ref: '#/components/schemas/UserRequest'
workgroups:
$ref: '#/components/schemas/UuidEntity'
RegisterRequest:
$ref: '#/components/schemas/CitizenRequest'

View File

@@ -6,7 +6,16 @@ begin
select to_json(t)
from (
select
a.*,
a.id,
a.version_number,
a.version_id,
a.title,
a.anonymous,
a.content,
a.description,
a.tags,
a.draft,
a.last_version,
find_citizen_by_id_with_user(a.created_by_id) as created_by,
find_workgroup_by_id(a.workgroup_id) as workgroup,
count_vote(a.id) as votes,

View File

@@ -21,11 +21,13 @@ begin
into resource, total
from (
select
a.*,
a.id,
a.title,
a.deleted_at,
a.draft,
find_citizen_by_id_with_user(a.created_by_id) as created_by,
find_workgroup_by_id(a.workgroup_id) as workgroup,
count_vote(a.id) as votes,
count_opinion(a.id) as opinions,
zdb.score(a.ctid) _score
from article as a
left join vote_cache ca using (id)

View File

@@ -54,10 +54,10 @@ begin
insert into article_relations (source_id, target_id, created_by_id)
select
(resource->>'id')::uuid,
(rel->>'id')::uuid,
id,
(resource#>>'{created_by, id}')::uuid
from json_populate_recordset(null::article, resource->>'relations');
(rel#>>'{created_by, id}')::uuid
from json_populate_recordset(null::article, resource->>'relations') rel;
end if;
select find_article_by_id(new_id) into resource;

View File

@@ -8,7 +8,6 @@ begin
select
z.*
from citizen as z
left join citizen_in_workgroup ciw on z.id = ciw.citizen_id
where z.id = _id
group by z.id
) as t;

View File

@@ -9,7 +9,6 @@ begin
z.*,
find_user_by_id(z.user_id) as "user"
from citizen as z
left join citizen_in_workgroup ciw on z.id = ciw.citizen_id
where z.id = _id
group by z.id
) as t;

View File

@@ -9,6 +9,8 @@ begin
from (
select
com.*,
(select count(*) from "comment" c2 where c2.parents_ids @> array[com.id]) as children_count,
find_comment_parent_by_id(com.parent_id) as parent,
find_reference_by_id(com.target_id, com.target_reference) as target,
find_citizen_by_id_with_user(com.created_by_id) as created_by,
count_vote(com.id) as votes

View File

@@ -0,0 +1,20 @@
create or replace function find_comment_parent_by_id(
_parent_id uuid,
out resource json
) language plpgsql as
$$
begin
select to_json(t)
into resource
from (
select
id,
deleted_at,
json_build_object('id', target_id, 'reference', target_reference) as target
from "comment" cp
where id = _parent_id
) as t;
end;
$$;

View File

@@ -20,6 +20,8 @@ begin
from (
select
com.*,
(select count(*) from "comment" c2 where c2.parents_ids @> array[com.id]) as children_count,
find_comment_parent_by_id(com.parent_id) as parent,
find_reference_by_id(com.target_id, _reference) as target,
find_citizen_by_id_with_user(com.created_by_id) as created_by,
count_vote(com.id) as votes

View File

@@ -13,6 +13,7 @@ begin
select
com.*,
(select count(*) from "comment" c2 where c2.parents_ids @> array[com.id]) as children_count,
find_comment_parent_by_id(com.parent_id) as parent,
find_reference_by_id(com.target_id, com.target_reference) as target,
find_citizen_by_id_with_user(com.created_by_id) as created_by,
count_vote(com.id) as votes

View File

@@ -14,6 +14,7 @@ begin
select
com.*,
(select count(c2) from "comment" c2 where c2.parent_comment_id = com.id) as children_count,
find_comment_parent_by_id(com.parent_id) as parent,
find_reference_by_id(com.target_id, com.target_reference) as target,
find_citizen_by_id_with_user(com.created_by_id) as created_by,
count_vote(com.id) as votes

View File

@@ -0,0 +1,5 @@
create or replace function json_to_array(json json) returns text[] language sql
immutable parallel safe as
$$
select array(select json_array_elements_text(json))
$$;

View File

@@ -1,31 +1,32 @@
create or replace function vote(reference regclass, _target_id uuid, _created_by_id uuid, _note int, _anonymous bool default true, out resource json)
create or replace function vote(reference regclass, _target_id uuid, _created_by_id uuid, _note int, _anonymous bool default null, out resource json)
language plpgsql as
$$
declare _anonymous_conf bool = coalesce(_anonymous, (select vote_anonymous from citizen where id = _created_by_id));
begin
if reference = 'article'::regclass then
insert into vote_for_article (created_by_id, target_id, note, anonymous)
values (_created_by_id, _target_id, _note, _anonymous)
values (_created_by_id, _target_id, _note, _anonymous_conf)
on conflict (created_by_id, target_id) do update set
note = excluded.note,
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)
values (_created_by_id, _target_id, _note, _anonymous_conf)
on conflict (created_by_id, target_id) do update set
note = excluded.note,
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)
values (_created_by_id, _target_id, _note, _anonymous_conf)
on conflict (created_by_id, target_id) do update set
note = excluded.note,
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)
values (_created_by_id, _target_id, _note, _anonymous_conf)
on conflict (created_by_id, target_id) do update set
note = excluded.note,
anonymous = excluded.anonymous,

View File

@@ -23,9 +23,9 @@ begin
insert into citizen_in_workgroup (workgroup_id, citizen_id, roles)
select
new_id::uuid,
citizen_id,
roles
from json_populate_recordset(null::citizen_in_workgroup, resource->'members') m;
(m#>>'{citizen,id}')::uuid,
json_to_array(m#>'{roles}')
from json_array_elements(resource->'members') m;
-- insert master if no members
if (exists) then

View File

@@ -11,6 +11,7 @@ drop table if exists follow_constitution;
drop table if exists follow_citizen;
drop table if exists follow;
drop table if exists vote_cache;
drop table if exists vote_for_article;
drop table if exists vote_for_constitution;
drop table if exists vote_for_comment_on_article;