move routes installation into component

This commit is contained in:
2021-01-21 21:55:24 +01:00
parent 3ba4a195ba
commit 667339979b
27 changed files with 492 additions and 420 deletions

View File

@@ -18,30 +18,31 @@ import io.ktor.response.respond
import io.ktor.routing.Route
@KtorExperimentalLocationsAPI
@Location("/articles/{article}/comments")
class PostArticleCommentRequest(
val article: ArticleForView
) {
class Comment(
val content: String
)
suspend fun getComment(call: ApplicationCall) = call.receive<Comment>().run {
CommentForUpdate(
target = article,
createdBy = call.citizen,
content = content
object CreateCommentArticle {
@Location("/articles/{article}/comments")
class PostArticleCommentRequest(
val article: ArticleForView
) {
class Comment(
val content: String
)
}
}
@KtorExperimentalLocationsAPI
fun Route.createCommentArticle(repo: CommentArticleRepository, voter: CommentVoter) {
post<PostArticleCommentRequest> {
it.getComment(call).let { comment ->
voter.assert { canCreate(comment, citizenOrNull) }
repo.comment(comment)
call.respond(HttpStatusCode.Created, comment)
suspend fun getComment(call: ApplicationCall) = call.receive<Comment>().run {
CommentForUpdate(
target = article,
createdBy = call.citizen,
content = content
)
}
}
fun Route.createCommentArticle(repo: CommentArticleRepository, voter: CommentVoter) {
post<PostArticleCommentRequest> {
it.getComment(call).let { comment ->
voter.assert { canCreate(comment, citizenOrNull) }
repo.comment(comment)
call.respond(HttpStatusCode.Created, comment)
}
}
}
}