feature #23: create route for citizen

This commit is contained in:
2019-08-15 15:59:46 +02:00
parent 9821833dd8
commit 7ae30bd3cd
7 changed files with 141 additions and 1 deletions

View File

@@ -0,0 +1,43 @@
create or replace function find_citizens(
search text default null,
direction text default 'desc',
sort text default 'created_at',
"limit" int default 50,
"offset" int default 0,
out resource json,
out total int
) language plpgsql as
$$
begin
select json_agg(t), (select count(id) from citizen)
into resource, total
from (
select
z.*
from citizen as z
where "search" is null or (
(name->'first_name')::text ilike '%'||"search"||'%' or
(name->'last_name')::text ilike '%'||"search"||'%'
)
order by
case direction when 'asc' then
case sort
when 'name' then (z.name->'first_name')::text
when 'created_at' then z.created_at::text
else null
end
end,
case direction when 'desc' then
case sort
when 'name' then (z.name->'first_name')::text
when 'created_at' then z.created_at::text
end
end
desc,
z.created_at desc
limit "limit" offset "offset"
) as t;
end;
$$;
-- drop function if exists find_citizens(text, text, text, int, int);