From dbfe2f6661d530ef122c02b6a301f94f4acdacf2 Mon Sep 17 00:00:00 2001 From: 000Sanya <000sanya.000sanya@gmail.com> Date: Sun, 20 Sep 2020 12:17:11 +1000 Subject: [PATCH] impl common for repos --- repos/common/build.gradle | 16 ++++++++ .../dev/inmo/micro_utils/repos/IdUtils.kt | 5 +++ .../repos/OneToManyKeyValueRepo.kt | 23 +++++++++++ .../kotlin/dev/inmo/micro_utils/repos/Repo.kt | 4 ++ .../micro_utils/repos/StandartCRUDRepo.kt | 40 +++++++++++++++++++ .../micro_utils/repos/StandartKeyValueRepo.kt | 23 +++++++++++ settings.gradle | 1 + 7 files changed, 112 insertions(+) create mode 100644 repos/common/build.gradle create mode 100644 repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/IdUtils.kt create mode 100644 repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/OneToManyKeyValueRepo.kt create mode 100644 repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/Repo.kt create mode 100644 repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt create mode 100644 repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt diff --git a/repos/common/build.gradle b/repos/common/build.gradle new file mode 100644 index 00000000000..3ba3d995b38 --- /dev/null +++ b/repos/common/build.gradle @@ -0,0 +1,16 @@ +plugins { + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" +} + +apply from: "$mppProjectWithSerializationPresetPath" + +kotlin { + sourceSets { + commonMain { + dependencies { + api internalProject("micro_utils.pagination.common") + } + } + } +} \ No newline at end of file diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/IdUtils.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/IdUtils.kt new file mode 100644 index 00000000000..8c99e003e29 --- /dev/null +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/IdUtils.kt @@ -0,0 +1,5 @@ +package dev.inmo.micro_utils.repos + +import com.benasher44.uuid.uuid4 + +fun generateId() = uuid4().toString() \ No newline at end of file diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/OneToManyKeyValueRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/OneToManyKeyValueRepo.kt new file mode 100644 index 00000000000..3d5ec4bb464 --- /dev/null +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/OneToManyKeyValueRepo.kt @@ -0,0 +1,23 @@ +package dev.inmo.micro_utils.repos + +import dev.inmo.micro_utils.pagination.Pagination +import dev.inmo.micro_utils.pagination.PaginationResult + +interface OneToManyReadKeyValueRepo : Repo { + suspend fun get(k: Key, pagination: Pagination, reversed: Boolean = false): PaginationResult + suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult + suspend fun contains(k: Key): Boolean + suspend fun contains(k: Key, v: Value): Boolean + suspend fun count(k: Key): Long + suspend fun count(): Long +} + +interface OneToManyWriteKeyValueRepo : + Repo { + suspend fun add(k: Key, v: Value) + suspend fun remove(k: Key, v: Value) + suspend fun clear(k: Key) +} + +interface OneToManyKeyValueRepo : OneToManyReadKeyValueRepo, + OneToManyWriteKeyValueRepo \ No newline at end of file diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/Repo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/Repo.kt new file mode 100644 index 00000000000..13b2c05a053 --- /dev/null +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/Repo.kt @@ -0,0 +1,4 @@ +package dev.inmo.micro_utils.repos + +interface Repo { +} \ No newline at end of file diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt new file mode 100644 index 00000000000..73b71a63f00 --- /dev/null +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt @@ -0,0 +1,40 @@ +package dev.inmo.micro_utils.repos + +import dev.inmo.micro_utils.pagination.Pagination +import dev.inmo.micro_utils.pagination.PaginationResult + +interface ReadStandardCRUDRepo : Repo { + suspend fun getByPagination(pagination: Pagination): PaginationResult + suspend fun getById(id: IdType): ObjectType? + suspend fun contains(id: IdType): Boolean +} + +typealias UpdatedValuePair = Pair +val UpdatedValuePair.id + get() = first +val UpdatedValuePair<*, ValueType>.value + get() = second + +interface WriteStandardCRUDRepo : Repo { + val newObjectsFlow: Flow + val updatedObjectsFlow: Flow + val deletedObjectsIdsFlow: Flow + + suspend fun create(values: List): List + suspend fun update(id: IdType, value: InputValueType): ObjectType? + suspend fun update(values: List>): List + suspend fun deleteById(ids: List) +} + +suspend fun WriteStandardCRUDRepo.create( + vararg values: InputValueType +): List = create(values.toList()) +suspend fun WriteStandardCRUDRepo.update( + vararg values: UpdatedValuePair +): List = update(values.toList()) +suspend fun WriteStandardCRUDRepo.deleteById( + vararg ids: IdType +) = deleteById(ids.toList()) + +interface StandardCRUDRepo : ReadStandardCRUDRepo, + WriteStandardCRUDRepo \ No newline at end of file diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt new file mode 100644 index 00000000000..9cd49aef05e --- /dev/null +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt @@ -0,0 +1,23 @@ +package dev.inmo.micro_utils.repos + +import dev.inmo.micro_utils.pagination.Pagination +import dev.inmo.micro_utils.pagination.PaginationResult + +interface StandardReadKeyValueRepo : Repo { + suspend fun get(k: Key): Value? + suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult + suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult + suspend fun contains(key: Key): Boolean + suspend fun count(): Long +} + +interface StandardWriteKeyValueRepo : Repo { + val onNewValue: Flow> + val onValueRemoved: Flow + + suspend fun set(k: Key, v: Value) + suspend fun unset(k: Key) +} + +interface StandardKeyValueRepo : StandardReadKeyValueRepo, + StandardWriteKeyValueRepo \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 2658fb6f011..ec72891f7d1 100644 --- a/settings.gradle +++ b/settings.gradle @@ -5,6 +5,7 @@ String[] includes = [ ":pagination:exposed", ":pagination:ktor:common", ":pagination:ktor:server", + ":repos:common", ]