@@ -417,11 +417,59 @@ fun Application.main() {
417417 delete(" /${EVENT_ENTITY_API_NAME } " ) {
418418 val roles = call.principal<WithRoles >()?.roles!!
419419 if (! roles.isAdmin) {
420- call.respond(HttpStatusCode .Unauthorized )
420+ call.respond(HttpStatusCode .Unauthorized , " Only user with Admin role can delete events " )
421421 return @delete
422- } else {
423- call.respond(HttpStatusCode .NotImplemented )
424- TODO (" To be implemented" )
422+ }
423+ var connEMD: Connection ? = null
424+ var deletedCount = 0
425+ try {
426+ // Here, we only care about reference to event, other event data is optional and is ignored, if passed
427+ val events = call.receive<Array <EventReprForDelete >>()
428+ connEMD = newEMDConnection(config, this .context)
429+ val storageMap = getStorageMap(connEMD!! )
430+ events.forEach { event ->
431+ println (" Deleting event: $event " )
432+ // val software_id = softwareMap.str_to_id[event.software_version]
433+ val file_path = event.reference.file_path
434+ val storage_name = event.reference.storage_name
435+ val storage_id = storageMap.str_to_id[storage_name]
436+
437+ val file_guid: Int
438+ val res = connEMD!! .createStatement().executeQuery(
439+ """ SELECT file_guid FROM file_ WHERE
440+ storage_id = $storage_id AND file_path = '$file_path '
441+ """ .trimMargin()
442+ )
443+ if (res.next()) {
444+ file_guid = res.getInt(" file_guid" )
445+ println (" File GUID = $file_guid " )
446+ } else { // no such file
447+ call.respond(HttpStatusCode .NotFound , " Error: file_guid not found for event" )
448+ return @delete
449+ }
450+ val query = """
451+ DELETE FROM ${page.db_table_name}
452+ WHERE (("file_guid" = $file_guid AND "event_number" = ${event.reference.event_number} ));
453+ """ .trimIndent()
454+ println (query)
455+ val intRes = connEMD!! .createStatement().executeUpdate(query)
456+ if (intRes == 1 ) {
457+ deletedCount++
458+ }
459+ }
460+ call.respond(HttpStatusCode .OK , " Overall $deletedCount events were deleted" )
461+ } catch (err: PSQLException ) {
462+ if (err.toString().contains(" The connection attempt failed." )) {
463+ call.respond(HttpStatusCode .ServiceUnavailable , " Database connection failed: $err " )
464+ } else {
465+ call.respond(HttpStatusCode .Conflict , " Database error: $err " )
466+ }
467+ } catch (err: BadRequestException ) {
468+ call.respond(HttpStatusCode .UnprocessableEntity , " Error processing content: $err " )
469+ } catch (err: Exception ) {
470+ call.respond(HttpStatusCode .InternalServerError , " Error deleting events: $err " )
471+ } finally {
472+ connEMD?.close()
425473 }
426474 }
427475
0 commit comments