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

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)
}