added bulk library GET
This commit is contained in:
@@ -11,7 +11,6 @@ import io.ktor.server.request.*
|
|||||||
import io.ktor.server.response.*
|
import io.ktor.server.response.*
|
||||||
import io.ktor.server.routing.*
|
import io.ktor.server.routing.*
|
||||||
import java.sql.Connection
|
import java.sql.Connection
|
||||||
import java.sql.SQLException
|
|
||||||
|
|
||||||
fun Application.configureLibraryRoutes(dbConnection: Connection) {
|
fun Application.configureLibraryRoutes(dbConnection: Connection) {
|
||||||
val libraryService = LibraryService(dbConnection)
|
val libraryService = LibraryService(dbConnection)
|
||||||
@@ -19,9 +18,14 @@ fun Application.configureLibraryRoutes(dbConnection: Connection) {
|
|||||||
routing {
|
routing {
|
||||||
get("/libraries") {
|
get("/libraries") {
|
||||||
try {
|
try {
|
||||||
val id = call.parameters["id"]?.toLong() ?: throw IllegalArgumentException("query parameter required")
|
val id = call.parameters["id"]?.toLong()
|
||||||
val library = libraryService.read(id)
|
if (id != null) {
|
||||||
call.respond(HttpStatusCode.OK, library.toString())
|
val library = libraryService.readLibraryById(id)
|
||||||
|
call.respond(HttpStatusCode.OK, library)
|
||||||
|
} else {
|
||||||
|
val libraries = libraryService.readAllLibraries()
|
||||||
|
call.respond(HttpStatusCode.OK, libraries)
|
||||||
|
}
|
||||||
} catch (cause: DbElementNotFoundException) {
|
} catch (cause: DbElementNotFoundException) {
|
||||||
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to find Library.")
|
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to find Library.")
|
||||||
} catch (cause: IllegalArgumentException) {
|
} catch (cause: IllegalArgumentException) {
|
||||||
|
|||||||
@@ -4,15 +4,15 @@ import codes.kalar.exception.DbElementInsertionException
|
|||||||
import codes.kalar.exception.DbElementNotFoundException
|
import codes.kalar.exception.DbElementNotFoundException
|
||||||
import codes.kalar.model.Library
|
import codes.kalar.model.Library
|
||||||
import codes.kalar.model.NewLibrary
|
import codes.kalar.model.NewLibrary
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import java.sql.*
|
import java.sql.*
|
||||||
|
|
||||||
class LibraryService(private val connection: Connection) {
|
class LibraryService(private val connection: Connection) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val SELECT_LIBRARY_BY_ID = "SELECT * FROM library WHERE id = ?"
|
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 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 = ""
|
private const val UPDATE_LIBRARY_BY_ID = ""
|
||||||
// In the event are "deleted" erroneously, having a flag set instead of actually removing the entry allows
|
// In the event are "deleted" erroneously, having a flag set instead of actually removing the entry allows
|
||||||
// for quick reversal.
|
// for quick reversal.
|
||||||
@@ -23,6 +23,7 @@ class LibraryService(private val connection: Connection) {
|
|||||||
val statement = connection.prepareStatement(INSERT_LIBRARY, Statement.RETURN_GENERATED_KEYS)
|
val statement = connection.prepareStatement(INSERT_LIBRARY, Statement.RETURN_GENERATED_KEYS)
|
||||||
statement.setString(1, library.name)
|
statement.setString(1, library.name)
|
||||||
statement.setString(2, library.address)
|
statement.setString(2, library.address)
|
||||||
|
statement.setBoolean(3, library.isArchived)
|
||||||
try {
|
try {
|
||||||
statement.execute()
|
statement.execute()
|
||||||
val key = statement.generatedKeys
|
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)
|
val statement = connection.prepareStatement(SELECT_LIBRARY_BY_ID)
|
||||||
statement.setLong(1, id)
|
statement.setLong(1, id)
|
||||||
try {
|
try {
|
||||||
val resultSet = statement.executeQuery()
|
val resultSet = statement.executeQuery()
|
||||||
if (resultSet.next() && resultSet.getBoolean("is_archived")) {
|
if (resultSet.next() && !resultSet.getBoolean("is_archived")) {
|
||||||
return createLibraryFromResult(resultSet)
|
return createLibraryFromResult(resultSet)
|
||||||
} else {
|
} else {
|
||||||
throw DbElementNotFoundException("Could not find collection item. resultSet: $resultSet")
|
throw DbElementNotFoundException("Could not find collection item. resultSet: $resultSet")
|
||||||
@@ -50,6 +51,19 @@ class LibraryService(private val connection: Connection) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun readAllLibraries(): List<Library> {
|
||||||
|
val libraries = ArrayList<Library>()
|
||||||
|
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 update(library: Library) {}
|
||||||
|
|
||||||
suspend fun delete(id: String) {}
|
suspend fun delete(id: String) {}
|
||||||
|
|||||||
Reference in New Issue
Block a user