🗃️ Adds action table

This commit is contained in:
Daniel Svitan 2025-05-11 16:09:45 +02:00
parent aa3e82010f
commit 04e0c8176b
2 changed files with 117 additions and 2 deletions

View File

@ -1,5 +1,3 @@
version: "3.3"
services:
postgres:
image: postgres:17
@ -10,5 +8,11 @@ services:
POSTGRES_PASSWORD: "password"
POSTGRES_DB: "db"
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
pgdata:

View File

@ -0,0 +1,111 @@
package dev.svitan.schemas
import kotlinx.serialization.Serializable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.UUID
enum class ActionKind {
TEXT,
SCRIPT;
companion object {
fun from(it: String): ActionKind {
return when (it.lowercase()) {
"text" -> ActionKind.TEXT
"script" -> ActionKind.SCRIPT
else -> throw IllegalArgumentException("Invalid action kind: $it")
}
}
}
override fun toString(): String {
return super.toString().lowercase()
}
}
@Serializable
class ActionDTO(
val id: String,
val name: String,
val kind: ActionKind,
val source: String,
val createdAt: String,
val updatedAt: String
)
@Serializable
class NewActionDTO(
val name: String,
val kind: String,
val source: String
)
class ActionService {
object Actions : Table("actions") {
val id = uuid("id").autoGenerate()
val name = text("name")
val kind = text("kind")
val a_source = text("source") // `source` is another property
val createdAt = text("created_at")
val updatedAt = text("updated_at")
override val primaryKey = PrimaryKey(id)
}
init {
transaction {
SchemaUtils.create(Actions)
}
}
fun create(action: NewActionDTO): UUID =
transaction {
val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
Actions.insert {
it[name] = action.name
it[kind] = ActionKind.from(action.kind).toString()
it[a_source] = action.source
it[createdAt] = now
it[updatedAt] = now
}[Actions.id]
}
fun read(id: UUID): ActionDTO? =
transaction {
Actions.selectAll()
.where { Actions.id eq id }
.map {
ActionDTO(
it[Actions.id].toString(),
it[Actions.name],
ActionKind.from(it[Actions.kind]),
it[Actions.a_source],
it[Actions.createdAt],
it[Actions.updatedAt]
)
}
.singleOrNull()
}
fun update(id: UUID, action: NewActionDTO) {
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[a_source] = action.source
it[updatedAt] = now
}
}
}
fun delete(id: UUID) {
transaction {
Actions.deleteWhere { Actions.id eq id }
}
}
}