diff --git a/resources/functions/citizen/find_citizen_by_id.sql b/resources/functions/citizen/find_citizen_by_id.sql index 9ca616e..3b8e675 100644 --- a/resources/functions/citizen/find_citizen_by_id.sql +++ b/resources/functions/citizen/find_citizen_by_id.sql @@ -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 - from citizen as z - where z.id = _id; + select to_json(t) into resource + from ( + select + z.*, + find_user_by_id(z.user_id) + from citizen as z + where z.id = _id + ) as t; end; $$; --- drop procedure if exists find_citizen_by_id(uuid, inout json); \ No newline at end of file +-- drop function if exists find_citizen_by_id(uuid, inout json); \ No newline at end of file diff --git a/resources/functions/citizen/find_citizen_by_user_id.sql b/resources/functions/citizen/find_citizen_by_user_id.sql index 0f9ffe3..3d62d1c 100644 --- a/resources/functions/citizen/find_citizen_by_user_id.sql +++ b/resources/functions/citizen/find_citizen_by_user_id.sql @@ -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 - from citizen as z - where z.user_id = _id; + select to_json(t) into resource + from ( + select + z.*, + find_user_by_id(z.user_id) + from citizen as z + where z.user_id = _user_id + ) as t; end; $$; --- drop procedure if exists find_citizen_by_user_id(uuid, inout json); \ No newline at end of file +-- drop function if exists find_citizen_by_user_id(uuid, inout json); \ No newline at end of file diff --git a/resources/tests/citizen.sql b/resources/tests/citizen.sql index 230ae6d..a7be3f4 100644 --- a/resources/tests/citizen.sql +++ b/resources/tests/citizen.sql @@ -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';