Adds proper source/file handling and executing

This commit is contained in:
Daniel Svitan
2025-05-11 18:58:42 +02:00
parent beb37d92f6
commit 957e031073
7 changed files with 57 additions and 27 deletions
+34 -14
View File
@@ -1,4 +1,4 @@
package dev.svitan.schemas
package dev.svitan.services
import io.ktor.server.plugins.NotFoundException
import kotlinx.serialization.Serializable
@@ -49,7 +49,7 @@ class NewActionDTO(
@Serializable
class RunActionDTO(
val pin: String,
val key: String
val key: String
)
class ActionService {
@@ -72,19 +72,26 @@ class ActionService {
}
}
fun create(action: NewActionDTO): UUID = transaction {
val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
val authId = UUID.fromString(action.authId)
AuthService.read(authId) ?: throw NotFoundException("auth not found")
fun create(action: NewActionDTO): UUID {
val id = transaction {
val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
val authId = UUID.fromString(action.authId)
AuthService.read(authId) ?: throw NotFoundException("auth not found")
Actions.insert {
it[name] = action.name
it[kind] = ActionKind.from(action.kind).toString()
it[aSource] = action.source
it[auth] = authId
it[createdAt] = now
it[updatedAt] = now
}[Actions.id]
Actions.insert {
it[name] = action.name
it[kind] = ActionKind.from(action.kind).toString()
it[aSource] = if (action.kind == ActionKind.SCRIPT.toString()) "" else action.source
it[auth] = authId
it[createdAt] = now
it[updatedAt] = now
}[Actions.id]
}
if (action.kind == ActionKind.SCRIPT.toString()) {
ExecutorService.writeFile(id, action.source)
}
return id
}
fun read(id: UUID): ActionDTO? = transaction {
@@ -118,6 +125,8 @@ class ActionService {
}
fun update(id: UUID, action: NewActionDTO) {
val oldAction = read(id) ?: throw NotFoundException("action not found")
transaction {
val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
Actions.update({ Actions.id eq id }) {
@@ -127,12 +136,23 @@ class ActionService {
it[updatedAt] = now
}
}
if (action.kind == ActionKind.SCRIPT.toString()) {
ExecutorService.writeFile(id, action.source)
} else if (oldAction.kind == ActionKind.SCRIPT) {
ExecutorService.deleteFile(id)
}
}
fun delete(id: UUID) {
val action = read(id) ?: throw NotFoundException("action not found")
transaction {
Actions.deleteWhere { Actions.id eq id }
}
if (action.kind == ActionKind.SCRIPT) {
ExecutorService.deleteFile(id)
}
}
}
}