From 6ec38940251e4780e439e7e6c8da2f198da7f938 Mon Sep 17 00:00:00 2001 From: Nicholas Kalar Date: Wed, 13 Aug 2025 15:12:10 -0400 Subject: [PATCH] added bulk library GET --- src/main/kotlin/routes/LibraryRoutes.kt | 12 ++++++++---- src/main/kotlin/service/LibraryService.kt | 22 ++++++++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/routes/LibraryRoutes.kt b/src/main/kotlin/routes/LibraryRoutes.kt index 8322d09..5d9d8e0 100644 --- a/src/main/kotlin/routes/LibraryRoutes.kt +++ b/src/main/kotlin/routes/LibraryRoutes.kt @@ -11,7 +11,6 @@ import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* import java.sql.Connection -import java.sql.SQLException fun Application.configureLibraryRoutes(dbConnection: Connection) { val libraryService = LibraryService(dbConnection) @@ -19,9 +18,14 @@ fun Application.configureLibraryRoutes(dbConnection: Connection) { routing { get("/libraries") { try { - val id = call.parameters["id"]?.toLong() ?: throw IllegalArgumentException("query parameter required") - val library = libraryService.read(id) - call.respond(HttpStatusCode.OK, library.toString()) + val id = call.parameters["id"]?.toLong() + if (id != null) { + val library = libraryService.readLibraryById(id) + call.respond(HttpStatusCode.OK, library) + } else { + val libraries = libraryService.readAllLibraries() + call.respond(HttpStatusCode.OK, libraries) + } } catch (cause: DbElementNotFoundException) { call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to find Library.") } catch (cause: IllegalArgumentException) { diff --git a/src/main/kotlin/service/LibraryService.kt b/src/main/kotlin/service/LibraryService.kt index 922090c..59cda37 100644 --- a/src/main/kotlin/service/LibraryService.kt +++ b/src/main/kotlin/service/LibraryService.kt @@ -4,15 +4,15 @@ import codes.kalar.exception.DbElementInsertionException import codes.kalar.exception.DbElementNotFoundException import codes.kalar.model.Library import codes.kalar.model.NewLibrary -import kotlinx.serialization.json.Json import java.sql.* class LibraryService(private val connection: Connection) { companion object { private const val SELECT_LIBRARY_BY_ID = "SELECT * FROM library WHERE id = ?" + private const val SELECT_ALL_LIBRARIES = "SELECT * FROM library" private const val SELECT_LIBRARY_ALL = "SELECT * FROM library ORDER BY id DESC LIMIT 25" - private const val INSERT_LIBRARY = "INSERT INTO library (name, address) VALUES (?, ?)" + private const val INSERT_LIBRARY = "INSERT INTO library (name, address, is_archived) VALUES (?, ?, ?)" private const val UPDATE_LIBRARY_BY_ID = "" // In the event are "deleted" erroneously, having a flag set instead of actually removing the entry allows // for quick reversal. @@ -23,6 +23,7 @@ class LibraryService(private val connection: Connection) { val statement = connection.prepareStatement(INSERT_LIBRARY, Statement.RETURN_GENERATED_KEYS) statement.setString(1, library.name) statement.setString(2, library.address) + statement.setBoolean(3, library.isArchived) try { statement.execute() val key = statement.generatedKeys @@ -35,12 +36,12 @@ class LibraryService(private val connection: Connection) { } } - suspend fun read(id: Long): Library { + suspend fun readLibraryById(id: Long): Library { val statement = connection.prepareStatement(SELECT_LIBRARY_BY_ID) statement.setLong(1, id) try { val resultSet = statement.executeQuery() - if (resultSet.next() && resultSet.getBoolean("is_archived")) { + if (resultSet.next() && !resultSet.getBoolean("is_archived")) { return createLibraryFromResult(resultSet) } else { throw DbElementNotFoundException("Could not find collection item. resultSet: $resultSet") @@ -50,6 +51,19 @@ class LibraryService(private val connection: Connection) { } } + fun readAllLibraries(): List { + val libraries = ArrayList() + val statement = connection.prepareStatement(SELECT_ALL_LIBRARIES) + val resultSet = statement.executeQuery() + while (resultSet.next()) { + if (!resultSet.getBoolean("is_archived")) { + val library = createLibraryFromResult(resultSet) + libraries.add(library) + } + } + return libraries + } + suspend fun update(library: Library) {} suspend fun delete(id: String) {}