🗃️ Fixes database tables
This commit is contained in:
parent
cb6262392b
commit
ac0ac8dd98
@ -20,6 +20,6 @@ fun Application.configureDatabases(dotenv: Dotenv) {
|
|||||||
password = dbPassword
|
password = dbPassword
|
||||||
)
|
)
|
||||||
|
|
||||||
val actionService = ActionService()
|
ActionService.init()
|
||||||
val authService = AuthService()
|
AuthService.init()
|
||||||
}
|
}
|
||||||
|
@ -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,7 +55,8 @@ class ActionService {
|
|||||||
override val primaryKey = PrimaryKey(id)
|
override val primaryKey = PrimaryKey(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
companion object {
|
||||||
|
fun init() {
|
||||||
transaction {
|
transaction {
|
||||||
SchemaUtils.create(Actions)
|
SchemaUtils.create(Actions)
|
||||||
}
|
}
|
||||||
@ -65,10 +64,14 @@ class ActionService {
|
|||||||
|
|
||||||
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)
|
||||||
|
val authId = UUID.fromString(action.authId)
|
||||||
|
AuthService.read(authId) ?: throw Exception("auth not found")
|
||||||
|
|
||||||
Actions.insert {
|
Actions.insert {
|
||||||
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[auth] = authId
|
||||||
it[createdAt] = now
|
it[createdAt] = now
|
||||||
it[updatedAt] = now
|
it[updatedAt] = now
|
||||||
}[Actions.id]
|
}[Actions.id]
|
||||||
@ -82,12 +85,26 @@ class ActionService {
|
|||||||
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.updatedAt]
|
||||||
|
)
|
||||||
|
}.singleOrNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun readAll(): List<ActionDTO> = transaction {
|
||||||
|
Actions.selectAll().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.createdAt],
|
||||||
it[Actions.updatedAt]
|
it[Actions.updatedAt]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
.singleOrNull()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun update(id: UUID, action: NewActionDTO) {
|
fun update(id: UUID, action: NewActionDTO) {
|
||||||
@ -96,7 +113,7 @@ class ActionService {
|
|||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -107,4 +124,5 @@ class ActionService {
|
|||||||
Actions.deleteWhere { Actions.id eq id }
|
Actions.deleteWhere { Actions.id eq id }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,24 +30,24 @@ 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 {
|
||||||
|
fun init() {
|
||||||
transaction {
|
transaction {
|
||||||
SchemaUtils.create(Auths)
|
SchemaUtils.create(Auths)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun create(action: NewAuthDTO): UUID = transaction {
|
fun create(action: NewAuthDTO): UUID = transaction {
|
||||||
|
val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
|
||||||
Auths.insert {
|
Auths.insert {
|
||||||
it[name] = action.name
|
it[name] = action.name
|
||||||
it[pin] = sha256(action.pin)
|
it[pin] = sha256(action.pin)
|
||||||
it[key] = sha256(action.key)
|
it[key] = sha256(action.key)
|
||||||
it[createdAt] = ""
|
it[createdAt] = now
|
||||||
it[updatedAt] = ""
|
|
||||||
}[Auths.id]
|
}[Auths.id]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,16 +59,26 @@ class AuthService {
|
|||||||
it[Auths.id].toString(),
|
it[Auths.id].toString(),
|
||||||
it[Auths.name],
|
it[Auths.name],
|
||||||
it[Auths.createdAt],
|
it[Auths.createdAt],
|
||||||
it[Auths.updatedAt]
|
|
||||||
)
|
)
|
||||||
}.singleOrNull()
|
}.singleOrNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun readAll(): List<AuthDTO> = transaction {
|
||||||
|
Auths.selectAll().map {
|
||||||
|
AuthDTO(
|
||||||
|
it[Auths.id].toString(),
|
||||||
|
it[Auths.name],
|
||||||
|
it[Auths.createdAt],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun delete(id: UUID) {
|
fun delete(id: UUID) {
|
||||||
transaction {
|
transaction {
|
||||||
Auths.deleteWhere { Auths.id eq id }
|
Auths.deleteWhere { Auths.id eq id }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sha256(it: String): String {
|
fun sha256(it: String): String {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user