Add Connection and refactoring EntityCollection

This commit is contained in:
2019-06-03 17:29:05 +02:00
parent 65fb032ad9
commit c5d4bfb07a
7 changed files with 177 additions and 37 deletions

View File

@@ -1,37 +1,23 @@
package fr.postgresjson.repository
import com.github.jasync.sql.db.pool.ConnectionPool
import com.github.jasync.sql.db.postgresql.PostgreSQLConnection
import fr.postgresjson.entity.EntityCollection
import fr.postgresjson.entity.EntityI
import fr.postgresjson.serializer.Serializer
import kotlin.reflect.KClass
interface RepositoryI<T, E : EntityI<T>>
abstract class Repository<T, E : EntityI<T>> : RepositoryI<T, E> {
private val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any>>> = mutableMapOf()
abstract var connection: ConnectionPool<PostgreSQLConnection>
private inline fun <I, reified R : EntityI<I>> get(id: I): R? {
val collection = collections[R::class]
val entity = collection?.get(id!!)
return entity as R?
}
private inline fun <I, reified R : EntityI<I>> set(entity: R) {
if (collections[R::class] == null) {
collections[R::class] = EntityCollection()
}
collections[R::class]!!.set(entity as EntityI<Any>)
}
fun <T> findById(id: T): EntityI<T>? {
return when (val e = get(id)) {
fun <T> findById(id: T): EntityI<T?>? {
return when (val e = EntityCollection().get(id)) {
null -> {
// TODO create Request
Serializer().deserialize<T, EntityI<T>>("""{"plop", "plip"}""")
Serializer().deserialize<T, EntityI<T?>>("""{"plop", "plip"}""")
}
else -> e
}
}
}