Code cleanup
This commit is contained in:
@@ -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")
|
||||||
|
|||||||
@@ -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)
|
||||||
@@ -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}"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user