feature #6: implement route for search articles

This commit is contained in:
2019-07-31 23:07:58 +02:00
parent 3bf7ee1d13
commit 4c13d18e8d
11 changed files with 97 additions and 38 deletions

View File

@@ -4,7 +4,7 @@
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="trace">
<root level="debug">
<appender-ref ref="STDOUT"/>
</root>
<logger name="org.eclipse.jetty" level="INFO"/>

View File

@@ -16,5 +16,3 @@ end;
$$;
-- drop function if exists find_article_by_id(uuid, out json);
select find_article_by_id('12fed2f3-2a7d-434b-87e6-5a9895f9d12d')

View File

@@ -0,0 +1,41 @@
create or replace function find_articles(
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 article)
into resource, total
from (
select
a.*,
find_citizen_by_id(a.created_by_id) as created_by
from article as a
where title ilike '%'||"search"||'%'
order by
case direction when 'asc' then
case sort
when 'title' then a.title
when 'created_at' then a.created_at::text
else null
end
end,
case direction when 'desc' then
case sort
when 'title' then a.title
when 'created_at' then a.created_at::text
end
end
desc,
a.created_at desc
limit "limit" offset "offset"
) as t;
end;
$$;
-- drop function if exists find_articles(json, int, int);

View File

@@ -6,8 +6,7 @@ begin
select to_json(t) into resource
from (
select
z.*,
find_user_by_id(z.user_id) as "user"
z.*
from citizen as z
where z.id = _id
) as t;