Added Staff authentication

This commit is contained in:
2025-08-25 15:50:42 -04:00
parent 46b81b91e7
commit 5231c8b690
4 changed files with 87 additions and 68 deletions

View File

@@ -42,7 +42,7 @@ fun Application.configureCollectionItemRoutes(dbConnection: Connection) {
} }
} }
authenticate("auth-jwt") { authenticate("staff") {
post("/items") { post("/items") {
try { try {
val item = call.receive<NewCollectionItem>() val item = call.receive<NewCollectionItem>()

View File

@@ -7,6 +7,7 @@ import codes.kalar.model.NewLibrary
import codes.kalar.service.LibraryService import codes.kalar.service.LibraryService
import io.ktor.http.* import io.ktor.http.*
import io.ktor.server.application.* import io.ktor.server.application.*
import io.ktor.server.auth.authenticate
import io.ktor.server.request.* 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.*
@@ -41,7 +42,10 @@ fun Application.configureLibraryRoutes(dbConnection: Connection) {
} 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: NumberFormatException) { } catch (cause: NumberFormatException) {
call.respond(HttpStatusCode.BadRequest, "Unable to parse number format. \"${call.pathParameters["id"]}\" is not a number.") call.respond(
HttpStatusCode.BadRequest,
"Unable to parse number format. \"${call.pathParameters["id"]}\" is not a number."
)
} }
} }
@@ -49,43 +53,44 @@ fun Application.configureLibraryRoutes(dbConnection: Connection) {
// TODO Add search for collection_it where itemID && libraryID // TODO Add search for collection_it where itemID && libraryID
} }
post("/libraries") { authenticate("staff") {
val library = call.receive<NewLibrary>() post("/libraries") {
try { val library = call.receive<NewLibrary>()
val id = libraryService.create(library) try {
call.respondText("${library.name} is posted with the ID: $id") val id = libraryService.create(library)
} catch (cause: DbElementInsertionException) { call.respondText("${library.name} is posted with the ID: $id")
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to insert Library.") } catch (cause: DbElementInsertionException) {
} call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to insert Library.")
} }
patch("/libraries") {
try {
val library = call.receive<Library>()
val patchedLibrary = libraryService.update(library)
call.respond(HttpStatusCode.OK, patchedLibrary)
} catch (cause: DbElementInsertionException) {
log.error(cause.message)
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to update Library.")
} catch (cause: ContentTransformationException) {
log.error(cause.message)
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments")
} }
} patch("/libraries") {
try {
val library = call.receive<Library>()
val patchedLibrary = libraryService.update(library)
call.respond(HttpStatusCode.OK, patchedLibrary)
} catch (cause: DbElementInsertionException) {
log.error(cause.message)
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to update Library.")
} catch (cause: ContentTransformationException) {
log.error(cause.message)
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments")
}
}
delete("/libraries") { delete("/libraries") {
try { try {
val id = call.parameters["id"]!!.toLong() val id = call.parameters["id"]!!.toLong()
log.info("Deleting item with id=$id") log.info("Deleting item with id=$id")
libraryService.delete(id) libraryService.delete(id)
call.respondText(":(", status = HttpStatusCode.OK) call.respondText(":(", status = HttpStatusCode.OK)
} catch (cause: DbElementNotFoundException) { } catch (cause: DbElementNotFoundException) {
log.error(cause.message, cause) log.error(cause.message, cause)
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments") call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments")
} catch (cause: NumberFormatException) { } catch (cause: NumberFormatException) {
log.error(cause.message, cause) log.error(cause.message, cause)
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Invalid ID format") call.respond(HttpStatusCode.BadRequest, cause.message ?: "Invalid ID format")
}
} }
} }
} }

View File

@@ -7,6 +7,7 @@ import codes.kalar.model.Patron
import codes.kalar.service.PatronService import codes.kalar.service.PatronService
import io.ktor.http.* import io.ktor.http.*
import io.ktor.server.application.* import io.ktor.server.application.*
import io.ktor.server.auth.authenticate
import io.ktor.server.request.* 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.*
@@ -42,39 +43,49 @@ fun Application.configurePatronRoutes(dbConnection: Connection) {
} }
} }
post("/patron") { authenticate("staff") {
try { post("/patron") {
val patron = call.receive<NewPatron>() try {
val id = patronService.create(patron) val patron = call.receive<NewPatron>()
call.respondText("Adding ${patron.name} to database with the id of $id", status = HttpStatusCode.OK) val id = patronService.create(patron)
} catch (cause: DbElementInsertionException) { call.respondText("Adding ${patron.name} to database with the id of $id", status = HttpStatusCode.OK)
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments") } catch (cause: DbElementInsertionException) {
} catch (cause: ContentTransformationException) { call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments")
call.respond(HttpStatusCode.BadRequest, "Bad Arguments. Must pass a valid CollectionItem object.") } catch (cause: ContentTransformationException) {
call.respond(HttpStatusCode.BadRequest, "Bad Arguments. Must pass a valid CollectionItem object.")
}
} }
} }
patch("/patron") { authenticate("general") {
try { patch("/patron") {
val patron = call.receive<Patron>() try {
val patchedPatron = patronService.update(patron) val patron = call.receive<Patron>()
call.respondText("${patron.name} is patched") val isPatched = patronService.update(patron)
} catch (cause: DbElementInsertionException) { if (isPatched) {
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to update Patron.") call.respond(HttpStatusCode.OK, "${patron.name} is patched")
} catch (cause: ContentTransformationException) { } else {
call.respond(HttpStatusCode.BadRequest, "${patron.name} is not patched")
}
} catch (cause: DbElementInsertionException) {
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to update Patron.")
} catch (cause: ContentTransformationException) {
call.respond(HttpStatusCode.BadRequest, "Bad Arguments. Must pass a valid Patron object.")
}
} }
} }
delete("/patron/{id}") { authenticate("staff") {
try { delete("/patron/{id}") {
val id = call.pathParameters["id"]!!.toLong() try {
patronService.delete(id) val id = call.pathParameters["id"]!!.toLong()
call.respond(HttpStatusCode.OK, "Successfully deleted the patron") patronService.delete(id)
} catch (cause: DbElementInsertionException) { call.respond(HttpStatusCode.OK, "Successfully deleted the patron")
call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to delete Patron.") } catch (cause: DbElementInsertionException) {
} catch (cause: NumberFormatException) { call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to delete Patron.")
call.respond(HttpStatusCode.BadRequest, cause.message ?: "ID needs to be a number.") } catch (cause: NumberFormatException) {
call.respond(HttpStatusCode.BadRequest, cause.message ?: "ID needs to be a number.")
}
} }
} }
} }

View File

@@ -1,6 +1,7 @@
package codes.kalar.routes package codes.kalar.routes
import io.ktor.server.application.* import io.ktor.server.application.*
import io.ktor.server.auth.authenticate
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
@@ -16,16 +17,18 @@ fun Application.configureStaffRoutes(dbConnection: Connection) {
call.respondText(call.parameters["id"]!!) call.respondText(call.parameters["id"]!!)
} }
post("/staff") { authenticate("staff") {
post("/staff") {
} }
patch("/staff") { patch("/staff") {
} }
delete("/staff/{id}") { delete("/staff/{id}") {
}
} }
} }
} }