diff --git a/src/main/resources/openApi.yaml b/src/main/resources/openApi.yaml index 44fadf0..084a002 100644 --- a/src/main/resources/openApi.yaml +++ b/src/main/resources/openApi.yaml @@ -208,7 +208,7 @@ paths: parameters: - $ref: '#/components/parameters/page' - $ref: '#/components/parameters/limit' - - $ref: '#/components/parameters/sort' + - $ref: '#/components/parameters/articleSort' - $ref: '#/components/parameters/direction' - $ref: '#/components/parameters/search' responses: @@ -721,6 +721,19 @@ components: required: false schema: type: string + articleSort: + name: sort + in: query + description: The sort field name + example: createdAt + required: false + schema: + type: string + enum: + - title + - createdAt + - vote + - popularity direction: name: direction in: query diff --git a/src/main/resources/sql/functions/article/find_articles.sql b/src/main/resources/sql/functions/article/find_articles.sql index d181806..5a33be3 100644 --- a/src/main/resources/sql/functions/article/find_articles.sql +++ b/src/main/resources/sql/functions/article/find_articles.sql @@ -23,12 +23,14 @@ begin or _search = '' or a ==> dsl.multi_match('{title^3, content, description, tags}', _search) ) and a.last_version = true + order by - _score desc, case direction when 'asc' then case sort when 'title' then a.title when 'created_at' then a.created_at::text + when 'vote' then count_vote(a.id)->>'score' + when 'popularity' then count_vote(a.id)->>'total' else null end end, @@ -36,6 +38,8 @@ begin case sort when 'title' then a.title when 'created_at' then a.created_at::text + when 'vote' then count_vote(a.id)->>'score' + when 'popularity' then count_vote(a.id)->>'total' end end desc,