GET comments of article is paginate

This commit is contained in:
2019-10-01 09:54:18 +02:00
parent 3a77eff86e
commit be03bc4df8
5 changed files with 23 additions and 9 deletions

View File

@@ -0,0 +1,5 @@
package fr.dcproject.entity.request
class Comment (
val content: String
): Request

View File

@@ -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<CommentArticlePaths.ArticleCommentRequest> {
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<CommentArticlePaths.ArticleCommentRequest> {
assertCan(CREATE, it.article)
val content = call.receiveText()
val content = call.receive<CommentEntityRequest>().content
val comment = CommentEntity(
target = it.article,
createdBy = citizen,
content = content
)
assertCan(CREATE, comment)
repo.comment(comment)
call.respond(HttpStatusCode.Created, comment)

View File

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

View File

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

View File

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