feature #4: create function for follow
This commit is contained in:
21
resources/functions/follow/follow.sql
Normal file
21
resources/functions/follow/follow.sql
Normal file
@@ -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);
|
||||||
16
resources/functions/follow/unfollow.sql
Normal file
16
resources/functions/follow/unfollow.sql
Normal file
@@ -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);
|
||||||
@@ -61,14 +61,14 @@ declare
|
|||||||
_version_id alias for version_id;
|
_version_id alias for version_id;
|
||||||
begin
|
begin
|
||||||
if (tablename = 'article'::regclass) then
|
if (tablename = 'article'::regclass) then
|
||||||
select version_number+1
|
select version_number + 1
|
||||||
into generated_number
|
into generated_number
|
||||||
from article as t
|
from article as t
|
||||||
where t.version_id = _version_id
|
where t.version_id = _version_id
|
||||||
order by version_number
|
order by version_number
|
||||||
limit 1;
|
limit 1;
|
||||||
elseif tablename = 'constitution'::regclass then
|
elseif tablename = 'constitution'::regclass then
|
||||||
select version_number+1
|
select version_number + 1
|
||||||
into generated_number
|
into generated_number
|
||||||
from constitution as t
|
from constitution as t
|
||||||
where t.version_id = _version_id
|
where t.version_id = _version_id
|
||||||
@@ -198,6 +198,7 @@ create table follow
|
|||||||
|
|
||||||
create table follow_article
|
create table follow_article
|
||||||
(
|
(
|
||||||
|
target_reference regclass default 'article'::regclass not null,
|
||||||
foreign key (citizen_id) references citizen (id),
|
foreign key (citizen_id) references citizen (id),
|
||||||
foreign key (target_id) references article (id),
|
foreign key (target_id) references article (id),
|
||||||
primary key (id)
|
primary key (id)
|
||||||
@@ -205,6 +206,7 @@ create table follow_article
|
|||||||
|
|
||||||
create table follow_constitution
|
create table follow_constitution
|
||||||
(
|
(
|
||||||
|
target_reference regclass default 'constitution'::regclass not null,
|
||||||
foreign key (citizen_id) references citizen (id),
|
foreign key (citizen_id) references citizen (id),
|
||||||
foreign key (target_id) references constitution (id),
|
foreign key (target_id) references constitution (id),
|
||||||
primary key (id)
|
primary key (id)
|
||||||
@@ -212,6 +214,7 @@ create table follow_constitution
|
|||||||
|
|
||||||
create table follow_citizen
|
create table follow_citizen
|
||||||
(
|
(
|
||||||
|
target_reference regclass default 'citizen'::regclass not null,
|
||||||
foreign key (citizen_id) references citizen (id),
|
foreign key (citizen_id) references citizen (id),
|
||||||
foreign key (target_id) references citizen (id),
|
foreign key (target_id) references citizen (id),
|
||||||
primary key (id)
|
primary key (id)
|
||||||
|
|||||||
56
resources/tests/follow.sql
Normal file
56
resources/tests/follow.sql
Normal file
@@ -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();
|
||||||
Reference in New Issue
Block a user