From a3259915d54f1d39cf6731e22140a4b917290d86 Mon Sep 17 00:00:00 2001 From: Nicholas Kalar Date: Sun, 17 Aug 2025 13:59:53 -0400 Subject: [PATCH] configured initial routes --- src/main/kotlin/routes/PatronRoutes.kt | 68 ++++++++++++++++++++------ 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/routes/PatronRoutes.kt b/src/main/kotlin/routes/PatronRoutes.kt index c0f95cb..ea533ea 100644 --- a/src/main/kotlin/routes/PatronRoutes.kt +++ b/src/main/kotlin/routes/PatronRoutes.kt @@ -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() - call.respondText("${patron.name} is posted") + try { + val patron = call.receive() + 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() - call.respondText("${patron.name} is patched") + try { + val patron = call.receive() + 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.") } } }