From 60b967c2a85c19ae83202b5cd80e95ad300cb1b9 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 29 Jul 2019 13:34:10 +0200 Subject: [PATCH] feature #4: create function for comment --- resources/functions/comment/comment.sql | 25 +++++++ resources/functions/comment/edit_comment.sql | 20 ++++++ .../sql/migrations/0000-init_schema.up.sql | 2 + resources/tests/comment.sql | 71 +++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 resources/functions/comment/comment.sql create mode 100644 resources/functions/comment/edit_comment.sql create mode 100644 resources/tests/comment.sql diff --git a/resources/functions/comment/comment.sql b/resources/functions/comment/comment.sql new file mode 100644 index 0000000..bbd9676 --- /dev/null +++ b/resources/functions/comment/comment.sql @@ -0,0 +1,25 @@ +create or replace function comment(reference regclass, target_id uuid, citizen_id uuid, content text, parent_id uuid default null, out id uuid) + language plpgsql as +$$ +declare + _citizen_id alias for citizen_id; + _target_id alias for target_id; + _content alias for content; + _parent_id alias for parent_id; + _id alias for id; +begin + if reference = 'article'::regclass then + insert into comment_on_article (citizen_id, target_id, content, parent_id) + values (_citizen_id, _target_id, _content, _parent_id) + returning comment_on_article.id into _id; + elseif reference = 'constitution'::regclass then + insert into comment_on_constitution (citizen_id, target_id, content, parent_id) + values (_citizen_id, _target_id, _content, _parent_id) + returning comment_on_constitution.id into _id; + else + raise exception '% no implemented', reference::text; + end if; +end; +$$; + +-- drop function if exists comment(regclass, uuid, uuid, text, uuid); \ No newline at end of file diff --git a/resources/functions/comment/edit_comment.sql b/resources/functions/comment/edit_comment.sql new file mode 100644 index 0000000..f747650 --- /dev/null +++ b/resources/functions/comment/edit_comment.sql @@ -0,0 +1,20 @@ +create or replace function edit_comment(reference regclass, id uuid, content text) returns void + language plpgsql as +$$ +declare + _id alias for id; + _content alias for content; +begin + if reference = 'article'::regclass then + update comment_on_article 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, uuid, text, uuid); \ No newline at end of file diff --git a/resources/sql/migrations/0000-init_schema.up.sql b/resources/sql/migrations/0000-init_schema.up.sql index a4431ab..1500e70 100644 --- a/resources/sql/migrations/0000-init_schema.up.sql +++ b/resources/sql/migrations/0000-init_schema.up.sql @@ -233,6 +233,7 @@ create table comment create table comment_on_article ( + target_reference regclass default 'article'::regclass not null, foreign key (citizen_id) references citizen (id), foreign key (target_id) references article (id), foreign key (parent_id) references comment_on_article (id), @@ -241,6 +242,7 @@ create table comment_on_article create table comment_on_constitution ( + target_reference regclass default 'constitution'::regclass not null, foreign key (citizen_id) references citizen (id), foreign key (target_id) references constitution (id), foreign key (parent_id) references comment_on_constitution (id), diff --git a/resources/tests/comment.sql b/resources/tests/comment.sql new file mode 100644 index 0000000..677874b --- /dev/null +++ b/resources/tests/comment.sql @@ -0,0 +1,71 @@ +do +$$ +declare + created_user json := '{"username": "george", "plain_password": "azerty"}'; + created_user2 json := '{"username": "john", "plain_password": "qwerty"}'; + _citizen_id uuid; + created_citizen json := $json$ + { + "name": { + "first_name": "George", + "last_name": "MICHEL" + }, + "birthday": "2001-01-01" + } + $json$; + created_article json := $json$ + { + "version_id": "933b6a1b-50c9-42b6-989f-c02a57814ef9", + "title": "Love the world", + "annonymous": false, + "content": "bla bal bla", + "tags": [ + "love", + "test" + ] + } + $json$; + _comment_id uuid; +begin + -- insert user for context + call insert_user(created_user); + call insert_user(created_user2); + created_citizen := jsonb_set(created_citizen::jsonb, '{user}'::text[], jsonb_build_object('id', created_user->>'id'), true)::json; + + -- insert new citizen for context + call upsert_citizen(created_citizen); + _citizen_id := created_citizen->>'id'; + created_article := jsonb_set(created_article::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json; + assert created_article#>>'{created_by, id}' = _citizen_id::text, format('citizenId in article must be the same as citizen, %s != %s', created_article#>>'{created_by, id}', _citizen_id::text); + -- upsert article + call upsert_article(created_article); + + + select comment( + reference => 'article'::regclass, + target_id => (created_article->>'id')::uuid, + citizen_id => _citizen_id, + content => 'Ho my god !'::text + ) into _comment_id; + assert (select count(*) = 1 from "comment"), 'comment must be inserted'; + + perform edit_comment( + reference => 'article'::regclass, + id => _comment_id, + content => 'edited'::text + ); + assert (select count(*) = 1 from "comment"), 'edit comment must not insert new comment'; + assert (select count(*) = 1 from "comment" where content = 'edited'), 'edit comment must not insert new comment'; + + -- delete article and context + delete from "comment"; + delete from article; + delete from citizen; + delete from "user"; + + raise notice 'comment test pass'; +end; +$$; + + +-- select uuid_generate_v4(); \ No newline at end of file