From 27ff5ed85e1732e6a185cf92f0ecd6c9a2e0754b Mon Sep 17 00:00:00 2001 From: Nicholas Kalar Date: Fri, 15 Aug 2025 14:28:12 -0400 Subject: [PATCH] Created initial patch and delete and updated get endpoints --- src/main/kotlin/routes/LibraryRoutes.kt | 44 ++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/routes/LibraryRoutes.kt b/src/main/kotlin/routes/LibraryRoutes.kt index 5d9d8e0..520cca4 100644 --- a/src/main/kotlin/routes/LibraryRoutes.kt +++ b/src/main/kotlin/routes/LibraryRoutes.kt @@ -33,8 +33,20 @@ fun Application.configureLibraryRoutes(dbConnection: Connection) { } } + get("/libraries/{id}") { + try { + val id = call.pathParameters["id"]!!.toLong() + val library = libraryService.readLibraryById(id) + call.respond(HttpStatusCode.OK, library) + } catch (cause: DbElementNotFoundException) { + call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to find Library.") + } catch (cause: NumberFormatException) { + call.respond(HttpStatusCode.BadRequest, "Unable to parse number format. \"${call.pathParameters["id"]}\" is not a number.") + } + } + get("/libraries/{libraryId}/items/{itemId}") { - call.respondText("You asked for ${call.parameters["itemId"]} from ${call.parameters["libraryId"]}") + // TODO Add search for collection_it where itemID && libraryID } post("/libraries") { @@ -48,12 +60,36 @@ fun Application.configureLibraryRoutes(dbConnection: Connection) { } patch("/libraries") { - val library = call.receive() - call.respondText("${library.name} is patched") + try { + val library = call.receive() + val patchedLibrary = libraryService.update(library) + call.respond(HttpStatusCode.OK, patchedLibrary) + } catch (cause: DbElementInsertionException) { + log.error(cause.message) + call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to update Library.") + } 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("/libraries") { - call.respondText("We hate to see you go!") + try { + val id = call.parameters["id"]!!.toLong() + log.info("Deleting item with id=$id") + libraryService.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") + } } }