From b1748e39ffeac11c79af80605b6760b28ba73dc8 Mon Sep 17 00:00:00 2001 From: Mustafa Ozhan Date: Sun, 12 Sep 2021 15:48:21 +0200 Subject: [PATCH] mapping CurrencyResponse directly to OfflineRates --- .../ccc/backend/controller/ApiController.kt | 2 +- .../calculator/CalculatorViewModel.kt | 3 +- .../viewmodel/settings/SettingsViewModel.kt | 3 +- .../db/offlinerates/OfflineRatesRepository.kt | 3 +- .../OfflineRatesRepositoryImpl.kt | 8 +++--- .../ccc/common/mapper/CurrencyResponse.kt | 28 +++++++++++++++++++ .../mustafaozhan/ccc/common/mapper/Rates.kt | 14 ---------- 7 files changed, 38 insertions(+), 23 deletions(-) diff --git a/backend/src/jvmMain/kotlin/com/github/mustafaozhan/ccc/backend/controller/ApiController.kt b/backend/src/jvmMain/kotlin/com/github/mustafaozhan/ccc/backend/controller/ApiController.kt index 17ab4ad920..ceda9b1618 100644 --- a/backend/src/jvmMain/kotlin/com/github/mustafaozhan/ccc/backend/controller/ApiController.kt +++ b/backend/src/jvmMain/kotlin/com/github/mustafaozhan/ccc/backend/controller/ApiController.kt @@ -38,7 +38,7 @@ class ApiController( apiRepository .getRatesViaApi(base.name) .execute({ currencyResponse -> - offlineRatesRepository.insertOfflineRates(currencyResponse.rates) + offlineRatesRepository.insertOfflineRates(currencyResponse) }, { error -> kermit.e(error) { error.message.toString() } }) diff --git a/client/src/commonMain/kotlin/com/github/mustafaozhan/ccc/client/viewmodel/calculator/CalculatorViewModel.kt b/client/src/commonMain/kotlin/com/github/mustafaozhan/ccc/client/viewmodel/calculator/CalculatorViewModel.kt index 6befa63b6d..d2977f6b96 100755 --- a/client/src/commonMain/kotlin/com/github/mustafaozhan/ccc/client/viewmodel/calculator/CalculatorViewModel.kt +++ b/client/src/commonMain/kotlin/com/github/mustafaozhan/ccc/client/viewmodel/calculator/CalculatorViewModel.kt @@ -94,7 +94,8 @@ class CalculatorViewModel( data.rates = it calculateConversions(it) _state.update(rateState = RateState.Online(it.date)) - offlineRatesRepository.insertOfflineRates(it) + }.also { + offlineRatesRepository.insertOfflineRates(currencyResponse) } private fun getRatesFailed(t: Throwable) { diff --git a/client/src/commonMain/kotlin/com/github/mustafaozhan/ccc/client/viewmodel/settings/SettingsViewModel.kt b/client/src/commonMain/kotlin/com/github/mustafaozhan/ccc/client/viewmodel/settings/SettingsViewModel.kt index b48d85e948..2bbf2f4fe6 100644 --- a/client/src/commonMain/kotlin/com/github/mustafaozhan/ccc/client/viewmodel/settings/SettingsViewModel.kt +++ b/client/src/commonMain/kotlin/com/github/mustafaozhan/ccc/client/viewmodel/settings/SettingsViewModel.kt @@ -10,7 +10,6 @@ import com.github.mustafaozhan.ccc.client.util.calculateAdRewardEnd import com.github.mustafaozhan.ccc.client.util.isRewardExpired import com.github.mustafaozhan.ccc.client.util.launchIgnored import com.github.mustafaozhan.ccc.client.util.toDateString -import com.github.mustafaozhan.ccc.client.util.toRates import com.github.mustafaozhan.ccc.client.viewmodel.settings.SettingsData.Companion.SYNC_DELAY import com.github.mustafaozhan.ccc.common.api.ApiRepository import com.github.mustafaozhan.ccc.common.db.currency.CurrencyRepository @@ -70,7 +69,7 @@ class SettingsViewModel( delay(SYNC_DELAY) apiRepository.getRatesViaBackend(name).execute( - success = { offlineRatesRepository.insertOfflineRates(it.toRates()) }, + success = { offlineRatesRepository.insertOfflineRates(it) }, error = { error -> kermit.e(error) { error.message.toString() } } ) } diff --git a/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/db/offlinerates/OfflineRatesRepository.kt b/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/db/offlinerates/OfflineRatesRepository.kt index 109da0a21a..1e8aed8b2d 100644 --- a/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/db/offlinerates/OfflineRatesRepository.kt +++ b/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/db/offlinerates/OfflineRatesRepository.kt @@ -1,9 +1,10 @@ package com.github.mustafaozhan.ccc.common.db.offlinerates +import com.github.mustafaozhan.ccc.common.model.CurrencyResponse import com.github.mustafaozhan.ccc.common.model.Rates interface OfflineRatesRepository { - fun insertOfflineRates(rates: Rates) + fun insertOfflineRates(currencyResponse: CurrencyResponse) fun getOfflineRatesByBase(baseName: String): Rates? diff --git a/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/db/offlinerates/OfflineRatesRepositoryImpl.kt b/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/db/offlinerates/OfflineRatesRepositoryImpl.kt index f6e0ce4fc5..4f6d0abb7b 100644 --- a/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/db/offlinerates/OfflineRatesRepositoryImpl.kt +++ b/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/db/offlinerates/OfflineRatesRepositoryImpl.kt @@ -5,16 +5,16 @@ import com.github.mustafaozhan.ccc.common.mapper.toCurrencyResponseEntity import com.github.mustafaozhan.ccc.common.mapper.toModel import com.github.mustafaozhan.ccc.common.mapper.toOfflineRates import com.github.mustafaozhan.ccc.common.mapper.toSerializedString -import com.github.mustafaozhan.ccc.common.model.Rates +import com.github.mustafaozhan.ccc.common.model.CurrencyResponse import com.github.mustafaozhan.logmob.kermit internal class OfflineRatesRepositoryImpl( private val offlineRatesQueries: OfflineRatesQueries ) : OfflineRatesRepository { - override fun insertOfflineRates(rates: Rates) = offlineRatesQueries.insertOfflineRates( - rates.toOfflineRates() - ).also { kermit.d { "OfflineRatesRepositoryImpl insertOfflineRates ${rates.base}" } } + override fun insertOfflineRates(currencyResponse: CurrencyResponse) = offlineRatesQueries + .insertOfflineRates(currencyResponse.toOfflineRates()) + .also { kermit.d { "OfflineRatesRepositoryImpl insertOfflineRates ${currencyResponse.base}" } } override fun getOfflineRatesByBase(baseName: String) = offlineRatesQueries .getOfflineRatesByBase(baseName) diff --git a/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/mapper/CurrencyResponse.kt b/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/mapper/CurrencyResponse.kt index 8cbab1945c..6e752295f8 100644 --- a/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/mapper/CurrencyResponse.kt +++ b/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/mapper/CurrencyResponse.kt @@ -2,6 +2,7 @@ package com.github.mustafaozhan.ccc.common.mapper import com.github.mustafaozhan.ccc.common.entity.CurrencyResponseEntity import com.github.mustafaozhan.ccc.common.model.CurrencyResponse +import com.github.mustafaozhan.ccc.common.db.sql.Offline_rates as OfflineRates internal fun CurrencyResponseEntity.toModel( fallbackBase: String = base @@ -10,3 +11,30 @@ internal fun CurrencyResponseEntity.toModel( date = date, rates = rates.toModel() ) + +internal fun CurrencyResponse.toOfflineRates() = OfflineRates( + base, + date, + rates.aed, rates.afn, rates.all, rates.amd, rates.ang, rates.aoa, rates.ars, rates.aud, + rates.awg, rates.azn, rates.bam, rates.bbd, rates.bdt, rates.bgn, rates.bhd, rates.bif, + rates.bmd, rates.bnd, rates.bob, rates.brl, rates.bsd, rates.btc, rates.btn, rates.bwp, + rates.byn, rates.bzd, rates.cad, rates.cdf, rates.chf, rates.clf, rates.clp, rates.cnh, + rates.cny, rates.cop, rates.crc, rates.cuc, rates.cup, rates.cve, rates.czk, rates.djf, + rates.dkk, rates.dop, rates.dzd, rates.egp, rates.ern, rates.etb, rates.eur, rates.fjd, + rates.fkp, rates.gbp, rates.gel, rates.ggp, rates.ghs, rates.gip, rates.gmd, rates.gnf, + rates.gtq, rates.gyd, rates.hkd, rates.hnl, rates.hrk, rates.htg, rates.huf, rates.idr, + rates.ils, rates.imp, rates.inr, rates.iqd, rates.irr, rates.isk, rates.jep, rates.jmd, + rates.jod, rates.jpy, rates.kes, rates.kgs, rates.khr, rates.kmf, rates.kpw, rates.krw, + rates.kwd, rates.kyd, rates.kzt, rates.lak, rates.lbp, rates.lkr, rates.lrd, rates.lsl, + rates.lyd, rates.mad, rates.mdl, rates.mga, rates.mkd, rates.mmk, rates.mnt, rates.mop, + rates.mro, rates.mru, rates.mur, rates.mvr, rates.mwk, rates.mxn, rates.myr, rates.mzn, + rates.nad, rates.ngn, rates.nio, rates.nok, rates.npr, rates.nzd, rates.omr, rates.pab, + rates.pen, rates.pgk, rates.php, rates.pkr, rates.pln, rates.pyg, rates.qar, rates.ron, + rates.rsd, rates.rub, rates.rwf, rates.sar, rates.sbd, rates.scr, rates.sdg, rates.sek, + rates.sgd, rates.shp, rates.sll, rates.sos, rates.srd, rates.ssp, rates.std, rates.stn, + rates.svc, rates.syp, rates.szl, rates.thb, rates.tjs, rates.tmt, rates.tnd, rates.top, + rates.`try`, rates.ttd, rates.twd, rates.tzs, rates.uah, rates.ugx, rates.usd, rates.uyu, + rates.uzs, rates.ves, rates.vnd, rates.vuv, rates.wst, rates.xaf, rates.xag, rates.xau, + rates.xcd, rates.xdr, rates.xof, rates.xpd, rates.xpf, rates.xpt, rates.yer, rates.zar, + rates.zmw, rates.zwl +) \ No newline at end of file diff --git a/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/mapper/Rates.kt b/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/mapper/Rates.kt index e4de900f29..31e2b5aaad 100644 --- a/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/mapper/Rates.kt +++ b/common/src/commonMain/kotlin/com/github/mustafaozhan/ccc/common/mapper/Rates.kt @@ -2,7 +2,6 @@ package com.github.mustafaozhan.ccc.common.mapper import com.github.mustafaozhan.ccc.common.entity.RatesEntity import com.github.mustafaozhan.ccc.common.model.Rates -import com.github.mustafaozhan.ccc.common.db.sql.Offline_rates as OfflineRates internal fun RatesEntity.toModel() = Rates( base, date, aed, afn, all, amd, ang, aoa, ars, aud, awg, azn, bam, bbd, bdt, bgn, bhd, bif, @@ -16,16 +15,3 @@ internal fun RatesEntity.toModel() = Rates( tnd, top, `try`, ttd, twd, tzs, uah, ugx, usd, uyu, uzs, ves, vnd, vuv, wst, xaf, xag, xau, xcd, xdr, xof, xpd, xpf, xpt, yer, zar, zmw, zwl ) - -internal fun Rates.toOfflineRates() = OfflineRates( - base, date, aed, afn, all, amd, ang, aoa, ars, aud, awg, azn, bam, bbd, bdt, bgn, bhd, bif, - bmd, bnd, bob, brl, bsd, btc, btn, bwp, byn, bzd, cad, cdf, chf, clf, clp, cnh, cny, cop, - crc, cuc, cup, cve, czk, djf, dkk, dop, dzd, egp, ern, etb, eur, fjd, fkp, gbp, gel, ggp, - ghs, gip, gmd, gnf, gtq, gyd, hkd, hnl, hrk, htg, huf, idr, ils, imp, inr, iqd, irr, isk, - jep, jmd, jod, jpy, kes, kgs, khr, kmf, kpw, krw, kwd, kyd, kzt, lak, lbp, lkr, lrd, lsl, - lyd, mad, mdl, mga, mkd, mmk, mnt, mop, mro, mru, mur, mvr, mwk, mxn, myr, mzn, nad, ngn, - nio, nok, npr, nzd, omr, pab, pen, pgk, php, pkr, pln, pyg, qar, ron, rsd, rub, rwf, sar, - sbd, scr, sdg, sek, sgd, shp, sll, sos, srd, ssp, std, stn, svc, syp, szl, thb, tjs, tmt, - tnd, top, `try`, ttd, twd, tzs, uah, ugx, usd, uyu, uzs, ves, vnd, vuv, wst, xaf, xag, xau, - xcd, xdr, xof, xpd, xpf, xpt, yer, zar, zmw, zwl -)