GET comments of article is paginate
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user