set method in realizations

This commit is contained in:
InsanusMokrassar 2020-11-18 20:27:29 +06:00
parent dd2fc5a86f
commit fcacdcd544
6 changed files with 54 additions and 3 deletions

View File

@ -2,6 +2,9 @@
## 0.4.2 ## 0.4.2
* `Repos`:
* Add `WriteOneToManyKeyValueRepo#set` function and extensions
## 0.4.1 ## 0.4.1
* `Repos`: * `Repos`:

View File

@ -107,6 +107,12 @@ open class MapperWriteOneToManyKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
}.toMap() }.toMap()
) )
override suspend fun set(toSet: Map<FromKey, List<FromValue>>) {
to.set(
toSet.map { (k, vs) -> k.toOutKey() to vs.map { v -> v.toOutValue() } }.toMap()
)
}
override suspend fun clear(k: FromKey) = to.clear(k.toOutKey()) override suspend fun clear(k: FromKey) = to.clear(k.toOutKey())
} }

View File

@ -87,6 +87,29 @@ class OneToManyAndroidRepo<Key, Value>(
} }
} }
override suspend fun set(toSet: Map<Key, List<Value>>) {
val (clearedKeys, inserted) = helper.writableTransaction {
toSet.mapNotNull { (k, _) ->
if (delete(tableName, "$idColumnName=?", arrayOf(k.asId())) > 0) {
k
} else {
null
}
} to toSet.flatMap { (k, values) ->
values.map { v ->
insert(
tableName,
null,
contentValuesOf(idColumnName to k.asId(), valueColumnName to v.asValue())
)
k to v
}
}
}
clearedKeys.forEach { _onDataCleared.emit(it) }
inserted.forEach { newPair -> _onNewValue.emit(newPair) }
}
override suspend fun contains(k: Key): Boolean = helper.readableTransaction { override suspend fun contains(k: Key): Boolean = helper.readableTransaction {
select(tableName, selection = "$idColumnName=?", selectionArgs = arrayOf(k.asId()), limit = FirstPagePagination(1).limitClause()).use { select(tableName, selection = "$idColumnName=?", selectionArgs = arrayOf(k.asId()), limit = FirstPagePagination(1).limitClause()).use {
it.count > 0 it.count > 0

View File

@ -42,12 +42,12 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
override suspend fun add(toAdd: Map<Key, List<Value>>) = client.unipost( override suspend fun add(toAdd: Map<Key, List<Value>>) = client.unipost(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
clearRoute, addRoute,
), ),
BodyPair(keyValueMapSerializer, toAdd), BodyPair(keyValueMapSerializer, toAdd),
Unit.serializer(), Unit.serializer(),
) )
override suspend fun clear(k: Key) = client.unipost( override suspend fun clear(k: Key) = client.unipost(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,
clearRoute, clearRoute,
@ -55,4 +55,13 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
BodyPair(keySerializer, k), BodyPair(keySerializer, k),
Unit.serializer(), Unit.serializer(),
) )
override suspend fun set(toSet: Map<Key, List<Value>>) = client.unipost(
buildStandardUrl(
baseUrl,
setRoute,
),
BodyPair(keyValueMapSerializer, toSet),
Unit.serializer(),
)
} }

View File

@ -14,3 +14,4 @@ const val onDataClearedRoute = "onDataCleared"
const val addRoute = "add" const val addRoute = "add"
const val removeRoute = "remove" const val removeRoute = "remove"
const val clearRoute = "clear" const val clearRoute = "clear"
const val setRoute = "set"

View File

@ -61,4 +61,13 @@ fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
originalRepo.clear(key), originalRepo.clear(key),
) )
} }
post(setRoute) {
val obj = call.uniload(keyValueMapSerializer)
call.unianswer(
Unit.serializer(),
originalRepo.set(obj)
)
}
} }