diff options
author | Linnea Gräf <nea@nea.moe> | 2025-01-21 00:10:18 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2025-01-21 00:15:49 +0100 |
commit | 4e20d0e95b2fe6ef71a33f41f9cd8cebd06cf982 (patch) | |
tree | fd681d3ff3ba3796d443b408e8f25b6c7ec4f509 /server/core/src/main/kotlin | |
parent | bf90a9b8eb85f28fbd2285aaa26316faf9753eb2 (diff) | |
download | LocalTransactionLedger-4e20d0e95b2fe6ef71a33f41f9cd8cebd06cf982.tar.gz LocalTransactionLedger-4e20d0e95b2fe6ef71a33f41f9cd8cebd06cf982.tar.bz2 LocalTransactionLedger-4e20d0e95b2fe6ef71a33f41f9cd8cebd06cf982.zip |
feat(server): Add example request route
Diffstat (limited to 'server/core/src/main/kotlin')
-rw-r--r-- | server/core/src/main/kotlin/moe/nea/ledger/server/core/api/BaseApi.kt | 75 |
1 files changed, 74 insertions, 1 deletions
diff --git a/server/core/src/main/kotlin/moe/nea/ledger/server/core/api/BaseApi.kt b/server/core/src/main/kotlin/moe/nea/ledger/server/core/api/BaseApi.kt index c58763f..4bc6472 100644 --- a/server/core/src/main/kotlin/moe/nea/ledger/server/core/api/BaseApi.kt +++ b/server/core/src/main/kotlin/moe/nea/ledger/server/core/api/BaseApi.kt @@ -8,12 +8,23 @@ import io.ktor.server.routing.get import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.async +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializable +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.json.Json import kotlinx.serialization.json.decodeFromStream +import moe.nea.ledger.ItemChange +import moe.nea.ledger.TransactionType +import moe.nea.ledger.database.DBItemEntry import moe.nea.ledger.database.DBLogEntry import moe.nea.ledger.database.Database +import moe.nea.ledger.database.sql.Clause import moe.nea.ledger.server.core.Profile -import sh.ondr.jsonschema.jsonSchema +import moe.nea.ledger.utils.ULIDWrapper fun Route.apiRouting(database: Database) { get("/profiles") { @@ -51,12 +62,74 @@ fun Route.apiRouting(database: Database) { schema<Map<String, String?>>() } } + get("/entries") { + val logs = mutableMapOf<ULIDWrapper, LogEntry>() + val items = mutableMapOf<ULIDWrapper, MutableList<SerializableItemChange>>() + DBLogEntry.from(database.connection) + .join(DBItemEntry, Clause { column(DBItemEntry.transactionId) eq column(DBLogEntry.transactionId) }) + .select(DBLogEntry.profileId, + DBLogEntry.playerId, + DBLogEntry.transactionId, + DBLogEntry.type, + DBItemEntry.mode, + DBItemEntry.itemId, + DBItemEntry.size) + .forEach { row -> + logs.getOrPut(row[DBLogEntry.transactionId]) { + LogEntry(row[DBLogEntry.type], + row[DBLogEntry.transactionId], + listOf()) + } + items.getOrPut(row[DBLogEntry.transactionId]) { mutableListOf() } + .add(SerializableItemChange( + row[DBItemEntry.itemId].string, + row[DBItemEntry.mode], + row[DBItemEntry.size], + )) + } + val compiled = logs.values.map { it.copy(items = items[it.id]!!) } + call.respond(compiled) + }.docs { + summary = "Get all log entries" + operationId = "getLogEntries" + tag(Tags.DATA) + respondsOk { + schema<List<LogEntry>>() + } + } +} + +@Serializable +data class LogEntry( + val type: TransactionType, + val id: @Serializable(ULIDSerializer::class) ULIDWrapper, + val items: List<SerializableItemChange>, +) + +@Serializable +data class SerializableItemChange( + val itemId: String, + val direction: ItemChange.ChangeDirection, + val amount: Double, +) + +object ULIDSerializer : KSerializer<ULIDWrapper> { + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ULID", PrimitiveKind.STRING) + + override fun deserialize(decoder: Decoder): ULIDWrapper { + return ULIDWrapper(decoder.decodeString()) + } + + override fun serialize(encoder: Encoder, value: ULIDWrapper) { + encoder.encodeString(value.wrapped) + } } enum class Tags : IntoTag { PROFILE, HYPIXEL, MANAGEMENT, + DATA, ; override fun intoTag(): String { |