mirror of
https://codeberg.org/selfsigned-ash/antifed
synced 2026-07-21 18:49:34 +02:00
✨ Adds proper source/file handling and executing
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user