Add sort on comments -> findByTarget

This commit is contained in:
2020-02-05 15:20:38 +01:00
parent 42a41da066
commit eb7a0c7210
7 changed files with 75 additions and 17 deletions

5
.idea/misc.xml generated
View File

@@ -1,10 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" />
</component>
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="adopt-openjdk-1.8.0_232" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@@ -46,21 +46,24 @@ abstract class Comment<T : TargetI>(override var requester: Requester) : Reposit
open fun findByTarget(
target: UuidEntityI,
page: Int = 1,
limit: Int = 50
limit: Int = 50,
sort: CommentArticle.Sort = CommentArticle.Sort.CREATED_AT
): Paginated<CommentEntity<T>> {
return findByTarget(target.id, page, limit)
return findByTarget(target.id, page, limit, sort)
}
open fun findByTarget(
targetId: UUID,
page: Int = 1,
limit: Int = 50
limit: Int = 50,
sort: CommentArticle.Sort = CommentArticle.Sort.CREATED_AT
): Paginated<CommentEntity<T>> {
return requester.run {
getFunction("find_comments_by_target")
.select(
page, limit,
"target_id" to targetId
"target_id" to targetId,
"sort" to sort.sql
)
}
}
@@ -145,16 +148,27 @@ class CommentArticle(requester: Requester) : Comment<ArticleRef>(requester) {
override fun findByTarget(
target: UuidEntityI,
page: Int,
limit: Int
limit: Int,
sort: Sort
): Paginated<CommentEntity<ArticleRef>> {
return requester.run {
getFunction("find_comments_by_target")
.select(
page, limit,
"target_id" to target.id
"target_id" to target.id,
"sort" to sort.sql
)
}
}
enum class Sort(val sql: String) {
CREATED_AT("created_at"), VOTES("votes");
companion object {
fun fromString(string: String): Sort? {
return values().firstOrNull { it.sql == string }
}
}
}
}
class CommentConstitution(requester: Requester) : Comment<ConstitutionRef>(requester) {
@@ -182,13 +196,15 @@ class CommentConstitution(requester: Requester) : Comment<ConstitutionRef>(reque
override fun findByTarget(
target: UuidEntityI,
page: Int,
limit: Int
limit: Int,
sort: CommentArticle.Sort
): Paginated<CommentEntity<ConstitutionRef>> {
return requester.run {
getFunction("find_comments_by_target")
.select(
page, limit,
"target_id" to target.id
"target_id" to target.id,
"sort" to sort.sql
)
}
}

View File

@@ -3,6 +3,7 @@ package fr.dcproject.routes
import fr.dcproject.citizen
import fr.dcproject.entity.ArticleRef
import fr.dcproject.entity.Citizen
import fr.dcproject.repository.CommentArticle.Sort
import fr.dcproject.security.voter.CommentVoter.Action.CREATE
import fr.dcproject.security.voter.CommentVoter.Action.VIEW
import fr.dcproject.security.voter.assertCan
@@ -26,10 +27,12 @@ object CommentArticlePaths {
val article: ArticleRef,
page: Int = 1,
limit: Int = 50,
val search: String? = null
val search: String? = null,
sort: String = Sort.CREATED_AT.sql
) {
val page: Int = if (page < 1) 1 else page
val limit: Int = if (limit > 50) 50 else if (limit < 1) 1 else limit
val sort: Sort = Sort.fromString(sort) ?: Sort.CREATED_AT
}
@Location("/citizens/{citizen}/comments/articles")
@@ -39,7 +42,7 @@ object CommentArticlePaths {
@KtorExperimentalLocationsAPI
fun Route.commentArticle(repo: CommentArticleRepository) {
get<CommentArticlePaths.ArticleCommentRequest> {
val comment = repo.findByTarget(it.article, it.page, it.limit)
val comment = repo.findByTarget(it.article, it.page, it.limit, it.sort)
assertCan(VIEW, comment.result)
call.respond(HttpStatusCode.OK, comment)
}

View File

@@ -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

View File

@@ -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;

View File

@@ -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;
$$;

View File

@@ -17,6 +17,14 @@ Feature: comment Article
When I send a GET request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/comments"
Then the response status code should be 200
Scenario: Can get all comment on article sorted by votes
Given I am authenticated as John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd"
And I have article with id "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b"
When I send a GET request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/comments?sort=votes"
Then the response status code should be 200
And the response should contain object:
| result[1].votes.up | 1 |
Scenario: Can get comments on articles of the current citizen
Given I have citizen John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd"
And I have article with id "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b"
@@ -33,11 +41,11 @@ Feature: comment Article
Hello boy
"""
Then the response status code should be 200
And the JSON should contain:
| content | Hello boy |
And the JSON should contain:
| content | Hello boy |
Scenario: Can get comment by its ID
When I send a GET request to "/comments/2f01c257-cf20-3466-fb10-a3b8eff12a97"
Then the response status code should be 200
And the JSON should contain:
| content | Hello boy |
And the JSON should contain:
| content | Hello boy |