configured initial routes
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
package codes.kalar.routes
|
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.model.Patron
|
||||||
|
import codes.kalar.service.PatronService
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
import io.ktor.server.application.*
|
import io.ktor.server.application.*
|
||||||
import io.ktor.server.request.*
|
import io.ktor.server.request.*
|
||||||
@@ -9,32 +13,68 @@ import io.ktor.server.routing.*
|
|||||||
import java.sql.Connection
|
import java.sql.Connection
|
||||||
|
|
||||||
fun Application.configurePatronRoutes(dbConnection: Connection) {
|
fun Application.configurePatronRoutes(dbConnection: Connection) {
|
||||||
|
val patronService = PatronService(dbConnection)
|
||||||
|
|
||||||
routing {
|
routing {
|
||||||
get("/patron") {
|
get("/patron/{id}") {
|
||||||
if (call.request.queryParameters["patron"] == null) {
|
try {
|
||||||
call.respond(HttpStatusCode.BadRequest, "Invalid parameters")
|
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") {
|
post("/patron") {
|
||||||
val patron = call.receive<Patron>()
|
try {
|
||||||
call.respondText("${patron.name} is posted")
|
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") {
|
patch("/patron") {
|
||||||
|
try {
|
||||||
val patron = call.receive<Patron>()
|
val patron = call.receive<Patron>()
|
||||||
|
val patchedPatron = patronService.update(patron)
|
||||||
call.respondText("${patron.name} is patched")
|
call.respondText("${patron.name} is patched")
|
||||||
|
} catch (cause: DbElementInsertionException) {
|
||||||
|
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to update Patron.")
|
||||||
|
} catch (cause: ContentTransformationException) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
delete("/patron") {
|
delete("/patron/{id}") {
|
||||||
if (call.request.queryParameters["id"] == null) {
|
try {
|
||||||
call.respond(HttpStatusCode.BadRequest, "Invalid parameters")
|
val id = call.pathParameters["id"]!!.toLong()
|
||||||
} else {
|
patronService.delete(id)
|
||||||
call.respondText("Do you have permissions?")
|
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