GET comments of article is paginate
This commit is contained in:
5
src/main/kotlin/fr/dcproject/entity/request/Comment.kt
Normal file
5
src/main/kotlin/fr/dcproject/entity/request/Comment.kt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package fr.dcproject.entity.request
|
||||||
|
|
||||||
|
class Comment (
|
||||||
|
val content: String
|
||||||
|
): Request
|
||||||
@@ -11,36 +11,46 @@ import io.ktor.locations.KtorExperimentalLocationsAPI
|
|||||||
import io.ktor.locations.Location
|
import io.ktor.locations.Location
|
||||||
import io.ktor.locations.get
|
import io.ktor.locations.get
|
||||||
import io.ktor.locations.post
|
import io.ktor.locations.post
|
||||||
import io.ktor.request.receiveText
|
import io.ktor.request.receive
|
||||||
import io.ktor.response.respond
|
import io.ktor.response.respond
|
||||||
import io.ktor.routing.Route
|
import io.ktor.routing.Route
|
||||||
import fr.dcproject.entity.Article as ArticleEntity
|
import fr.dcproject.entity.Article as ArticleEntity
|
||||||
import fr.dcproject.entity.Comment as CommentEntity
|
import fr.dcproject.entity.Comment as CommentEntity
|
||||||
|
import fr.dcproject.entity.request.Comment as CommentEntityRequest
|
||||||
import fr.dcproject.repository.CommentArticle as CommentArticleRepository
|
import fr.dcproject.repository.CommentArticle as CommentArticleRepository
|
||||||
|
|
||||||
@KtorExperimentalLocationsAPI
|
@KtorExperimentalLocationsAPI
|
||||||
object CommentArticlePaths {
|
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)
|
@Location("/citizens/{citizen}/comments/articles") class CitizenCommentArticleRequest(val citizen: Citizen)
|
||||||
}
|
}
|
||||||
|
|
||||||
@KtorExperimentalLocationsAPI
|
@KtorExperimentalLocationsAPI
|
||||||
fun Route.commentArticle(repo: CommentArticleRepository) {
|
fun Route.commentArticle(repo: CommentArticleRepository) {
|
||||||
get<CommentArticlePaths.ArticleCommentRequest> {
|
get<CommentArticlePaths.ArticleCommentRequest> {
|
||||||
val comment = repo.findByTarget(it.article)
|
val comment = repo.findByTarget(it.article, it.page, it.limit)
|
||||||
assertCan(VIEW, comment.result)
|
assertCan(VIEW, comment.result)
|
||||||
call.respond(HttpStatusCode.OK, comment)
|
call.respond(HttpStatusCode.OK, comment)
|
||||||
}
|
}
|
||||||
|
|
||||||
post<CommentArticlePaths.ArticleCommentRequest> {
|
post<CommentArticlePaths.ArticleCommentRequest> {
|
||||||
assertCan(CREATE, it.article)
|
val content = call.receive<CommentEntityRequest>().content
|
||||||
|
|
||||||
val content = call.receiveText()
|
|
||||||
val comment = CommentEntity(
|
val comment = CommentEntity(
|
||||||
target = it.article,
|
target = it.article,
|
||||||
createdBy = citizen,
|
createdBy = citizen,
|
||||||
content = content
|
content = content
|
||||||
)
|
)
|
||||||
|
|
||||||
|
assertCan(CREATE, comment)
|
||||||
repo.comment(comment)
|
repo.comment(comment)
|
||||||
|
|
||||||
call.respond(HttpStatusCode.Created, comment)
|
call.respond(HttpStatusCode.Created, comment)
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ begin
|
|||||||
and created_by_id = _created_by_id
|
and created_by_id = _created_by_id
|
||||||
|
|
||||||
order by
|
order by
|
||||||
created_at desc,
|
|
||||||
com.created_at desc
|
com.created_at desc
|
||||||
|
|
||||||
limit "limit" offset "offset"
|
limit "limit" offset "offset"
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ begin
|
|||||||
find_citizen_by_id(com.created_by_id) as created_by
|
find_citizen_by_id(com.created_by_id) as created_by
|
||||||
from "comment" as com
|
from "comment" as com
|
||||||
where parent_id = _parent_id
|
where parent_id = _parent_id
|
||||||
order by created_at desc,
|
order by created_at asc,
|
||||||
com.created_at desc
|
com.created_at desc
|
||||||
limit "limit" offset "offset"
|
limit "limit" offset "offset"
|
||||||
) as t;
|
) as t;
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ begin
|
|||||||
find_citizen_by_id(com.created_by_id) as created_by
|
find_citizen_by_id(com.created_by_id) as created_by
|
||||||
from "comment" as com
|
from "comment" as com
|
||||||
where com.parent_id = _target_id
|
where com.parent_id = _target_id
|
||||||
order by created_at desc,
|
order by created_at asc,
|
||||||
com.created_at desc
|
com.created_at desc
|
||||||
limit "limit" offset "offset"
|
limit "limit" offset "offset"
|
||||||
) as t;
|
) as t;
|
||||||
|
|||||||
Reference in New Issue
Block a user