#4 #20

Merged
flecomte merged 14 commits from #4 into master 2019-07-29 16:41:48 +02:00
4 changed files with 98 additions and 2 deletions
Showing only changes of commit 97900fba15 - Show all commits

View 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);

View 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);

View File

@@ -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)

View 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();