crud exposed realization open fun asObject

This commit is contained in:
InsanusMokrassar 2020-12-02 16:25:37 +06:00
parent 490c318d1c
commit 88f2c16c82
2 changed files with 12 additions and 13 deletions

View File

@ -4,6 +4,9 @@
* `Versions`:
* `Ktor`: `1.4.2` -> `1.4.3`
* `Repo`
* `Exposed`
* `asObject` open fun has been added in CRUD realization
## 0.4.8

View File

@ -25,8 +25,10 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
override val updatedObjectsFlow: Flow<ObjectType> = updateObjectsChannel.asSharedFlow()
override val deletedObjectsIdsFlow: Flow<IdType> = deleteObjectsIdsChannel.asSharedFlow()
@Deprecated("Will be removed in near major update. Override open fun with the same name instead")
abstract val InsertStatement<Number>.asObject: ObjectType
abstract val selectByIds: SqlExpressionBuilder.(List<out IdType>) -> Op<Boolean>
protected open fun InsertStatement<Number>.asObject(value: InputValueType): ObjectType = asObject
abstract val selectByIds: SqlExpressionBuilder.(List<IdType>) -> Op<Boolean>
protected abstract fun insert(value: InputValueType, it: InsertStatement<Number>)
protected abstract fun update(id: IdType, value: InputValueType, it: UpdateStatement)
@ -34,7 +36,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
protected open suspend fun onBeforeCreate(value: List<InputValueType>) {}
private fun createWithoutNotification(value: InputValueType): ObjectType {
return transaction(database) {
insert { insert(value, it) }.asObject
insert { insert(value, it) }.asObject(value)
}
}
@ -42,10 +44,8 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
onBeforeCreate(values)
return transaction(db = database) {
values.map { value -> createWithoutNotification(value) }
}.also {
it.forEach {
newObjectsChannel.emit(it)
}
}.onEach {
newObjectsChannel.emit(it)
}
}
@ -83,13 +83,9 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
return (
transaction(db = database) {
values.map { (id, value) -> updateWithoutNotification(id, value) }
}.filter {
it != null
} as List<ObjectType>
).also {
it.forEach {
updateObjectsChannel.emit(it)
}
}.filterNotNull()
).onEach {
updateObjectsChannel.emit(it)
}
}
protected open suspend fun onBeforeDelete(ids: List<IdType>) {}