⚡ Changes action update dto to partial
This commit is contained in:
parent
957e031073
commit
e4e2312595
@ -7,6 +7,7 @@ import dev.svitan.services.AuthService
|
||||
import dev.svitan.services.ExecutorService
|
||||
import dev.svitan.services.NewActionDTO
|
||||
import dev.svitan.services.RunActionDTO
|
||||
import dev.svitan.services.UpdateActionDTO
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.auth.*
|
||||
@ -59,7 +60,7 @@ fun Application.routeAction() {
|
||||
patch("/action/{id}") {
|
||||
val idRaw = call.parameters["id"] ?: throw BadRequestException("Invalid id")
|
||||
val id = UUID.fromString(idRaw)
|
||||
val action = call.receive<NewActionDTO>()
|
||||
val action = call.receive<UpdateActionDTO>()
|
||||
call.respond(ActionService.update(id, action))
|
||||
}
|
||||
|
||||
|
@ -46,6 +46,13 @@ class NewActionDTO(
|
||||
val authId: String
|
||||
)
|
||||
|
||||
@Serializable
|
||||
class UpdateActionDTO(
|
||||
val name: String?,
|
||||
val kind: String?,
|
||||
val source: String?
|
||||
)
|
||||
|
||||
@Serializable
|
||||
class RunActionDTO(
|
||||
val pin: String,
|
||||
@ -124,22 +131,22 @@ class ActionService {
|
||||
}
|
||||
}
|
||||
|
||||
fun update(id: UUID, action: NewActionDTO) {
|
||||
fun update(id: UUID, action: UpdateActionDTO) {
|
||||
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 }) {
|
||||
it[name] = action.name
|
||||
it[kind] = ActionKind.from(action.kind).toString()
|
||||
it[aSource] = action.source
|
||||
if (action.name != null) it[name] = action.name
|
||||
if (action.kind != null) it[kind] = ActionKind.from(action.kind).toString()
|
||||
if (action.source != null) it[aSource] = action.source
|
||||
it[updatedAt] = now
|
||||
}
|
||||
}
|
||||
|
||||
if (action.kind == ActionKind.SCRIPT.toString()) {
|
||||
if (action.kind == ActionKind.SCRIPT.toString() && action.source != null) {
|
||||
ExecutorService.writeFile(id, action.source)
|
||||
} else if (oldAction.kind == ActionKind.SCRIPT) {
|
||||
} else if (action.kind == ActionKind.TEXT.toString() && oldAction.kind == ActionKind.SCRIPT) {
|
||||
ExecutorService.deleteFile(id)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user