Diploma-1 Main CRUD operations
This commit is contained in:
46
src/main/kotlin/modules/news/Controller.kt
Normal file
46
src/main/kotlin/modules/news/Controller.kt
Normal file
@@ -0,0 +1,46 @@
|
||||
package cc.essaenko.modules.news
|
||||
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
|
||||
fun Route.adminNewsRoutes(svc: NewsService) = route("/news") {
|
||||
post {
|
||||
val payload = call.receive<NewsCreate>()
|
||||
val id = svc.create(payload)
|
||||
call.respond(mapOf("id" to id))
|
||||
}
|
||||
put("{slug}") {
|
||||
val slug = call.parameters["slug"]!!
|
||||
val body = call.receive<Map<String, String>>()
|
||||
val ok = svc.update(slug, body["summary"].orEmpty(), body["content"].orEmpty())
|
||||
call.respond(mapOf("updated" to ok))
|
||||
}
|
||||
post("{slug}/publish") {
|
||||
val slug = call.parameters["slug"]!!
|
||||
val ok = svc.publish(slug)
|
||||
call.respond(mapOf("published" to ok))
|
||||
}
|
||||
delete("{slug}") {
|
||||
val slug = call.parameters["slug"]!!
|
||||
val ok = svc.delete(slug)
|
||||
call.respond(mapOf("deleted" to ok))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun Route.publicNewsRoutes(svc: NewsService) = route("/news") {
|
||||
get {
|
||||
val items = svc.list()
|
||||
call.respond(items.map {
|
||||
// можно вернуть Entity напрямую (оно сериализуемо, если поля простые),
|
||||
// но лучше собрать DTO — показываю минимально:
|
||||
mapOf("id" to it.id, "title" to it.title, "slug" to it.slug, "summary" to it.summary)
|
||||
})
|
||||
}
|
||||
get("{slug}") {
|
||||
val slug = call.parameters["slug"]!!
|
||||
val item = svc.get(slug)
|
||||
call.respond(item)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user