From be03bc4df8f1aa750f4b944ac31dea27a3592e27 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 1 Oct 2019 09:54:18 +0200 Subject: [PATCH] GET comments of article is paginate --- .../fr/dcproject/entity/request/Comment.kt | 5 +++++ .../fr/dcproject/routes/CommentArticle.kt | 22 ++++++++++++++----- .../comment/find_comments_by_citizen.sql | 1 - .../comment/find_comments_by_parent.sql | 2 +- .../comment/find_comments_by_target.sql | 2 +- 5 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 src/main/kotlin/fr/dcproject/entity/request/Comment.kt diff --git a/src/main/kotlin/fr/dcproject/entity/request/Comment.kt b/src/main/kotlin/fr/dcproject/entity/request/Comment.kt new file mode 100644 index 0000000..20f0c14 --- /dev/null +++ b/src/main/kotlin/fr/dcproject/entity/request/Comment.kt @@ -0,0 +1,5 @@ +package fr.dcproject.entity.request + +class Comment ( + val content: String +): Request \ No newline at end of file diff --git a/src/main/kotlin/fr/dcproject/routes/CommentArticle.kt b/src/main/kotlin/fr/dcproject/routes/CommentArticle.kt index d9aa456..dd7e268 100644 --- a/src/main/kotlin/fr/dcproject/routes/CommentArticle.kt +++ b/src/main/kotlin/fr/dcproject/routes/CommentArticle.kt @@ -11,36 +11,46 @@ import io.ktor.locations.KtorExperimentalLocationsAPI import io.ktor.locations.Location import io.ktor.locations.get import io.ktor.locations.post -import io.ktor.request.receiveText +import io.ktor.request.receive import io.ktor.response.respond import io.ktor.routing.Route import fr.dcproject.entity.Article as ArticleEntity import fr.dcproject.entity.Comment as CommentEntity +import fr.dcproject.entity.request.Comment as CommentEntityRequest import fr.dcproject.repository.CommentArticle as CommentArticleRepository @KtorExperimentalLocationsAPI object CommentArticlePaths { - @Location("/articles/{article}/comments") class ArticleCommentRequest(val article: ArticleEntity) + @Location("/articles/{article}/comments") + class ArticleCommentRequest( + val article: ArticleEntity, + page: Int = 1, + limit: Int = 50, + val search: String? = null + ) { + val page: Int = if (page < 1) 1 else page + val limit: Int = if (limit > 50) 50 else if (limit < 1) 1 else limit + } @Location("/citizens/{citizen}/comments/articles") class CitizenCommentArticleRequest(val citizen: Citizen) } @KtorExperimentalLocationsAPI fun Route.commentArticle(repo: CommentArticleRepository) { get { - val comment = repo.findByTarget(it.article) + val comment = repo.findByTarget(it.article, it.page, it.limit) assertCan(VIEW, comment.result) call.respond(HttpStatusCode.OK, comment) } post { - assertCan(CREATE, it.article) - - val content = call.receiveText() + val content = call.receive().content val comment = CommentEntity( target = it.article, createdBy = citizen, content = content ) + + assertCan(CREATE, comment) repo.comment(comment) call.respond(HttpStatusCode.Created, comment) diff --git a/src/main/resources/sql/functions/comment/find_comments_by_citizen.sql b/src/main/resources/sql/functions/comment/find_comments_by_citizen.sql index 3338b46..a91332c 100644 --- a/src/main/resources/sql/functions/comment/find_comments_by_citizen.sql +++ b/src/main/resources/sql/functions/comment/find_comments_by_citizen.sql @@ -30,7 +30,6 @@ begin and created_by_id = _created_by_id order by - created_at desc, com.created_at desc limit "limit" offset "offset" diff --git a/src/main/resources/sql/functions/comment/find_comments_by_parent.sql b/src/main/resources/sql/functions/comment/find_comments_by_parent.sql index 00b9d57..a842ad3 100644 --- a/src/main/resources/sql/functions/comment/find_comments_by_parent.sql +++ b/src/main/resources/sql/functions/comment/find_comments_by_parent.sql @@ -17,7 +17,7 @@ begin find_citizen_by_id(com.created_by_id) as created_by from "comment" as com where parent_id = _parent_id - order by created_at desc, + order by created_at asc, com.created_at desc limit "limit" offset "offset" ) as t; diff --git a/src/main/resources/sql/functions/comment/find_comments_by_target.sql b/src/main/resources/sql/functions/comment/find_comments_by_target.sql index 13b0373..42c3ad3 100644 --- a/src/main/resources/sql/functions/comment/find_comments_by_target.sql +++ b/src/main/resources/sql/functions/comment/find_comments_by_target.sql @@ -17,7 +17,7 @@ begin find_citizen_by_id(com.created_by_id) as created_by from "comment" as com where com.parent_id = _target_id - order by created_at desc, + order by created_at asc, com.created_at desc limit "limit" offset "offset" ) as t;