Code cleanup

This commit is contained in:
2025-08-17 14:00:34 -04:00
parent aab5857b94
commit 8111e7e41d
2 changed files with 21 additions and 12 deletions

View File

@@ -67,9 +67,6 @@ fun Application.configureLibraryRoutes(dbConnection: Connection) {
} catch (cause: DbElementInsertionException) { } catch (cause: DbElementInsertionException) {
log.error(cause.message) log.error(cause.message)
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to update Library.") 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) { } catch (cause: ContentTransformationException) {
log.error(cause.message) log.error(cause.message)
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments") call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments")

View File

@@ -10,7 +10,8 @@ import java.sql.*
class CollectionItemService(private val connection: Connection) { class CollectionItemService(private val connection: Connection) {
companion object { 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 SELECT_ITEM_BY_ID = "SELECT * FROM collection_item WHERE id = ?"
private const val INSERT_ITEM = "INSERT INTO collection_item (title, author, publisher, publishing_date, " + 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, " + "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 = ?, " + "publishing_date = ?, loc_number = ?, dewey_decimal_number = ?, isbn = ?, sort_title = ?, format = ?, " +
"language = ?, page_count = ?, categories = ?, description = ?, price_in_cents = ?, cover_image_uri = ?, " + "language = ?, page_count = ?, categories = ?, description = ?, price_in_cents = ?, cover_image_uri = ?, " +
"is_checked_in = ?, is_archived = ?, is_lost = ?, lost_date = ? WHERE id = ?" "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 // In the event books are "deleted" erroneously, having a flag set instead of actually removing the entry allows
// for quick reversal. // for quick reversal.
private const val ARCHIVE_ITEM_BY_ID = "UPDATE collection_item SET is_archived = true WHERE id = ?" private const val ARCHIVE_ITEM_BY_ID = "UPDATE collection_item SET is_archived = true WHERE id = ?"
} }
fun create(newCollectionItem: NewCollectionItem): Long { fun create(newCollectionItem: NewCollectionItem): Long {
val statement = connection.prepareStatement(INSERT_ITEM, val statement = connection.prepareStatement(
Statement.RETURN_GENERATED_KEYS) INSERT_ITEM,
Statement.RETURN_GENERATED_KEYS
)
statement.setString(1, newCollectionItem.title) statement.setString(1, newCollectionItem.title)
statement.setString(2, newCollectionItem.author) statement.setString(2, newCollectionItem.author)
statement.setString(3, newCollectionItem.publisher) statement.setString(3, newCollectionItem.publisher)
@@ -47,7 +51,7 @@ class CollectionItemService(private val connection: Connection) {
statement.setBoolean(17, newCollectionItem.isArchived) statement.setBoolean(17, newCollectionItem.isArchived)
statement.setBoolean(18, newCollectionItem.isLost) statement.setBoolean(18, newCollectionItem.isLost)
statement.setDate(19, Date.valueOf(newCollectionItem.lostDate)) statement.setDate(19, Date.valueOf(newCollectionItem.lostDate))
try{ try {
statement.execute() statement.execute()
val key = statement.generatedKeys val key = statement.generatedKeys
if (key.next()) { if (key.next()) {
@@ -55,8 +59,10 @@ class CollectionItemService(private val connection: Connection) {
} }
return -1 return -1
} catch (cause: SQLException) { } catch (cause: SQLException) {
throw DbElementInsertionException("Couldn't insert item " + throw DbElementInsertionException(
"${newCollectionItem.title} into database. ${cause.message}") "Couldn't insert item " +
"${newCollectionItem.title} into database. ${cause.message}"
)
} }
} }
@@ -91,7 +97,7 @@ class CollectionItemService(private val connection: Connection) {
} else { } else {
throw DbElementNotFoundException("Could not find collection item. resultSet: $resultSet") 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") 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) statement.setLong(20, collectionItem.id)
return statement.execute() return statement.execute()
} catch (e: SQLException) { } 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) { } 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
)
} }
} }