FIRST AUTHENTICATED ROUTES!
This commit is contained in:
@@ -7,6 +7,7 @@ import codes.kalar.exception.DbElementNotFoundException
|
||||
import codes.kalar.model.NewCollectionItem
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.auth.authenticate
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
@@ -41,49 +42,51 @@ fun Application.configureCollectionItemRoutes(dbConnection: Connection) {
|
||||
}
|
||||
}
|
||||
|
||||
post("/items") {
|
||||
try {
|
||||
val item = call.receive<NewCollectionItem>()
|
||||
val id = itemService.create(item)
|
||||
call.respondText("Adding ${item.title} to database with the id of $id", status=HttpStatusCode.OK)
|
||||
} catch (cause: DbElementInsertionException) {
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments")
|
||||
} catch (cause: ContentTransformationException) {
|
||||
call.respond(HttpStatusCode.BadRequest, "Bad Arguments. Must pass a valid CollectionItem object.")
|
||||
authenticate("auth-jwt") {
|
||||
post("/items") {
|
||||
try {
|
||||
val item = call.receive<NewCollectionItem>()
|
||||
val id = itemService.create(item)
|
||||
call.respondText("Adding ${item.title} to database with the id of $id", status = HttpStatusCode.OK)
|
||||
} catch (cause: DbElementInsertionException) {
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments")
|
||||
} catch (cause: ContentTransformationException) {
|
||||
call.respond(HttpStatusCode.BadRequest, "Bad Arguments. Must pass a valid CollectionItem object.")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
patch("/items") {
|
||||
try {
|
||||
val inputItem = call.receive<CollectionItem>()
|
||||
itemService.readById(inputItem.id)
|
||||
itemService.update(inputItem)
|
||||
call.respondText("Updated ${inputItem.title} to database.", status=HttpStatusCode.OK)
|
||||
} catch (cause: DbElementNotFoundException) {
|
||||
log.error(cause.message)
|
||||
call.respond(HttpStatusCode.NotFound, cause.message ?: "Could not find item in database.")
|
||||
} catch (cause: DbElementInsertionException) {
|
||||
log.error(cause.message)
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments")
|
||||
} catch (cause: ContentTransformationException) {
|
||||
log.error(cause.message)
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments")
|
||||
patch("/items") {
|
||||
try {
|
||||
val inputItem = call.receive<CollectionItem>()
|
||||
itemService.readById(inputItem.id)
|
||||
itemService.update(inputItem)
|
||||
call.respondText("Updated ${inputItem.title} to database.", status = HttpStatusCode.OK)
|
||||
} catch (cause: DbElementNotFoundException) {
|
||||
log.error(cause.message)
|
||||
call.respond(HttpStatusCode.NotFound, cause.message ?: "Could not find item in database.")
|
||||
} catch (cause: DbElementInsertionException) {
|
||||
log.error(cause.message)
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments")
|
||||
} catch (cause: ContentTransformationException) {
|
||||
log.error(cause.message)
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete("/items/{id}") {
|
||||
try {
|
||||
val id = call.parameters["id"]!!.toLong()
|
||||
log.info("Deleting item with id=$id")
|
||||
itemService.delete(id)
|
||||
call.respondText(":(", status = HttpStatusCode.OK)
|
||||
} catch (cause: DbElementNotFoundException) {
|
||||
log.error(cause.message, cause)
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments")
|
||||
} catch (cause: NumberFormatException) {
|
||||
log.error(cause.message, cause)
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Invalid ID format")
|
||||
delete("/items/{id}") {
|
||||
try {
|
||||
val id = call.parameters["id"]!!.toLong()
|
||||
log.info("Deleting item with id=$id")
|
||||
itemService.delete(id)
|
||||
call.respondText(":(", status = HttpStatusCode.OK)
|
||||
} catch (cause: DbElementNotFoundException) {
|
||||
log.error(cause.message, cause)
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments")
|
||||
} catch (cause: NumberFormatException) {
|
||||
log.error(cause.message, cause)
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Invalid ID format")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user