From ca78bad6f38b492f01e85420fbf21c839cb25e7f Mon Sep 17 00:00:00 2001 From: Nicholas Kalar Date: Sun, 10 Aug 2025 16:22:17 -0400 Subject: [PATCH] Initial Item Routes --- .../kotlin/routes/CollectionItemRoutes.kt | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/main/kotlin/routes/CollectionItemRoutes.kt diff --git a/src/main/kotlin/routes/CollectionItemRoutes.kt b/src/main/kotlin/routes/CollectionItemRoutes.kt new file mode 100644 index 0000000..f92c8e0 --- /dev/null +++ b/src/main/kotlin/routes/CollectionItemRoutes.kt @@ -0,0 +1,90 @@ +package codes.kalar.routes + +import codes.kalar.exception.DbElementInsertionException +import codes.kalar.model.CollectionItem +import codes.kalar.service.CollectionItemService +import codes.kalar.exception.DbElementNotFoundException +import codes.kalar.model.NewCollectionItem +import io.ktor.http.* +import io.ktor.server.application.* +import io.ktor.server.request.* +import io.ktor.server.response.* +import io.ktor.server.routing.* +import java.sql.Connection + +fun Application.configureCollectionItemRoutes(dbConnection: Connection) { + val itemService = CollectionItemService(dbConnection) + + routing { + get("/items") { + try { + val input: Any + val item: CollectionItem + val items: MutableList + val offset: Int + if (call.request.queryParameters["id"] != null) { + input = call.request.queryParameters["id"]!!.toLong() + item = itemService.readById(input) + call.respond(item) + } else if (call.request.queryParameters["title"] != null) { + input = call.request.queryParameters["title"]!!.replace("-", " ") + offset = call.request.queryParameters["offset"]?.toInt() ?: 0 + items = itemService.readByTitle(input, offset) + call.respond(items) + } + else { + throw IllegalArgumentException("query parameter required") + } + } catch(cause: DbElementNotFoundException) { + call.respond(HttpStatusCode.NotFound, cause.message ?: "No element found in database.") + } catch(cause: IllegalArgumentException) { + call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments") + } + } + + post("/items") { + try { + val item = call.receive() + val id = itemService.create(item) + call.respondText("Adding ${item.title} to database with the id of $id") + } catch (e: DbElementInsertionException) { + call.respond(HttpStatusCode.BadRequest, e.message ?: "Bad Arguments") + } catch (e: ContentTransformationException) { + call.respond(HttpStatusCode.BadRequest, "Bad Arguments. Must pass a valid CollectionItem object.") + } + + } + + patch("/items") { + try { + val inputItem = call.receive() + itemService.readById(inputItem.id) + val result = itemService.update(inputItem) + call.respondText("Updated ${inputItem.title} to database: $result") + } catch (cause: DbElementNotFoundException) { + log.error(cause.message) + call.respond(HttpStatusCode.NotFound, "${cause.message}") + } + catch (cause: DbElementInsertionException) { + log.error(cause.message) + call.respond(HttpStatusCode.BadRequest, "${cause.message}") + } catch (cause: ContentTransformationException) { + log.error(cause.message) + call.respond(HttpStatusCode.BadRequest, "${cause.message}") + } + } + + delete("/items/{id}") { + try { + val id = call.parameters["id"]!!.toLong() + log.info("Deleting item with id=$id") + itemService.delete(id) + call.respondText(":(") + } catch (cause: DbElementNotFoundException) { + log.error(cause.message, cause) + } catch (cause: NumberFormatException) { + log.error(cause.message, cause) + } + } + } +} \ No newline at end of file