configured initial routes
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package codes.kalar.routes
|
||||
|
||||
import codes.kalar.exception.DbElementInsertionException
|
||||
import codes.kalar.exception.DbElementNotFoundException
|
||||
import codes.kalar.model.NewPatron
|
||||
import codes.kalar.model.Patron
|
||||
import codes.kalar.service.PatronService
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.request.*
|
||||
@@ -9,32 +13,68 @@ import io.ktor.server.routing.*
|
||||
import java.sql.Connection
|
||||
|
||||
fun Application.configurePatronRoutes(dbConnection: Connection) {
|
||||
val patronService = PatronService(dbConnection)
|
||||
|
||||
routing {
|
||||
get("/patron") {
|
||||
if (call.request.queryParameters["patron"] == null) {
|
||||
call.respond(HttpStatusCode.BadRequest, "Invalid parameters")
|
||||
get("/patron/{id}") {
|
||||
try {
|
||||
val id = call.pathParameters["id"]!!.toLong()
|
||||
val patron = patronService.readPatronById(id)
|
||||
call.respond(HttpStatusCode.OK, patron)
|
||||
} catch (cause: DbElementInsertionException) {
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unknown error")
|
||||
} catch (cause: NumberFormatException) {
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unknown error")
|
||||
}
|
||||
else {
|
||||
call.respondText("Hello, ${call.request.queryParameters["patron"]}")
|
||||
}
|
||||
|
||||
get("/patron") {
|
||||
try {
|
||||
val username = call.parameters["username"]
|
||||
if (username == null) {
|
||||
call.respond(HttpStatusCode.BadRequest, "username & password required")
|
||||
} else {
|
||||
val patron = patronService.readPatronByLoginUsername(username)
|
||||
call.respond(HttpStatusCode.OK, patron)
|
||||
}
|
||||
} catch (cause: DbElementNotFoundException) {
|
||||
call.respond(HttpStatusCode.BadRequest, "Username not found")
|
||||
}
|
||||
}
|
||||
|
||||
post("/patron") {
|
||||
val patron = call.receive<Patron>()
|
||||
call.respondText("${patron.name} is posted")
|
||||
try {
|
||||
val patron = call.receive<NewPatron>()
|
||||
val id = patronService.create(patron)
|
||||
call.respondText("Adding ${patron.name} 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("/patron") {
|
||||
val patron = call.receive<Patron>()
|
||||
call.respondText("${patron.name} is patched")
|
||||
try {
|
||||
val patron = call.receive<Patron>()
|
||||
val patchedPatron = patronService.update(patron)
|
||||
call.respondText("${patron.name} is patched")
|
||||
} catch (cause: DbElementInsertionException) {
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to update Patron.")
|
||||
} catch (cause: ContentTransformationException) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
delete("/patron") {
|
||||
if (call.request.queryParameters["id"] == null) {
|
||||
call.respond(HttpStatusCode.BadRequest, "Invalid parameters")
|
||||
} else {
|
||||
call.respondText("Do you have permissions?")
|
||||
delete("/patron/{id}") {
|
||||
try {
|
||||
val id = call.pathParameters["id"]!!.toLong()
|
||||
patronService.delete(id)
|
||||
call.respond(HttpStatusCode.OK, "Successfully deleted the patron")
|
||||
} catch (cause: DbElementInsertionException) {
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to delete Patron.")
|
||||
} catch (cause: NumberFormatException) {
|
||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "ID needs to be a number.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user