refactoring: replace procedure find_citizen_by_* to function and include user into citizen object
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
create or replace procedure find_citizen_by_id(in id uuid, inout resource json) language plpgsql as
|
||||
create or replace function find_citizen_by_id(in id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_id alias for id;
|
||||
begin
|
||||
select to_json(z) into resource
|
||||
select to_json(t) into resource
|
||||
from (
|
||||
select
|
||||
z.*,
|
||||
find_user_by_id(z.user_id)
|
||||
from citizen as z
|
||||
where z.id = _id;
|
||||
where z.id = _id
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop procedure if exists find_citizen_by_id(uuid, inout json);
|
||||
-- drop function if exists find_citizen_by_id(uuid, inout json);
|
||||
@@ -1,12 +1,17 @@
|
||||
create or replace procedure find_citizen_by_user_id(in id uuid, inout resource json) language plpgsql as
|
||||
create or replace function find_citizen_by_user_id(in user_id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_id alias for id;
|
||||
_user_id alias for user_id;
|
||||
begin
|
||||
select to_json(z) into resource
|
||||
select to_json(t) into resource
|
||||
from (
|
||||
select
|
||||
z.*,
|
||||
find_user_by_id(z.user_id)
|
||||
from citizen as z
|
||||
where z.user_id = _id;
|
||||
where z.user_id = _user_id
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop procedure if exists find_citizen_by_user_id(uuid, inout json);
|
||||
-- drop function if exists find_citizen_by_user_id(uuid, inout json);
|
||||
@@ -26,11 +26,11 @@ begin
|
||||
end;
|
||||
|
||||
-- get citizen by id and check the first name
|
||||
call find_citizen_by_id((created_citizen->>'id')::uuid, selected_citizen);
|
||||
select find_citizen_by_id((created_citizen->>'id')::uuid) into selected_citizen;
|
||||
assert selected_citizen#>>'{name, first_name}' = 'George', format('first name must be George, %s', selected_citizen#>>'{name, first_name}');
|
||||
|
||||
-- get citizen by user id and check the first name
|
||||
call find_citizen_by_user_id((created_citizen->>'user_id')::uuid, selected_citizen);
|
||||
select find_citizen_by_user_id((created_citizen->>'user_id')::uuid) into selected_citizen;
|
||||
assert selected_citizen#>>'{name, first_name}' = 'George', format('first name must be George, %s', selected_citizen#>>'{name, first_name}');
|
||||
|
||||
-- delete citizen
|
||||
@@ -38,7 +38,7 @@ begin
|
||||
delete from "user" where username = 'george';
|
||||
|
||||
-- check if fint by id return null if citizen not exist
|
||||
call find_citizen_by_user_id((created_citizen->>'user_id')::uuid, selected_citizen);
|
||||
select find_citizen_by_user_id((created_citizen->>'user_id')::uuid) into selected_citizen;
|
||||
assert selected_citizen is null, format('citizen must be null if not exist, %s', selected_citizen);
|
||||
|
||||
raise notice 'citizen test pass';
|
||||
|
||||
Reference in New Issue
Block a user