From 8111e7e41d6d27b4ce4fe37aa1deec932d580aa6 Mon Sep 17 00:00:00 2001 From: Nicholas Kalar Date: Sun, 17 Aug 2025 14:00:34 -0400 Subject: [PATCH] Code cleanup --- src/main/kotlin/routes/LibraryRoutes.kt | 3 -- .../kotlin/service/CollectionItemService.kt | 30 +++++++++++++------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/routes/LibraryRoutes.kt b/src/main/kotlin/routes/LibraryRoutes.kt index 520cca4..6329a53 100644 --- a/src/main/kotlin/routes/LibraryRoutes.kt +++ b/src/main/kotlin/routes/LibraryRoutes.kt @@ -67,9 +67,6 @@ fun Application.configureLibraryRoutes(dbConnection: Connection) { } 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") diff --git a/src/main/kotlin/service/CollectionItemService.kt b/src/main/kotlin/service/CollectionItemService.kt index a9b0a95..bd32b71 100644 --- a/src/main/kotlin/service/CollectionItemService.kt +++ b/src/main/kotlin/service/CollectionItemService.kt @@ -10,7 +10,8 @@ import java.sql.* class CollectionItemService(private val connection: Connection) { companion object { - private const val SELECT_ITEM_BY_TITLE = "SELECT * FROM collection_item WHERE levenshtein(title, ?) <= 5 LIMIT 25 OFFSET ?" + private const val SELECT_ITEM_BY_TITLE = + "SELECT * FROM collection_item WHERE levenshtein(title, ?) <= 5 LIMIT 25 OFFSET ?" private const val SELECT_ITEM_BY_ID = "SELECT * FROM collection_item WHERE id = ?" private const val INSERT_ITEM = "INSERT INTO collection_item (title, author, publisher, publishing_date, " + "loc_number, dewey_decimal_number, isbn, sort_title, format, language, page_count, categories, " + @@ -20,14 +21,17 @@ class CollectionItemService(private val connection: Connection) { "publishing_date = ?, loc_number = ?, dewey_decimal_number = ?, isbn = ?, sort_title = ?, format = ?, " + "language = ?, page_count = ?, categories = ?, description = ?, price_in_cents = ?, cover_image_uri = ?, " + "is_checked_in = ?, is_archived = ?, is_lost = ?, lost_date = ? WHERE id = ?" + // In the event books are "deleted" erroneously, having a flag set instead of actually removing the entry allows // for quick reversal. private const val ARCHIVE_ITEM_BY_ID = "UPDATE collection_item SET is_archived = true WHERE id = ?" } fun create(newCollectionItem: NewCollectionItem): Long { - val statement = connection.prepareStatement(INSERT_ITEM, - Statement.RETURN_GENERATED_KEYS) + val statement = connection.prepareStatement( + INSERT_ITEM, + Statement.RETURN_GENERATED_KEYS + ) statement.setString(1, newCollectionItem.title) statement.setString(2, newCollectionItem.author) statement.setString(3, newCollectionItem.publisher) @@ -47,7 +51,7 @@ class CollectionItemService(private val connection: Connection) { statement.setBoolean(17, newCollectionItem.isArchived) statement.setBoolean(18, newCollectionItem.isLost) statement.setDate(19, Date.valueOf(newCollectionItem.lostDate)) - try{ + try { statement.execute() val key = statement.generatedKeys if (key.next()) { @@ -55,8 +59,10 @@ class CollectionItemService(private val connection: Connection) { } return -1 } catch (cause: SQLException) { - throw DbElementInsertionException("Couldn't insert item " + - "${newCollectionItem.title} into database. ${cause.message}") + throw DbElementInsertionException( + "Couldn't insert item " + + "${newCollectionItem.title} into database. ${cause.message}" + ) } } @@ -91,7 +97,7 @@ class CollectionItemService(private val connection: Connection) { } else { throw DbElementNotFoundException("Could not find collection item. resultSet: $resultSet") } - } catch(cause: SQLException) { + } catch (cause: SQLException) { throw DbElementNotFoundException("Could not find collection item with id $id") } } @@ -121,9 +127,15 @@ class CollectionItemService(private val connection: Connection) { statement.setLong(20, collectionItem.id) return statement.execute() } catch (e: SQLException) { - throw DbElementInsertionException("${e.message}\ncollectionItem: $collectionItem\n statement: $statement\n ", e) + throw DbElementInsertionException( + "${e.message}\ncollectionItem: $collectionItem\n statement: $statement\n ", + e + ) } catch (e: IllegalArgumentException) { - throw DbElementInsertionException("${e.message}\ncollectionItem: $collectionItem\n statement: $statement\n ", e) + throw DbElementInsertionException( + "${e.message}\ncollectionItem: $collectionItem\n statement: $statement\n ", + e + ) } }