🗃️ Adds auth table
This commit is contained in:
parent
04e0c8176b
commit
cb6262392b
@ -1,14 +1,10 @@
|
||||
package dev.svitan.plugins
|
||||
|
||||
import dev.svitan.schemas.UserDTO
|
||||
import dev.svitan.schemas.UserService
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import dev.svitan.schemas.ActionService
|
||||
import dev.svitan.schemas.AuthService
|
||||
import io.github.cdimascio.dotenv.Dotenv
|
||||
import io.ktor.server.application.*
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
|
||||
fun Application.configureDatabases(dotenv: Dotenv) {
|
||||
val dbHost = dotenv["DB_HOST"] ?: throw Exception("DB_HOST not found")
|
||||
@ -23,40 +19,7 @@ fun Application.configureDatabases(dotenv: Dotenv) {
|
||||
user = dbUser,
|
||||
password = dbPassword
|
||||
)
|
||||
val userService = UserService()
|
||||
|
||||
routing {
|
||||
// Create user
|
||||
post("/users") {
|
||||
val user = call.receive<UserDTO>()
|
||||
val id = userService.create(user)
|
||||
call.respond(HttpStatusCode.Created, id)
|
||||
}
|
||||
|
||||
// Read user
|
||||
get("/users/{id}") {
|
||||
val id = call.parameters["id"]?.toInt() ?: throw IllegalArgumentException("Invalid ID")
|
||||
val user = userService.read(id)
|
||||
if (user != null) {
|
||||
call.respond(HttpStatusCode.OK, user)
|
||||
} else {
|
||||
call.respond(HttpStatusCode.NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
// Update user
|
||||
put("/users/{id}") {
|
||||
val id = call.parameters["id"]?.toInt() ?: throw IllegalArgumentException("Invalid ID")
|
||||
val user = call.receive<UserDTO>()
|
||||
userService.update(id, user)
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
|
||||
// Delete user
|
||||
delete("/users/{id}") {
|
||||
val id = call.parameters["id"]?.toInt() ?: throw IllegalArgumentException("Invalid ID")
|
||||
userService.delete(id)
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
}
|
||||
val actionService = ActionService()
|
||||
val authService = AuthService()
|
||||
}
|
||||
|
@ -47,9 +47,10 @@ class NewActionDTO(
|
||||
class ActionService {
|
||||
object Actions : Table("actions") {
|
||||
val id = uuid("id").autoGenerate()
|
||||
val name = text("name")
|
||||
val name = text("name").uniqueIndex()
|
||||
val kind = text("kind")
|
||||
val a_source = 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")
|
||||
|
||||
@ -62,34 +63,32 @@ class ActionService {
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
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 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 {
|
||||
|
78
backend/src/main/kotlin/schemas/Auth.kt
Normal file
78
backend/src/main/kotlin/schemas/Auth.kt
Normal file
@ -0,0 +1,78 @@
|
||||
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.security.MessageDigest
|
||||
import java.util.UUID
|
||||
|
||||
@Serializable
|
||||
class AuthDTO(
|
||||
val id: String,
|
||||
val name: String,
|
||||
val createdAt: String,
|
||||
val updatedAt: String
|
||||
)
|
||||
|
||||
@Serializable
|
||||
class NewAuthDTO(
|
||||
val name: String,
|
||||
val pin: String,
|
||||
val key: String
|
||||
)
|
||||
|
||||
class AuthService {
|
||||
object Auths : Table("auths") {
|
||||
val id = uuid("id").autoGenerate()
|
||||
val name = text("name").uniqueIndex()
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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 read(id: UUID): AuthDTO? = transaction {
|
||||
Auths.selectAll()
|
||||
.where { Auths.id eq id }
|
||||
.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 sha256(it: String): String {
|
||||
val bytes = it.toByteArray()
|
||||
val md = MessageDigest.getInstance("SHA-256")
|
||||
val digest = md.digest(bytes)
|
||||
return digest.joinToString("") { "%02x".format(it) }
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
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
|
||||
|
||||
@Serializable
|
||||
data class UserDTO(val name: String, val age: Int)
|
||||
|
||||
class UserService {
|
||||
object Users : Table("users") {
|
||||
val id = integer("id").autoIncrement()
|
||||
val name = varchar("name", length = 50)
|
||||
val age = integer("age")
|
||||
|
||||
override val primaryKey = PrimaryKey(id)
|
||||
}
|
||||
|
||||
init {
|
||||
transaction {
|
||||
SchemaUtils.create(Users)
|
||||
}
|
||||
}
|
||||
|
||||
fun create(user: UserDTO): Int =
|
||||
transaction {
|
||||
Users.insert {
|
||||
it[name] = user.name
|
||||
it[age] = user.age
|
||||
}[Users.id]
|
||||
}
|
||||
|
||||
fun read(id: Int): UserDTO? =
|
||||
transaction {
|
||||
Users.selectAll()
|
||||
.where { Users.id eq id }
|
||||
.map { UserDTO(it[Users.name], it[Users.age]) }
|
||||
.singleOrNull()
|
||||
}
|
||||
|
||||
fun update(id: Int, user: UserDTO) {
|
||||
transaction {
|
||||
Users.update({ Users.id eq id }) {
|
||||
it[name] = user.name
|
||||
it[age] = user.age
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun delete(id: Int) {
|
||||
transaction {
|
||||
Users.deleteWhere { Users.id.eq(id) }
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user