Exposed WIP

This commit is contained in:
2022-03-08 00:33:39 +01:00
parent 77f3ab0ed9
commit 59e4bcf36e
2 changed files with 82 additions and 5 deletions

View File

@@ -1,7 +1,26 @@
package eu.fosil.okupamicoche.cli
import eu.fosil.okupamicoche.model.Travel
import eu.fosil.okupamicoche.model.Travels
import io.ktor.http.*
import mu.KotlinLogging
import net.folivo.trixnity.client.api.MatrixApiClient
import net.folivo.trixnity.core.model.RoomId
import net.folivo.trixnity.core.model.UserId
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.transaction
import java.text.DateFormat
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
private val logger = KotlinLogging.logger {}
val matrixApiClient = MatrixApiClient(
baseUrl = Url("http://okupamicoche-synapse:8008/"),
).apply { accessToken.value = "30c05ae90a248a4188e620216fa72e349803310ec83e2a77b34fe90be6081f46" }
object CommandParser {
@@ -12,7 +31,7 @@ object CommandParser {
room: RoomId,
body: String
) {
val words = Regex("[a-zA-Z]+").findAll(body)
val words = Regex("[^!\\s]+").findAll(body)
val command = words.first().value
val args = words.drop(1).toList().map { sequence -> sequence.value }
handleCommand(user, room, command, args)
@@ -24,7 +43,51 @@ object CommandParser {
command: String,
args: List<String>
) {
println("command=$command args=$args")
logger.info("command=$command args=$args")
when (command) {
"create" -> handleCreate(user, room, args)
}
}
private fun handleCreate(
user: UserId,
room: RoomId,
args: List<String>
) {
val origin = args[0]
val destination = args[1]
val date = args[2]
val time = args[3]
val formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:mm")
val unixTime = LocalDateTime.from(formatter.parse("$date $time")).toEpochSecond(ZoneOffset.UTC)
val places = args[4].toIntOrNull() ?: 0
val description = args[5]
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
transaction {
SchemaUtils.create(Travels)
}
transaction {
Travel.new {
this.roomId = room.full
this.driver = user.full
this.origin = origin
this.destination = destination
this.time = unixTime
this.places = places
this.description = description
}
}
transaction {
Travel.all().forEach { travel ->
logger.info("travel id=${travel.id} origin=${travel.origin} dest=${travel.destination}")
}
}
}
}

View File

@@ -1,15 +1,29 @@
package eu.fosil.okupamicoche.model
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
object Travel: IntIdTable() {
object Travels: IntIdTable() {
val roomId = varchar("room_id", 255)
val driver = varchar("driver", 255)
val origin = varchar("origin", 255)
val destination = varchar("destination", 255)
val time = integer("time")
val time = long("time")
val places = integer("places")
val description = varchar("destination", 1023)
val description = varchar("description", 1023)
}
class Travel(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Travel>(Travels)
var roomId by Travels.roomId
var driver by Travels.driver
var origin by Travels.origin
var destination by Travels.destination
var time by Travels.time
var places by Travels.places
var description by Travels.description
}
//object StarWarsFilms : Table() {