From 89bef924bea6e5c4eab2af4e08386a3f4f1bbc64 Mon Sep 17 00:00:00 2001
From: 000Sanya <000sanya.000sanya@gmail.com>
Date: Wed, 26 Aug 2020 12:35:19 +1000
Subject: [PATCH] write klient impl

---
 .../ktor/client/KtorOneToManyKeyValueRepo.kt  | 61 +++++++++++++++----
 .../repos/ktor/common/OneToManyPostObjects.kt |  9 +++
 .../repos/ktor/common/OneToManyRoutes.kt      |  6 +-
 3 files changed, 63 insertions(+), 13 deletions(-)
 create mode 100644 utils/repos/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/common/OneToManyPostObjects.kt

diff --git a/utils/repos/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/client/KtorOneToManyKeyValueRepo.kt b/utils/repos/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/client/KtorOneToManyKeyValueRepo.kt
index bd0bc8dc..ea67e2cc 100644
--- a/utils/repos/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/client/KtorOneToManyKeyValueRepo.kt
+++ b/utils/repos/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/client/KtorOneToManyKeyValueRepo.kt
@@ -2,10 +2,13 @@ package com.insanusmokrassar.postssystem.utils.repos.ktor.client
 
 import com.insanusmokrassar.postssystem.ktor.asUrlQueryParts
 import com.insanusmokrassar.postssystem.ktor.buildStandardUrl
+import com.insanusmokrassar.postssystem.ktor.client.BodyPair
 import com.insanusmokrassar.postssystem.ktor.client.uniget
+import com.insanusmokrassar.postssystem.ktor.client.unipost
 import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
 import com.insanusmokrassar.postssystem.utils.common.pagination.Pagination
 import com.insanusmokrassar.postssystem.utils.common.pagination.PaginationResult
+import com.insanusmokrassar.postssystem.utils.repos.OneToManyKeyValueRepo
 import com.insanusmokrassar.postssystem.utils.repos.OneToManyReadKeyValueRepo
 import com.insanusmokrassar.postssystem.utils.repos.OneToManyWriteKeyValueRepo
 import com.insanusmokrassar.postssystem.utils.repos.ktor.common.*
@@ -72,7 +75,7 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
             baseUrl,
             countByKeyRoute,
             mapOf(
-                keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k)
+                keyParameterName to standardKtorSerialFormat.encodeToHexString(keySerializer, k) // TODO: Леша, придумай короче запись, типа keySerializer.toHexString(k)
             )
         ),
         Long.serializer()
@@ -91,19 +94,53 @@ class KtorOneToManyReadKeyValueRepo<Key, Value> (
 class KtorOneToManyWriteKeyValueRepo<Key, Value> (
     private val baseUrl: String,
     private val client: HttpClient = HttpClient(),
+    private val keySerializer: KSerializer<Key>,
+    private val valueSerializer: KSerializer<Value>,
 ) : OneToManyWriteKeyValueRepo<Key, Value> {
-    override suspend fun add(k: Key, v: Value) {
-        TODO("Not yet implemented")
-    }
+    override suspend fun add(k: Key, v: Value) = client.unipost(
+        buildStandardUrl(
+            baseUrl,
+            addRoute,
+        ),
+        BodyPair(KeyValuePostObject.serializer(keySerializer, valueSerializer), KeyValuePostObject(k, v)),
+        Unit.serializer(),
+    )
 
-    override suspend fun remove(k: Key, v: Value) {
-        TODO("Not yet implemented")
-    }
+    override suspend fun remove(k: Key, v: Value) = client.unipost(
+        buildStandardUrl(
+            baseUrl,
+            removeRoute,
+        ),
+        BodyPair(KeyValuePostObject.serializer(keySerializer, valueSerializer), KeyValuePostObject(k, v)),
+        Unit.serializer(),
+    )
 
-    override suspend fun clear(k: Key) {
-        TODO("Not yet implemented")
-    }
+    override suspend fun clear(k: Key)  = client.unipost(
+        buildStandardUrl(
+            baseUrl,
+            clearRoute,
+        ),
+        BodyPair(keySerializer, k),
+        Unit.serializer(),
+    )
 }
 
-class KtorOneToManyKeyValueRepo {
-}
\ No newline at end of file
+class KtorOneToManyKeyValueRepo<Key, Value>(
+    baseUrl: String,
+    baseSubpart: String,
+    client: HttpClient,
+    keySerializer: KSerializer<Key>,
+    valueSerializer: KSerializer<Value>,
+) : OneToManyKeyValueRepo<Key, Value>,
+    OneToManyReadKeyValueRepo<Key, Value> by KtorOneToManyReadKeyValueRepo<Key, Value> (
+        "$baseUrl/$baseSubpart",
+        client,
+        keySerializer,
+        valueSerializer,
+    ),
+    OneToManyWriteKeyValueRepo<Key, Value> by KtorOneToManyWriteKeyValueRepo<Key, Value> (
+        "$baseUrl/$baseSubpart",
+        client,
+        keySerializer,
+        valueSerializer,
+    )
\ No newline at end of file
diff --git a/utils/repos/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/common/OneToManyPostObjects.kt b/utils/repos/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/common/OneToManyPostObjects.kt
new file mode 100644
index 00000000..2dbcbaa8
--- /dev/null
+++ b/utils/repos/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/common/OneToManyPostObjects.kt
@@ -0,0 +1,9 @@
+package com.insanusmokrassar.postssystem.utils.repos.ktor.common
+
+import kotlinx.serialization.Serializable
+
+@Serializable
+data class KeyValuePostObject<Key, Value>(
+    val key: Key,
+    val value: Value
+)
\ No newline at end of file
diff --git a/utils/repos/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/common/OneToManyRoutes.kt b/utils/repos/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/common/OneToManyRoutes.kt
index 2a89cbd5..a2157d19 100644
--- a/utils/repos/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/common/OneToManyRoutes.kt
+++ b/utils/repos/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/ktor/common/OneToManyRoutes.kt
@@ -5,4 +5,8 @@ const val keysRoute = "keys"
 const val containsByKeyRoute = "containsByKey"
 const val containsByKeyValueRoute = "containsByKeyValue"
 const val countByKeyRoute = "countByKey"
-const val countRoute = "count"
\ No newline at end of file
+const val countRoute = "count"
+
+const val addRoute = "add"
+const val removeRoute = "remove"
+const val clearRoute = "clear"
\ No newline at end of file