From b77f2fa371f88249b653eb53618002ea19834d32 Mon Sep 17 00:00:00 2001 From: calboo Date: Sun, 30 Nov 2025 21:59:05 +0100 Subject: [PATCH] patching values --- .../src/main/kotlin/api/plugins/Routing.kt | 2 + .../src/main/kotlin/api/routes/getValue.kt | 2 +- .../src/main/kotlin/api/routes/patchValue.kt | 41 +++++++++++++++++++ .../src/main/kotlin/api/routes/setValue.kt | 1 + .../src/main/kotlin/theGoodStuff/Pool.kt | 7 ++++ 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 Server/FastKeyValueServer/src/main/kotlin/api/routes/patchValue.kt diff --git a/Server/FastKeyValueServer/src/main/kotlin/api/plugins/Routing.kt b/Server/FastKeyValueServer/src/main/kotlin/api/plugins/Routing.kt index 65532bc..8fb6ad4 100644 --- a/Server/FastKeyValueServer/src/main/kotlin/api/plugins/Routing.kt +++ b/Server/FastKeyValueServer/src/main/kotlin/api/plugins/Routing.kt @@ -13,6 +13,7 @@ import kotlinx.serialization.json.Json import org.calvin.erfmann.api.routes.getValue import org.calvin.erfmann.api.routes.helloRoutes import org.calvin.erfmann.api.routes.login +import org.calvin.erfmann.api.routes.patchValue import org.calvin.erfmann.api.routes.setValue import org.calvin.erfmann.stuff.authService import org.calvin.erfmann.theGoodStuff.PoolManager @@ -23,6 +24,7 @@ fun Application.configureRouting(authService: authService, poolManager: PoolMana login(authService) getValue(authService, poolManager) setValue(authService, poolManager) + patchValue(authService, poolManager) } } diff --git a/Server/FastKeyValueServer/src/main/kotlin/api/routes/getValue.kt b/Server/FastKeyValueServer/src/main/kotlin/api/routes/getValue.kt index 4daedea..3fc997b 100644 --- a/Server/FastKeyValueServer/src/main/kotlin/api/routes/getValue.kt +++ b/Server/FastKeyValueServer/src/main/kotlin/api/routes/getValue.kt @@ -11,7 +11,7 @@ import org.calvin.erfmann.stuff.authService import org.calvin.erfmann.theGoodStuff.PoolManager @Serializable -data class GetValueRequest(val token: String, val pool: String, val key: String) +data class GetValueRequest(val token: String, val pool: String, val key: String,) fun Route.getValue(authService: authService, poolManager: PoolManager) { diff --git a/Server/FastKeyValueServer/src/main/kotlin/api/routes/patchValue.kt b/Server/FastKeyValueServer/src/main/kotlin/api/routes/patchValue.kt new file mode 100644 index 0000000..02150c5 --- /dev/null +++ b/Server/FastKeyValueServer/src/main/kotlin/api/routes/patchValue.kt @@ -0,0 +1,41 @@ +package org.calvin.erfmann.api.routes + +import io.ktor.server.routing.patch + + + +import io.ktor.server.application.call +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.routing.Route +import io.ktor.server.routing.post +import kotlinx.serialization.Serializable +import org.calvin.erfmann.stuff.authService +import org.calvin.erfmann.theGoodStuff.PoolManager + + +@Serializable +data class PatchValueRequest(val token: String, val pool: String, val key: String, val value: String) + + +fun Route.patchValue(authService: authService, poolManager: PoolManager) { + + patch("/value") { + val request = call.receive() + + val isValid = authService.isTokenValid(request.token) + + if (isValid) { + val pool = poolManager.getPool(request.pool) + if (pool != null) { + pool.setValueValue(request.key, request.value) + call.respond(mapOf("status" to "success")) + } else { + call.respond(mapOf("error" to "Pool not found")) + } + } else { + call.respond(mapOf("error" to "Invalid token")) + } + } + +} \ No newline at end of file diff --git a/Server/FastKeyValueServer/src/main/kotlin/api/routes/setValue.kt b/Server/FastKeyValueServer/src/main/kotlin/api/routes/setValue.kt index d2abab2..abe1ad3 100644 --- a/Server/FastKeyValueServer/src/main/kotlin/api/routes/setValue.kt +++ b/Server/FastKeyValueServer/src/main/kotlin/api/routes/setValue.kt @@ -28,6 +28,7 @@ fun Route.setValue(authService: authService, poolManager: PoolManager) { call.respond(mapOf("status" to "success")) } else { poolManager.createPool(request.pool).setValueValue(request.key, request.value) + call.respond(mapOf("status" to "success")) } } else { call.respond(mapOf("error" to "Invalid token")) diff --git a/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/Pool.kt b/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/Pool.kt index 6210ffa..3f973fe 100644 --- a/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/Pool.kt +++ b/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/Pool.kt @@ -23,6 +23,13 @@ class Pool(initName: String) { } } + fun updateValueValue(key: String, newValue: String) { + val value = values[key] + if (value != null) { + value.setValueValue(newValue) + } + } + fun getVersion(key: String, version: Long): String? { val value = values[key] return value?.getVersion(version)