Add sort on comments -> findByTarget
This commit is contained in:
5
.idea/misc.xml
generated
5
.idea/misc.xml
generated
@@ -1,10 +1,13 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="FrameworkDetectionExcludesConfiguration">
|
||||||
|
<file type="web" url="file://$PROJECT_DIR$" />
|
||||||
|
</component>
|
||||||
<component name="JavaScriptSettings">
|
<component name="JavaScriptSettings">
|
||||||
<option name="languageLevel" value="ES6" />
|
<option name="languageLevel" value="ES6" />
|
||||||
</component>
|
</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" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -46,21 +46,24 @@ abstract class Comment<T : TargetI>(override var requester: Requester) : Reposit
|
|||||||
open fun findByTarget(
|
open fun findByTarget(
|
||||||
target: UuidEntityI,
|
target: UuidEntityI,
|
||||||
page: Int = 1,
|
page: Int = 1,
|
||||||
limit: Int = 50
|
limit: Int = 50,
|
||||||
|
sort: CommentArticle.Sort = CommentArticle.Sort.CREATED_AT
|
||||||
): Paginated<CommentEntity<T>> {
|
): Paginated<CommentEntity<T>> {
|
||||||
return findByTarget(target.id, page, limit)
|
return findByTarget(target.id, page, limit, sort)
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun findByTarget(
|
open fun findByTarget(
|
||||||
targetId: UUID,
|
targetId: UUID,
|
||||||
page: Int = 1,
|
page: Int = 1,
|
||||||
limit: Int = 50
|
limit: Int = 50,
|
||||||
|
sort: CommentArticle.Sort = CommentArticle.Sort.CREATED_AT
|
||||||
): Paginated<CommentEntity<T>> {
|
): Paginated<CommentEntity<T>> {
|
||||||
return requester.run {
|
return requester.run {
|
||||||
getFunction("find_comments_by_target")
|
getFunction("find_comments_by_target")
|
||||||
.select(
|
.select(
|
||||||
page, limit,
|
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(
|
override fun findByTarget(
|
||||||
target: UuidEntityI,
|
target: UuidEntityI,
|
||||||
page: Int,
|
page: Int,
|
||||||
limit: Int
|
limit: Int,
|
||||||
|
sort: Sort
|
||||||
): Paginated<CommentEntity<ArticleRef>> {
|
): Paginated<CommentEntity<ArticleRef>> {
|
||||||
return requester.run {
|
return requester.run {
|
||||||
getFunction("find_comments_by_target")
|
getFunction("find_comments_by_target")
|
||||||
.select(
|
.select(
|
||||||
page, limit,
|
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) {
|
class CommentConstitution(requester: Requester) : Comment<ConstitutionRef>(requester) {
|
||||||
@@ -182,13 +196,15 @@ class CommentConstitution(requester: Requester) : Comment<ConstitutionRef>(reque
|
|||||||
override fun findByTarget(
|
override fun findByTarget(
|
||||||
target: UuidEntityI,
|
target: UuidEntityI,
|
||||||
page: Int,
|
page: Int,
|
||||||
limit: Int
|
limit: Int,
|
||||||
|
sort: CommentArticle.Sort
|
||||||
): Paginated<CommentEntity<ConstitutionRef>> {
|
): Paginated<CommentEntity<ConstitutionRef>> {
|
||||||
return requester.run {
|
return requester.run {
|
||||||
getFunction("find_comments_by_target")
|
getFunction("find_comments_by_target")
|
||||||
.select(
|
.select(
|
||||||
page, limit,
|
page, limit,
|
||||||
"target_id" to target.id
|
"target_id" to target.id,
|
||||||
|
"sort" to sort.sql
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package fr.dcproject.routes
|
|||||||
import fr.dcproject.citizen
|
import fr.dcproject.citizen
|
||||||
import fr.dcproject.entity.ArticleRef
|
import fr.dcproject.entity.ArticleRef
|
||||||
import fr.dcproject.entity.Citizen
|
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.CREATE
|
||||||
import fr.dcproject.security.voter.CommentVoter.Action.VIEW
|
import fr.dcproject.security.voter.CommentVoter.Action.VIEW
|
||||||
import fr.dcproject.security.voter.assertCan
|
import fr.dcproject.security.voter.assertCan
|
||||||
@@ -26,10 +27,12 @@ object CommentArticlePaths {
|
|||||||
val article: ArticleRef,
|
val article: ArticleRef,
|
||||||
page: Int = 1,
|
page: Int = 1,
|
||||||
limit: Int = 50,
|
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 page: Int = if (page < 1) 1 else page
|
||||||
val limit: Int = if (limit > 50) 50 else if (limit < 1) 1 else limit
|
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")
|
@Location("/citizens/{citizen}/comments/articles")
|
||||||
@@ -39,7 +42,7 @@ object CommentArticlePaths {
|
|||||||
@KtorExperimentalLocationsAPI
|
@KtorExperimentalLocationsAPI
|
||||||
fun Route.commentArticle(repo: CommentArticleRepository) {
|
fun Route.commentArticle(repo: CommentArticleRepository) {
|
||||||
get<CommentArticlePaths.ArticleCommentRequest> {
|
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)
|
assertCan(VIEW, comment.result)
|
||||||
call.respond(HttpStatusCode.OK, comment)
|
call.respond(HttpStatusCode.OK, comment)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -449,6 +449,22 @@ paths:
|
|||||||
tags:
|
tags:
|
||||||
- comment
|
- comment
|
||||||
- article
|
- 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:
|
responses:
|
||||||
200:
|
200:
|
||||||
description: Return Comment and children
|
description: Return Comment and children
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ create or replace function find_comments_by_target(
|
|||||||
_target_id uuid,
|
_target_id uuid,
|
||||||
"limit" int default 50,
|
"limit" int default 50,
|
||||||
"offset" int default 0,
|
"offset" int default 0,
|
||||||
|
"sort" text default 'created_at',
|
||||||
out resource json,
|
out resource json,
|
||||||
out total int
|
out total int
|
||||||
) language plpgsql as
|
) language plpgsql as
|
||||||
@@ -18,7 +19,15 @@ begin
|
|||||||
count_vote(com.id) as votes
|
count_vote(com.id) as votes
|
||||||
from "comment" as com
|
from "comment" as com
|
||||||
where com.target_id = _target_id
|
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
|
com.created_at desc
|
||||||
limit "limit" offset "offset"
|
limit "limit" offset "offset"
|
||||||
) as t;
|
) as t;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ declare
|
|||||||
agg jsonb;
|
agg jsonb;
|
||||||
empty jsonb = '{"down":0,"neutral":0,"up":0,"total":0,"score":0}'::jsonb;
|
empty jsonb = '{"down":0,"neutral":0,"up":0,"total":0,"score":0}'::jsonb;
|
||||||
score int;
|
score int;
|
||||||
|
percent numeric;
|
||||||
total int;
|
total int;
|
||||||
begin
|
begin
|
||||||
select jsonb_object_agg(t.label, t.total)
|
select jsonb_object_agg(t.label, t.total)
|
||||||
@@ -25,12 +26,14 @@ begin
|
|||||||
|
|
||||||
agg = empty || coalesce(agg, empty);
|
agg = empty || coalesce(agg, empty);
|
||||||
score = ((agg->>'up')::int - (agg->>'down')::int);
|
score = ((agg->>'up')::int - (agg->>'down')::int);
|
||||||
|
percent = ((agg->>'up')::int - (agg->>'down')::int);
|
||||||
total = ((agg->>'up')::int + (agg->>'down')::int + (agg->>'neutral')::int);
|
total = ((agg->>'up')::int + (agg->>'down')::int + (agg->>'neutral')::int);
|
||||||
|
|
||||||
resource = agg ||
|
resource = agg ||
|
||||||
jsonb_build_object('updated_at', now()) ||
|
jsonb_build_object('updated_at', now()) ||
|
||||||
jsonb_build_object('total', total) ||
|
jsonb_build_object('total', total) ||
|
||||||
jsonb_build_object('score', score);
|
jsonb_build_object('score', score) ||
|
||||||
|
jsonb_build_object('percent', percent);
|
||||||
end;
|
end;
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,14 @@ Feature: comment Article
|
|||||||
When I send a GET request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/comments"
|
When I send a GET request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/comments"
|
||||||
Then the response status code should be 200
|
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
|
Scenario: Can get comments on articles of the current citizen
|
||||||
Given I have citizen John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd"
|
Given I have citizen John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd"
|
||||||
And I have article with id "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b"
|
And I have article with id "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b"
|
||||||
@@ -33,11 +41,11 @@ Feature: comment Article
|
|||||||
Hello boy
|
Hello boy
|
||||||
"""
|
"""
|
||||||
Then the response status code should be 200
|
Then the response status code should be 200
|
||||||
And the JSON should contain:
|
And the JSON should contain:
|
||||||
| content | Hello boy |
|
| content | Hello boy |
|
||||||
|
|
||||||
Scenario: Can get comment by its ID
|
Scenario: Can get comment by its ID
|
||||||
When I send a GET request to "/comments/2f01c257-cf20-3466-fb10-a3b8eff12a97"
|
When I send a GET request to "/comments/2f01c257-cf20-3466-fb10-a3b8eff12a97"
|
||||||
Then the response status code should be 200
|
Then the response status code should be 200
|
||||||
And the JSON should contain:
|
And the JSON should contain:
|
||||||
| content | Hello boy |
|
| content | Hello boy |
|
||||||
|
|||||||
Reference in New Issue
Block a user