Add sort on comments -> findByTarget
This commit is contained in:
@@ -449,6 +449,22 @@ paths:
|
||||
tags:
|
||||
- comment
|
||||
- article
|
||||
parameters:
|
||||
- $ref: '#/components/parameters/page'
|
||||
- $ref: '#/components/parameters/limit'
|
||||
- $ref: '#/components/parameters/search'
|
||||
- name: sort
|
||||
in: query
|
||||
required: false
|
||||
example:
|
||||
- created_at
|
||||
- votes
|
||||
schema:
|
||||
type: string
|
||||
default: created_at
|
||||
enum:
|
||||
- created_at
|
||||
- votes
|
||||
responses:
|
||||
200:
|
||||
description: Return Comment and children
|
||||
|
||||
@@ -2,6 +2,7 @@ create or replace function find_comments_by_target(
|
||||
_target_id uuid,
|
||||
"limit" int default 50,
|
||||
"offset" int default 0,
|
||||
"sort" text default 'created_at',
|
||||
out resource json,
|
||||
out total int
|
||||
) language plpgsql as
|
||||
@@ -18,7 +19,15 @@ begin
|
||||
count_vote(com.id) as votes
|
||||
from "comment" as com
|
||||
where com.target_id = _target_id
|
||||
order by created_at asc,
|
||||
order by
|
||||
case sort
|
||||
when 'votes' then (count_vote(com.id)->>'percent')::int
|
||||
else null
|
||||
end desc,
|
||||
case sort
|
||||
when 'created_at' then com.created_at::text
|
||||
else null
|
||||
end desc,
|
||||
com.created_at desc
|
||||
limit "limit" offset "offset"
|
||||
) as t;
|
||||
|
||||
@@ -5,6 +5,7 @@ declare
|
||||
agg jsonb;
|
||||
empty jsonb = '{"down":0,"neutral":0,"up":0,"total":0,"score":0}'::jsonb;
|
||||
score int;
|
||||
percent numeric;
|
||||
total int;
|
||||
begin
|
||||
select jsonb_object_agg(t.label, t.total)
|
||||
@@ -25,12 +26,14 @@ begin
|
||||
|
||||
agg = empty || coalesce(agg, empty);
|
||||
score = ((agg->>'up')::int - (agg->>'down')::int);
|
||||
percent = ((agg->>'up')::int - (agg->>'down')::int);
|
||||
total = ((agg->>'up')::int + (agg->>'down')::int + (agg->>'neutral')::int);
|
||||
|
||||
resource = agg ||
|
||||
jsonb_build_object('updated_at', now()) ||
|
||||
jsonb_build_object('total', total) ||
|
||||
jsonb_build_object('score', score);
|
||||
jsonb_build_object('score', score) ||
|
||||
jsonb_build_object('percent', percent);
|
||||
end;
|
||||
$$;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user