Files
antifed/backend/src/main/kotlin/routes/Auth.kt
T

46 lines
1.5 KiB
Kotlin

package dev.svitan.routes
import dev.svitan.services.AuthService
import dev.svitan.services.NewAuthDTO
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.Application
import io.ktor.server.auth.UserIdPrincipal
import io.ktor.server.auth.authentication
import io.ktor.server.auth.principal
import io.ktor.server.plugins.BadRequestException
import io.ktor.server.plugins.NotFoundException
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.*
import java.util.UUID
fun Application.routeAuth() {
routing {
authentication {
get("/auth") {
println("Hello ${call.principal<UserIdPrincipal>()?.name}")
call.respond(AuthService.readAll())
}
put("/auth") {
val auth = call.receive<NewAuthDTO>()
call.respond(HttpStatusCode.Created, AuthService.create(auth))
}
get("/auth/{id}") {
val idRaw = call.parameters["id"] ?: throw BadRequestException("Invalid id")
val id = UUID.fromString(idRaw)
val auth = AuthService.read(id) ?: throw NotFoundException()
call.respond(auth)
}
delete("/auth/{id}") {
val idRaw = call.parameters["id"] ?: throw BadRequestException("Invalid id")
val id = UUID.fromString(idRaw)
AuthService.delete(id)
call.respond(HttpStatusCode.NoContent)
}
}
}
}