All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.app.softwork.cloudkitclient.TestDatabase.kt Maven / Gradle / Ivy

The newest version!
package app.softwork.cloudkitclient

import app.softwork.cloudkitclient.Client.Database
import app.softwork.cloudkitclient.Environment.Development
import app.softwork.cloudkitclient.Push.Response
import app.softwork.cloudkitclient.Record.Fields
import app.softwork.cloudkitclient.Record.Information
import app.softwork.cloudkitclient.types.*
import app.softwork.cloudkitclient.values.*
import kotlinx.datetime.*
import kotlin.reflect.*
import kotlin.uuid.*

@OptIn(ExperimentalUuidApi::class)
public class TestDatabase(
    public val name: String,
    public val assets: MutableMap>,
    clock: Clock,
    public val zones: Map = mapOf(ZoneID.default to TestStorage(initUser, assets, clock))
) : Database {

    public companion object {
        public val initUser: UserRecord = UserRecord(
            recordName = "TestUser", 
            fields = UserRecord.Fields(
                firstName = Value.String("Test"),
                lastName = Value.String("User"), 
                emailAddress = null
            )
        )
    }

    public override suspend fun > query(
        recordInformation: Information,
        zoneID: ZoneID,
        sort: Sort.Builder.() -> Unit,
        filter: Filter.Builder.() -> Unit
    ): List = zones[zoneID]!!.query(
        recordInformation,
        Filter.Builder().apply(filter).build(),
        Sort.Builder().apply(sort).build()
    )

    private val > R.zone: Storage get() = zones[zoneID] ?: error("Zone $zoneID not found")

    override suspend fun > create(record: R, recordInformation: Information): R =
        record.zone.create(record, recordInformation)

    override suspend fun > read(
        recordName: String,
        recordInformation: Information,
        zoneID: ZoneID
    ): R? = zones[zoneID]!!.get(recordName, recordInformation)

    override suspend fun > update(record: R, recordInformation: Information): R =
        record.zone.update(record, recordInformation)

    override suspend fun > delete(record: R, recordInformation: Information) {
        record.zone.delete(record, recordInformation)
    }

    override suspend fun > upload(
        asset: ByteArray,
        recordInformation: Information,
        field: KProperty1,
        recordName: String?,
        zoneID: ZoneID
    ): Asset = zones[zoneID]!!.upload(asset, recordInformation, field, recordName)

    override suspend fun createToken(): Response =
        Response(apnsEnvironment = Development, apnsToken = Uuid.random().toString(), webcourierURL = "")
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy