Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,6 @@ fabric.properties
*/build/
/build
/kotlin-js-store/
/default.realm
/default.realm.lock
/default.realm.management/
2 changes: 2 additions & 0 deletions carddb/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
kotlin("plugin.serialization")
// id("com.android.library")
id("com.squareup.sqldelight")
id("io.realm.kotlin") version "1.6.0"
}

kotlin {
Expand All @@ -18,6 +19,7 @@ kotlin {
dependencies {
implementation(libs.bundles.base)
implementation(libs.sqldelight.coroutines)
implementation("io.realm.kotlin:library-base:1.6.0")
}
}
val jvmMain by getting {
Expand Down
19 changes: 3 additions & 16 deletions carddb/src/commonMain/kotlin/DatabaseHelper.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
import com.mfriend.db.MTGDb
import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.runtime.coroutines.asFlow
import com.squareup.sqldelight.runtime.coroutines.mapToList
import commfrienddb.Card
import kotlinx.coroutines.flow.Flow

interface DatabaseHelper {
suspend fun insertCard(card: Card)
fun getCards(): Flow<List<Card>>
}

class DatabaseHelperImpl(sqlDriver: SqlDriver) : DatabaseHelper {
private val database = MTGDb(sqlDriver)
override suspend fun insertCard(card: Card) {
database.cardQueries.insertCard(card)
}
override fun getCards(): Flow<List<Card>> {
return database.cardQueries.selectAll().asFlow().mapToList()
}
}
fun getCardsFlow(): Flow<List<Card>>
suspend fun getCards(): List<Card>
}
2 changes: 1 addition & 1 deletion carddb/src/commonMain/kotlin/Koin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import org.koin.dsl.module

val databaseModule = module {
includes(platformModule)
singleOf(::DatabaseHelperImpl) { bind<DatabaseHelper>() }
singleOf(::RealmDatabase) { bind<DatabaseHelper>() }
}
internal expect val platformModule: Module
32 changes: 32 additions & 0 deletions carddb/src/commonMain/kotlin/RealmDatabase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import commfrienddb.Card
import io.realm.kotlin.Realm
import io.realm.kotlin.RealmConfiguration
import io.realm.kotlin.ext.query
import io.realm.kotlin.notifications.UpdatedResults
import io.realm.kotlin.query.RealmResults
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.map
import realm.RealmCard
import realm.toCard
import realm.toRealm

class RealmDatabase : DatabaseHelper {
private val config = RealmConfiguration.create(schema = setOf(RealmCard::class))
val realm = Realm.open(config)
override suspend fun insertCard(card: Card) {
realm.write {
copyToRealm(card.toRealm())
}
}

override fun getCardsFlow(): Flow<List<Card>> {
val results: RealmResults<RealmCard> = realm.query<RealmCard>().find()
return results.asFlow().filterIsInstance<UpdatedResults<RealmCard>>().map { it.list.map(RealmCard::toCard) }
}

override suspend fun getCards(): List<Card> {
val results: RealmResults<RealmCard> = realm.query<RealmCard>().find()
return results.map(RealmCard::toCard)
}
}
21 changes: 21 additions & 0 deletions carddb/src/commonMain/kotlin/SqlDatabase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import com.mfriend.db.MTGDb
import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.runtime.coroutines.asFlow
import com.squareup.sqldelight.runtime.coroutines.mapToList
import commfrienddb.Card
import kotlinx.coroutines.flow.Flow

class SqlDatabase(sqlDriver: SqlDriver) : DatabaseHelper {
private val database = MTGDb(sqlDriver)
override suspend fun insertCard(card: Card) {
database.cardQueries.insertCard(card)
}

override fun getCardsFlow(): Flow<List<Card>> {
return database.cardQueries.selectAll().asFlow().mapToList()
}

override suspend fun getCards(): List<Card> {
return database.cardQueries.selectAll().executeAsList()
}
}
37 changes: 37 additions & 0 deletions carddb/src/commonMain/kotlin/realm/RealmCard.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package realm

import commfrienddb.Card
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey
import org.mongodb.kbson.ObjectId

class RealmCard(
) : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
var set_code: String = ""
var set_name: String = ""
var scryfall_url: String = ""
var image_url: String? = null
var price: Double? = null

constructor(
name: String,
set_code: String,
set_name: String,
scryfall_url: String,
image_url: String? = null,
price: Double? = null
) : this() {
this.name = name
this.set_code = set_code
this.set_name = set_name
this.scryfall_url = scryfall_url
this.image_url = image_url
this.price = price
}
}

internal fun Card.toRealm() = RealmCard(name, set_code, set_name, scryfall_url, image_url, price)
internal fun RealmCard.toCard() = Card(name, set_code, set_name, image_url, scryfall_url, price)
14 changes: 12 additions & 2 deletions cli/src/main/kotlin/Cli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ suspend fun main() = coroutineScope {
val client: ScryfallApi = koin.get()
val databaseHelper:DatabaseHelper = koin.get()
launch {
databaseHelper.getCards().collect {
databaseHelper.getCardsFlow().collect {
println(it)
println()
}
Expand All @@ -25,7 +25,6 @@ suspend fun main() = coroutineScope {
2 -> searchCard(client)
else -> break
}
println(result)
result.map { databaseHelper.insertCard(it) }
}
}
Expand All @@ -38,6 +37,17 @@ suspend fun getCardNamed(client: ScryfallApi): Either<String, Card> {

suspend fun searchCard(client: ScryfallApi): Either<String, Card> {
return client.searchCard(readln()).map {
it.singleOrNull()?.let { selection ->
return@map Card(
selection.name,
selection.set,
selection.setName,
selection.imageUris?.large?.url,
selection.scryfallUrl.url,
selection.prices.usd.toDouble()
)
}

it.forEachIndexed { i, card ->
println("$i) $card")
}
Expand Down