diff --git a/backend/src/main/kotlin/plugins/Databases.kt b/backend/src/main/kotlin/plugins/Databases.kt index 292c9b7..df8e040 100644 --- a/backend/src/main/kotlin/plugins/Databases.kt +++ b/backend/src/main/kotlin/plugins/Databases.kt @@ -20,6 +20,6 @@ fun Application.configureDatabases(dotenv: Dotenv) { password = dbPassword ) - val actionService = ActionService() - val authService = AuthService() + ActionService.init() + AuthService.init() } diff --git a/backend/src/main/kotlin/schemas/Action.kt b/backend/src/main/kotlin/schemas/Action.kt index 7ffd41c..968eab3 100644 --- a/backend/src/main/kotlin/schemas/Action.kt +++ b/backend/src/main/kotlin/schemas/Action.kt @@ -9,8 +9,7 @@ import java.time.format.DateTimeFormatter import java.util.UUID enum class ActionKind { - TEXT, - SCRIPT; + TEXT, SCRIPT; companion object { fun from(it: String): ActionKind { @@ -33,15 +32,14 @@ class ActionDTO( val name: String, val kind: ActionKind, val source: String, + val authId: String, val createdAt: String, val updatedAt: String ) @Serializable class NewActionDTO( - val name: String, - val kind: String, - val source: String + val name: String, val kind: String, val source: String, val authId: String ) class ActionService { @@ -49,7 +47,7 @@ class ActionService { val id = uuid("id").autoGenerate() val name = text("name").uniqueIndex() 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 createdAt = text("created_at") val updatedAt = text("updated_at") @@ -57,54 +55,74 @@ class ActionService { override val primaryKey = PrimaryKey(id) } - init { - transaction { - SchemaUtils.create(Actions) + companion object { + fun 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 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 Exception("auth not found") - fun read(id: UUID): ActionDTO? = transaction { - Actions.selectAll() - .where { Actions.id eq id } - .map { + 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] + } + + 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 = transaction { + Actions.selectAll().map { ActionDTO( it[Actions.id].toString(), it[Actions.name], ActionKind.from(it[Actions.kind]), - it[Actions.a_source], + it[Actions.aSource], + it[Actions.auth].toString(), 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 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[aSource] = action.source + 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 } - } - } } diff --git a/backend/src/main/kotlin/schemas/Auth.kt b/backend/src/main/kotlin/schemas/Auth.kt index 104b378..35b6e0d 100644 --- a/backend/src/main/kotlin/schemas/Auth.kt +++ b/backend/src/main/kotlin/schemas/Auth.kt @@ -5,14 +5,15 @@ import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.transactions.transaction import java.security.MessageDigest +import java.time.LocalDateTime +import java.time.format.DateTimeFormatter import java.util.UUID @Serializable class AuthDTO( val id: String, val name: String, - val createdAt: String, - val updatedAt: String + val createdAt: String ) @Serializable @@ -29,43 +30,53 @@ class AuthService { val pin = text("pin") val key = text("key") val createdAt = text("created_at") - val updatedAt = text("updated_at") override val primaryKey = PrimaryKey(id) } - init { - transaction { - SchemaUtils.create(Auths) + companion object { + fun init() { + transaction { + SchemaUtils.create(Auths) + } } - } - fun create(action: NewAuthDTO): UUID = transaction { - Auths.insert { - it[name] = action.name - it[pin] = sha256(action.pin) - it[key] = sha256(action.key) - it[createdAt] = "" - it[updatedAt] = "" - }[Auths.id] - } + fun create(action: NewAuthDTO): UUID = transaction { + val now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + Auths.insert { + it[name] = action.name + it[pin] = sha256(action.pin) + it[key] = sha256(action.key) + it[createdAt] = now + }[Auths.id] + } - fun read(id: UUID): AuthDTO? = transaction { - Auths.selectAll() - .where { Auths.id eq id } - .map { + fun read(id: UUID): AuthDTO? = transaction { + Auths.selectAll() + .where { Auths.id eq id } + .map { + AuthDTO( + it[Auths.id].toString(), + it[Auths.name], + it[Auths.createdAt], + ) + }.singleOrNull() + } + + fun readAll(): List = transaction { + Auths.selectAll().map { AuthDTO( it[Auths.id].toString(), it[Auths.name], it[Auths.createdAt], - it[Auths.updatedAt] ) - }.singleOrNull() - } + } + } - fun delete(id: UUID) { - transaction { - Auths.deleteWhere { Auths.id eq id } + fun delete(id: UUID) { + transaction { + Auths.deleteWhere { Auths.id eq id } + } } } }