From 97900fba15c2a8ae69ace00bf16ef5449e6f94bf Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sun, 28 Jul 2019 23:24:31 +0200 Subject: [PATCH] feature #4: create function for follow --- resources/functions/follow/follow.sql | 21 +++++++ resources/functions/follow/unfollow.sql | 16 ++++++ .../sql/migrations/0000-init_schema.up.sql | 7 ++- resources/tests/follow.sql | 56 +++++++++++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 resources/functions/follow/follow.sql create mode 100644 resources/functions/follow/unfollow.sql create mode 100644 resources/tests/follow.sql diff --git a/resources/functions/follow/follow.sql b/resources/functions/follow/follow.sql new file mode 100644 index 0000000..3043f5d --- /dev/null +++ b/resources/functions/follow/follow.sql @@ -0,0 +1,21 @@ +create or replace function follow(reference regclass, target_id uuid, citizen_id uuid) returns void + language plpgsql as +$$ +declare + _citizen_id alias for citizen_id; + _target_id alias for target_id; +begin + if reference = 'article'::regclass then + insert into follow_article (citizen_id, target_id) + values (_citizen_id, _target_id); + elseif reference = 'constitution'::regclass then + insert into follow_constitution (citizen_id, target_id) + values (_citizen_id, _target_id); + elseif reference = 'citizen'::regclass then + insert into follow_citizen (citizen_id, target_id) + values (_citizen_id, _target_id); + end if; +end; +$$; + +-- drop function if exists follow(regclass, uuid, uuid); \ No newline at end of file diff --git a/resources/functions/follow/unfollow.sql b/resources/functions/follow/unfollow.sql new file mode 100644 index 0000000..22ef62a --- /dev/null +++ b/resources/functions/follow/unfollow.sql @@ -0,0 +1,16 @@ +create or replace function unfollow(reference regclass, target_id uuid, citizen_id uuid) returns void + language plpgsql as +$$ +declare + _citizen_id alias for citizen_id; + _target_id alias for target_id; +begin + delete + from follow f + where f.citizen_id = _citizen_id + and f.target_id = _target_id + and f.target_reference = reference; +end; +$$; + +-- drop function if exists unfollow(regclass, uuid, 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 7a6ed8d..a4431ab 100644 --- a/resources/sql/migrations/0000-init_schema.up.sql +++ b/resources/sql/migrations/0000-init_schema.up.sql @@ -61,14 +61,14 @@ declare _version_id alias for version_id; begin if (tablename = 'article'::regclass) then - select version_number+1 + select version_number + 1 into generated_number from article as t where t.version_id = _version_id order by version_number limit 1; elseif tablename = 'constitution'::regclass then - select version_number+1 + select version_number + 1 into generated_number from constitution as t where t.version_id = _version_id @@ -198,6 +198,7 @@ create table follow create table follow_article ( + target_reference regclass default 'article'::regclass not null, foreign key (citizen_id) references citizen (id), foreign key (target_id) references article (id), primary key (id) @@ -205,6 +206,7 @@ create table follow_article create table follow_constitution ( + target_reference regclass default 'constitution'::regclass not null, foreign key (citizen_id) references citizen (id), foreign key (target_id) references constitution (id), primary key (id) @@ -212,6 +214,7 @@ create table follow_constitution create table follow_citizen ( + target_reference regclass default 'citizen'::regclass not null, foreign key (citizen_id) references citizen (id), foreign key (target_id) references citizen (id), primary key (id) diff --git a/resources/tests/follow.sql b/resources/tests/follow.sql new file mode 100644 index 0000000..2a8ba3d --- /dev/null +++ b/resources/tests/follow.sql @@ -0,0 +1,56 @@ +do +$$ +declare + created_user json := '{"username": "george", "plain_password": "azerty"}'; + created_user2 json := '{"username": "john", "plain_password": "qwerty"}'; + _citizen_id uuid; + _citizen_id2 uuid; + created_citizen json := $json$ + { + "name": { + "first_name": "George", + "last_name": "MICHEL" + }, + "birthday": "2001-01-01" + } + $json$; + created_citizen2 json := $json$ + { + "name": { + "first_name": "John", + "last_name": "Doe" + }, + "birthday": "2002-01-01" + } + $json$; +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; + created_citizen2 := jsonb_set(created_citizen2::jsonb, '{user}'::text[], jsonb_build_object('id', created_user2->>'id'), true)::json; + + -- insert new citizen for context + call upsert_citizen(created_citizen); + _citizen_id := created_citizen->>'id'; + -- insert new citizen for context + call upsert_citizen(created_citizen2); + _citizen_id2 := created_citizen2->>'id'; + + + perform follow('citizen'::regclass, _citizen_id, _citizen_id2); + assert (select count(*) > 0 from follow), 'follow must be inserted'; + + perform unfollow('citizen'::regclass, _citizen_id, _citizen_id2); + assert (select count(*) = 0 from follow), 'follow must be deleted after unfollow'; + + -- delete article and context + delete from citizen; + delete from "user"; + + raise notice 'follow test pass'; +end; +$$; + + +-- select uuid_generate_v4(); \ No newline at end of file