🗃️ Fixes database tables

This commit is contained in:
Daniel Svitan 2025-05-11 17:32:06 +02:00
parent cb6262392b
commit ac0ac8dd98
3 changed files with 98 additions and 69 deletions

View File

@ -20,6 +20,6 @@ fun Application.configureDatabases(dotenv: Dotenv) {
password = dbPassword password = dbPassword
) )
val actionService = ActionService() ActionService.init()
val authService = AuthService() AuthService.init()
} }

View File

@ -9,8 +9,7 @@ import java.time.format.DateTimeFormatter
import java.util.UUID import java.util.UUID
enum class ActionKind { enum class ActionKind {
TEXT, TEXT, SCRIPT;
SCRIPT;
companion object { companion object {
fun from(it: String): ActionKind { fun from(it: String): ActionKind {
@ -33,15 +32,14 @@ class ActionDTO(
val name: String, val name: String,
val kind: ActionKind, val kind: ActionKind,
val source: String, val source: String,
val authId: String,
val createdAt: String, val createdAt: String,
val updatedAt: String val updatedAt: String
) )
@Serializable @Serializable
class NewActionDTO( class NewActionDTO(
val name: String, val name: String, val kind: String, val source: String, val authId: String
val kind: String,
val source: String
) )
class ActionService { class ActionService {
@ -49,7 +47,7 @@ class ActionService {
val id = uuid("id").autoGenerate() val id = uuid("id").autoGenerate()
val name = text("name").uniqueIndex() val name = text("name").uniqueIndex()
val kind = text("kind") val kind = text("kind")
val a_source = text("source") // `source` is another property val aSource = text("source") // `source` is another property
val auth = uuid("auth_id").references(AuthService.Auths.id) val auth = uuid("auth_id").references(AuthService.Auths.id)
val createdAt = text("created_at") val createdAt = text("created_at")
val updatedAt = text("updated_at") val updatedAt = text("updated_at")
@ -57,54 +55,74 @@ class ActionService {
override val primaryKey = PrimaryKey(id) override val primaryKey = PrimaryKey(id)
} }
init { companion object {
transaction { fun init() {
SchemaUtils.create(Actions) transaction {
SchemaUtils.create(Actions)
}
} }
}
fun create(action: NewActionDTO): UUID = transaction { fun create(action: NewActionDTO): UUID = transaction {
val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
Actions.insert { val authId = UUID.fromString(action.authId)
it[name] = action.name AuthService.read(authId) ?: throw Exception("auth not found")
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.insert {
Actions.selectAll() it[name] = action.name
.where { Actions.id eq id } it[kind] = ActionKind.from(action.kind).toString()
.map { it[aSource] = action.source
it[auth] = authId
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.aSource],
it[Actions.auth].toString(),
it[Actions.createdAt],
it[Actions.updatedAt]
)
}.singleOrNull()
}
fun readAll(): List<ActionDTO> = transaction {
Actions.selectAll().map {
ActionDTO( ActionDTO(
it[Actions.id].toString(), it[Actions.id].toString(),
it[Actions.name], it[Actions.name],
ActionKind.from(it[Actions.kind]), ActionKind.from(it[Actions.kind]),
it[Actions.a_source], it[Actions.aSource],
it[Actions.auth].toString(),
it[Actions.createdAt], it[Actions.createdAt],
it[Actions.updatedAt] it[Actions.updatedAt]
) )
} }
.singleOrNull() }
}
fun update(id: UUID, action: NewActionDTO) { fun update(id: UUID, action: NewActionDTO) {
transaction { transaction {
val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
Actions.update({ Actions.id eq id }) { Actions.update({ Actions.id eq id }) {
it[name] = action.name it[name] = action.name
it[kind] = ActionKind.from(action.kind).toString() it[kind] = ActionKind.from(action.kind).toString()
it[a_source] = action.source it[aSource] = action.source
it[updatedAt] = now it[updatedAt] = now
}
}
}
fun delete(id: UUID) {
transaction {
Actions.deleteWhere { Actions.id eq id }
} }
} }
} }
fun delete(id: UUID) {
transaction {
Actions.deleteWhere { Actions.id eq id }
}
}
} }

View File

@ -5,14 +5,15 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import java.security.MessageDigest import java.security.MessageDigest
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.UUID import java.util.UUID
@Serializable @Serializable
class AuthDTO( class AuthDTO(
val id: String, val id: String,
val name: String, val name: String,
val createdAt: String, val createdAt: String
val updatedAt: String
) )
@Serializable @Serializable
@ -29,43 +30,53 @@ class AuthService {
val pin = text("pin") val pin = text("pin")
val key = text("key") val key = text("key")
val createdAt = text("created_at") val createdAt = text("created_at")
val updatedAt = text("updated_at")
override val primaryKey = PrimaryKey(id) override val primaryKey = PrimaryKey(id)
} }
init { companion object {
transaction { fun init() {
SchemaUtils.create(Auths) transaction {
SchemaUtils.create(Auths)
}
} }
}
fun create(action: NewAuthDTO): UUID = transaction { fun create(action: NewAuthDTO): UUID = transaction {
Auths.insert { val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
it[name] = action.name Auths.insert {
it[pin] = sha256(action.pin) it[name] = action.name
it[key] = sha256(action.key) it[pin] = sha256(action.pin)
it[createdAt] = "" it[key] = sha256(action.key)
it[updatedAt] = "" it[createdAt] = now
}[Auths.id] }[Auths.id]
} }
fun read(id: UUID): AuthDTO? = transaction { fun read(id: UUID): AuthDTO? = transaction {
Auths.selectAll() Auths.selectAll()
.where { Auths.id eq id } .where { Auths.id eq id }
.map { .map {
AuthDTO(
it[Auths.id].toString(),
it[Auths.name],
it[Auths.createdAt],
)
}.singleOrNull()
}
fun readAll(): List<AuthDTO> = transaction {
Auths.selectAll().map {
AuthDTO( AuthDTO(
it[Auths.id].toString(), it[Auths.id].toString(),
it[Auths.name], it[Auths.name],
it[Auths.createdAt], it[Auths.createdAt],
it[Auths.updatedAt]
) )
}.singleOrNull() }
} }
fun delete(id: UUID) { fun delete(id: UUID) {
transaction { transaction {
Auths.deleteWhere { Auths.id eq id } Auths.deleteWhere { Auths.id eq id }
}
} }
} }
} }