Skip to content

Commit 3bbd31a

Browse files
committed
single transaction for POST and DELETE for events
1 parent 7e3a156 commit 3bbd31a

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

src/jvmMain/kotlin/Application.kt

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ fun Application.main() {
322322
} catch (err: BadRequestException) {
323323
call.respond(HttpStatusCode.UnprocessableEntity, "Error processing content: $err")
324324
} catch (err: Exception) {
325-
call.respond(HttpStatusCode.InternalServerError, "Error obtaining software table data: $err")
325+
call.respond(HttpStatusCode.InternalServerError, "Error obtaining event data: $err")
326326
} finally {
327327
connEMD?.close()
328328
}
@@ -341,6 +341,7 @@ fun Application.main() {
341341
try {
342342
val events = call.receive<Array<EventRepr>>()
343343
connEMD = newEMDConnection(config, this.context)
344+
connEMD!!.autoCommit = false
344345
val softwareMap = getSoftwareMap(connEMD!!)
345346
val storageMap = getStorageMap(connEMD!!)
346347
events.forEach { event ->
@@ -350,37 +351,23 @@ fun Application.main() {
350351
val storage_name = event.reference.storage_name
351352
val storage_id = storageMap.str_to_id[storage_name]
352353

353-
// get file_guid
354354
val file_guid: Int
355355
val res = connEMD!!.createStatement().executeQuery(
356-
"""SELECT file_guid FROM file_ WHERE
357-
storage_id = $storage_id AND file_path = '$file_path'
358-
""".trimMargin()
356+
"""SELECT file_guid FROM file_ WHERE storage_id = $storage_id AND file_path = '$file_path'"""
359357
)
360-
if (res.next()) {
358+
if (res.next()) { // file record already exists
361359
file_guid = res.getInt("file_guid")
362-
println("File GUID = $file_guid")
363-
} else {
364-
// create file
365-
val fileQuery = """
366-
INSERT INTO file_ (storage_id, file_path)
367-
VALUES ($storage_id, '$file_path')
368-
""".trimIndent()
360+
} else { // create file record
361+
val fileQuery = """INSERT INTO file_ (storage_id, file_path) VALUES ($storage_id, '$file_path')"""
369362
println(fileQuery)
370363
connEMD!!.createStatement().executeUpdate(fileQuery)
371-
// TODO remove duplicate code here...
372364
val res2 = connEMD!!.createStatement().executeQuery(
373-
"""SELECT file_guid FROM file_ WHERE
374-
storage_id = $storage_id AND file_path = '$file_path'
375-
""".trimMargin()
365+
"""SELECT file_guid FROM file_ WHERE storage_id = $storage_id AND file_path = '$file_path'"""
376366
)
377-
if (res2.next()) {
378-
file_guid = res2.getInt("file_guid")
379-
println("File GUID = $file_guid")
380-
} else {
381-
throw java.lang.Exception("File guid writing issue... ")
382-
}
367+
res2.next()
368+
file_guid = res2.getInt("file_guid")
383369
}
370+
println("File GUID = $file_guid")
384371
val parameterValuesStr =
385372
page.parameters.joinToString(", ") {
386373
when (it.type.uppercase()) {
@@ -398,7 +385,8 @@ fun Application.main() {
398385
println(query)
399386
connEMD!!.createStatement().executeUpdate(query)
400387
}
401-
call.respond(HttpStatusCode.OK, "Events were created")
388+
connEMD!!.commit()
389+
call.respond(HttpStatusCode.OK, "Success: ${events.size} event(s) were created")
402390
} catch (err: PSQLException) {
403391
if (err.toString().contains("The connection attempt failed.")) {
404392
call.respond(HttpStatusCode.ServiceUnavailable, "Database connection failed: $err")
@@ -408,12 +396,16 @@ fun Application.main() {
408396
} catch (err: BadRequestException) {
409397
call.respond(HttpStatusCode.UnprocessableEntity, "Error processing content: $err")
410398
} catch (err: Exception) {
411-
call.respond(HttpStatusCode.InternalServerError, "Error obtaining software table data: $err")
399+
call.respond(HttpStatusCode.InternalServerError, "Error writing event data: $err")
412400
} finally {
413401
connEMD?.close()
414402
}
415403
}
416404

405+
put("/${EVENT_ENTITY_API_NAME}") {
406+
TODO()
407+
}
408+
417409
delete("/${EVENT_ENTITY_API_NAME}") {
418410
val roles = call.principal<WithRoles>()?.roles!!
419411
if (!roles.isAdmin) {
@@ -426,25 +418,26 @@ fun Application.main() {
426418
// Here, we only care about reference to event, other event data is optional and is ignored, if passed
427419
val events = call.receive<Array<EventReprForDelete>>()
428420
connEMD = newEMDConnection(config, this.context)
421+
connEMD!!.autoCommit = false
429422
val storageMap = getStorageMap(connEMD!!)
430423
events.forEach { event ->
431424
println("Deleting event: $event")
432-
// val software_id = softwareMap.str_to_id[event.software_version]
433425
val file_path = event.reference.file_path
434426
val storage_name = event.reference.storage_name
435427
val storage_id = storageMap.str_to_id[storage_name]
436428

437429
val file_guid: Int
438430
val res = connEMD!!.createStatement().executeQuery(
439-
"""SELECT file_guid FROM file_ WHERE
440-
storage_id = $storage_id AND file_path = '$file_path'
441-
""".trimMargin()
431+
"""SELECT file_guid FROM file_ WHERE storage_id = $storage_id AND file_path = '$file_path'"""
442432
)
443433
if (res.next()) {
444434
file_guid = res.getInt("file_guid")
445435
println("File GUID = $file_guid")
446436
} else { // no such file
447-
call.respond(HttpStatusCode.NotFound, "Error: file_guid not found for event")
437+
call.respond(
438+
HttpStatusCode.NotFound,
439+
"Error: file_guid not found for event ${event.str()}"
440+
)
448441
return@delete
449442
}
450443
val query = """
@@ -455,9 +448,14 @@ fun Application.main() {
455448
val intRes = connEMD!!.createStatement().executeUpdate(query)
456449
if (intRes == 1) {
457450
deletedCount++
451+
} else {
452+
call.respond(HttpStatusCode.NotFound,
453+
"Error: event (${event.str()}) not found")
454+
return@delete
458455
}
459456
}
460-
call.respond(HttpStatusCode.OK, "Overall $deletedCount events were deleted")
457+
connEMD!!.commit()
458+
call.respond(HttpStatusCode.OK, "Success: $deletedCount event(s) were deleted")
461459
} catch (err: PSQLException) {
462460
if (err.toString().contains("The connection attempt failed.")) {
463461
call.respond(HttpStatusCode.ServiceUnavailable, "Database connection failed: $err")

src/jvmMain/kotlin/models.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ data class EventReprForDelete(
2020
val period_number: Short? = 0,
2121
val run_number: Int? = 0,
2222
val parameters: Map<String, Any>?
23-
)
23+
)
24+
25+
fun EventReprForDelete.str(): String =
26+
"(${this.reference.storage_name}, ${this.reference.file_path}, ${this.reference.event_number})"

0 commit comments

Comments
 (0)