Implement comment constitution

fix bugs
This commit is contained in:
2019-08-28 23:32:43 +02:00
parent 33f4992b5e
commit 4c2da6ab71
17 changed files with 277 additions and 86 deletions

View File

@@ -3,16 +3,28 @@ create or replace function upsert_article(inout resource json)
$$
declare
new_id uuid;
_id_exist boolean;
begin
insert into article (version_id, created_by_id, title, annonymous, content, description, tags)
-- check if version id already exist
select count(*) >= 1
into _id_exist
from article
where (resource->>'id')::uuid is not null
and id = (resource->>'id')::uuid
-- and draft = false
;
insert into article (id, version_id, created_by_id, title, annonymous, content, description, tags)
select
coalesce(version_id, uuid_generate_v4()),
(resource#>>'{created_by, id}')::uuid,
title,
annonymous,
content,
description,
tags
case when _id_exist then uuid_generate_v4()
else coalesce(id, uuid_generate_v4()) end,
coalesce(version_id, uuid_generate_v4()),
(resource#>>'{created_by, id}')::uuid,
title,
annonymous,
content,
description,
tags
from json_populate_record(null::article, resource)
returning id into new_id;

View File

@@ -1,17 +1,13 @@
create or replace function edit_comment(reference regclass, _id uuid, _content text) returns void
create or replace function edit_comment(_id uuid, _content text) returns void
language plpgsql as
$$
begin
if reference = 'article'::regclass then
update comment_on_article c set
update comment c set
"content" = _content
where c.id = _id;
elseif reference = 'constitution'::regclass then
update comment_on_constitution c set
"content" = _content
where c.id = _id;
end if;
end;
$$;
-- drop function if exists edit_comment(regclass, uuid, text);
-- drop function if exists edit_comment(regclass, uuid, text);
-- select edit_comment('b0422e48-687f-bea7-b45f-b6b301246e97', 'plop4')

View File

@@ -1,27 +0,0 @@
create or replace function find_comments_article_by_citizen(
_created_by_id uuid,
"limit" int default 50,
"offset" int default 0,
out resource json,
out total int
) language plpgsql as
$$
begin
select json_agg(t), (select count(id) from comment where target_reference = 'article'::regclass)
into resource, total
from (
select
com.*,
find_article_by_id(com.target_id) as target,
find_citizen_by_id(com.created_by_id) as created_by
from comment as com
where created_by_id = _created_by_id
and target_reference = 'article'::regclass
order by created_at desc,
com.created_at desc
limit "limit" offset "offset"
) as t;
end;
$$;
-- drop function if exists find_comments_article_by_citizen(uuid, int, int);

View File

@@ -6,9 +6,23 @@ declare
_title json;
_citizen_id uuid = (resource#>>'{created_by, id}')::uuid;
new_id uuid;
_id_exist boolean;
begin
insert into constitution (version_id, created_by_id, title, annonymous)
-- check if version id already exist
select count(*) >= 1
into _id_exist
from constitution
where (resource->>'id')::uuid is not null
and id = (resource->>'id')::uuid
-- and draft = false
;
raise notice '%', _id_exist;
insert into constitution (id, version_id, created_by_id, title, annonymous)
select
case when _id_exist then uuid_generate_v4()
else coalesce(id, uuid_generate_v4()) end,
version_id,
_citizen_id,
title,

View File

@@ -10,7 +10,7 @@ begin
when 'article'::regclass then
find_article_by_id(_id)
when 'constitution'::regclass then
find_article_by_id(_id)
find_constitution_by_id(_id)
else
json_build_object('id', _id)
end
@@ -19,3 +19,5 @@ end;
$$;
-- drop function if exists find_reference_by_id(uuid, regclass, out json);
-- select find_reference_by_id('8944221c-3766-f952-7064-9f229c288049'::uuid, 'constitution'::regclass)