Files
Lab7-Prog/server/src/main/kotlin/database/Repository.kt
T
2026-05-13 23:51:00 +03:00

60 lines
1.9 KiB
Kotlin

package database
import database.tables.CarTable
import database.tables.CoordinatesTable
import database.tables.HumanBeingTable
import database.tables.UserTable
import io.github.oshai.kotlinlogging.KotlinLogging
import models.HumanBeing
import org.jetbrains.exposed.v1.core.*
import org.jetbrains.exposed.v1.jdbc.*
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
import java.util.UUID
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid
private val logger = KotlinLogging.logger {}
class Repository(private val filePath: String) {
//fun read(): List<HumanBeing> {}
@OptIn(ExperimentalUuidApi::class)
fun write(items: Collection<HumanBeing>, userId: UUID): Boolean {
transaction {
for (item in items) {
val cords = CoordinatesTable.insertAndGetId {
it[x] = item.coordinates.x
it[y] = item.coordinates.y
}
val cars = CarTable.insertAndGetId {
it[cool] = item.car.cool
}
val user = UserTable
.select(UserTable.id)
.where { UserTable.id eq userId }
.first()
HumanBeingTable.insert {
it[owner_id] = userId
it[name] = item.name
it[id] = user[UserTable.id] // it fucking autoincrements (check perplexity)
it[coordinates] = cords
it[creationDate] = item.creationDate
it[realHero] = item.realHero
it[hasToothpick] = item.hasToothpick
it[impactSpeed] = item.impactSpeed
it[soundtrackName] = item.soundtrackName
it[minutesOfWaiting] = item.minutesOfWaiting
it[car] = cars
it[weaponType] = item.weaponType
}
}
}
}
}