Diploma-1 Main CRUD operations

This commit is contained in:
Evgenii Saenko
2025-10-16 17:30:03 -07:00
parent c470f766e0
commit 2d5b329b36
38 changed files with 1354 additions and 213 deletions

View 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)
}
}