diff --git a/app/src/main/kotlin/com/omricat/maplibrarian/chartlist/FirebaseChartsRepository.kt b/app/src/main/kotlin/com/omricat/maplibrarian/chartlist/FirebaseChartsRepository.kt index af012844..053c92e8 100644 --- a/app/src/main/kotlin/com/omricat/maplibrarian/chartlist/FirebaseChartsRepository.kt +++ b/app/src/main/kotlin/com/omricat/maplibrarian/chartlist/FirebaseChartsRepository.kt @@ -4,6 +4,7 @@ import co.touchlab.kermit.Severity.Warn import com.github.michaelbull.result.Result import com.github.michaelbull.result.andThen import com.github.michaelbull.result.combine +import com.github.michaelbull.result.flatMap import com.github.michaelbull.result.map import com.github.michaelbull.result.mapError import com.github.michaelbull.result.onFailure @@ -30,6 +31,7 @@ import com.omricat.maplibrarian.chartlist.ChartsRepository.AddNewChartError.Unav import com.omricat.maplibrarian.chartlist.ChartsRepository.Error.ExceptionWrappingError import com.omricat.maplibrarian.chartlist.ChartsRepository.Error.MessageError import com.omricat.maplibrarian.chartlist.ChartsRepository.SaveEditedChartError +import com.omricat.maplibrarian.chartlist.ChartsRepository.SaveEditedChartError.FailureGettingSavedChartError import com.omricat.maplibrarian.model.ChartId import com.omricat.maplibrarian.model.ChartModel import com.omricat.maplibrarian.model.DbChartModel @@ -92,13 +94,29 @@ class FirebaseChartsRepository( .map { ref -> newChart.withChartId(ChartId(ref.id)) } } - override fun saveEditedChart( + override suspend fun saveEditedChart( user: User, chartId: ChartId, model: ChartModel - ): Result { - TODO("not implemented") - } + ): Result = + withContext(dispatchers.io) { + runCatchingFirestoreException { + val chartRef = db.mapsCollection(user).document(chartId.id) + chartRef.update(ChartModelToMapSerializer.serializeToMap(model)).await() + chartRef.get().await() + } + } + .logAndMapException { exception -> + when (exception.code) { + else -> SaveEditedChartError.OtherException(exception) + } + } + .onFailure { log(Warn) { "error saving changed chart: ${it.message}" } } + .flatMap { documentSnapshot -> + documentSnapshot.parseMapModel().mapError { + FailureGettingSavedChartError("DeserializationError: ${it.message}") + } + } private fun FirebaseFirestore.mapsCollection(user: User) = collection("users").document(user.id.value).collection("maps") diff --git a/core/src/main/kotlin/com/omricat/maplibrarian/chartlist/ChartsRepository.kt b/core/src/main/kotlin/com/omricat/maplibrarian/chartlist/ChartsRepository.kt index d4a7bb61..989f5c7f 100644 --- a/core/src/main/kotlin/com/omricat/maplibrarian/chartlist/ChartsRepository.kt +++ b/core/src/main/kotlin/com/omricat/maplibrarian/chartlist/ChartsRepository.kt @@ -17,7 +17,7 @@ public interface ChartsRepository { newChart: UnsavedChartModel ): Result - public fun saveEditedChart( + public suspend fun saveEditedChart( user: User, chartId: ChartId, model: ChartModel @@ -34,7 +34,13 @@ public interface ChartsRepository { } } - public sealed class SaveEditedChartError + public sealed class SaveEditedChartError(public val message: String) { + + public class FailureGettingSavedChartError(message: String) : SaveEditedChartError(message) + + public data class OtherException(val exception: Throwable) : + SaveEditedChartError(exception.message ?: "No message in $exception") + } public sealed class AddNewChartError(public val message: String) { public data object Unavailable : AddNewChartError("Service temporarily unavailable") diff --git a/core/src/test/kotlin/com/omricat/maplibrarian/chartlist/ChartDetailsWorkflowTest.kt b/core/src/test/kotlin/com/omricat/maplibrarian/chartlist/ChartDetailsWorkflowTest.kt index 101c77b4..ade66dba 100644 --- a/core/src/test/kotlin/com/omricat/maplibrarian/chartlist/ChartDetailsWorkflowTest.kt +++ b/core/src/test/kotlin/com/omricat/maplibrarian/chartlist/ChartDetailsWorkflowTest.kt @@ -6,7 +6,6 @@ import assertk.tableOf import com.omricat.maplibrarian.chartlist.ChartDetailsWorkflowImpl.State import com.omricat.maplibrarian.model.ChartId import com.omricat.maplibrarian.model.DbChartModel -import com.omricat.maplibrarian.model.UserUid import kotlinx.serialization.json.Json import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test @@ -19,7 +18,7 @@ class ChartDetailsWorkflowTest { inner class StateSnapshotterTest { @Test fun `Snapshot round trips are the identity`() { - val chart = DbChartModel(UserUid("uid"), "title", ChartId("chartId")) + val chart = DbChartModel("title", ChartId("chartId")) tableOf("state") .row(State.ShowingDetails(chart)) .row(State.EditingChart(chart)) diff --git a/core/src/test/kotlin/com/omricat/maplibrarian/chartlist/ChartsWorkflowTest.kt b/core/src/test/kotlin/com/omricat/maplibrarian/chartlist/ChartsWorkflowTest.kt index cd68784e..3cfa16ea 100644 --- a/core/src/test/kotlin/com/omricat/maplibrarian/chartlist/ChartsWorkflowTest.kt +++ b/core/src/test/kotlin/com/omricat/maplibrarian/chartlist/ChartsWorkflowTest.kt @@ -27,11 +27,7 @@ internal class ChartsWorkflowTest { listOf(DbChartModel("title", ChartId("chart"))) ) ) - .row( - ChartsWorkflowState.ShowingDetails( - DbChartModel(UserUid("userid"), "title", ChartId("chartid")) - ) - ) + .row(ChartsWorkflowState.ShowingDetails(DbChartModel("title", ChartId("chartid")))) .forAll { state -> assertThat(snapshotter.valueFromSnapshot(snapshotter.snapshotOf(state))) .isEqualTo(state) diff --git a/integration-tests/firebase/src/main/kotlin/com/omricat/maplibrarian/firebase/charts/FirebaseChartsRepositoryTest.kt b/integration-tests/firebase/src/main/kotlin/com/omricat/maplibrarian/firebase/charts/FirebaseChartsRepositoryTest.kt index 3eb88327..7f9754a2 100644 --- a/integration-tests/firebase/src/main/kotlin/com/omricat/maplibrarian/firebase/charts/FirebaseChartsRepositoryTest.kt +++ b/integration-tests/firebase/src/main/kotlin/com/omricat/maplibrarian/firebase/charts/FirebaseChartsRepositoryTest.kt @@ -1,13 +1,12 @@ package com.omricat.maplibrarian.firebase.charts import assertk.assertThat -import com.github.michaelbull.result.Result +import assertk.assertions.isEqualTo import com.github.michaelbull.result.get import com.github.michaelbull.result.unwrap import com.omricat.logging.test.TestLogger import com.omricat.maplibrarian.auth.EmailPasswordCredential import com.omricat.maplibrarian.auth.FirebaseUserRepository -import com.omricat.maplibrarian.chartlist.ChartsRepository import com.omricat.maplibrarian.chartlist.FirebaseChartsRepository import com.omricat.maplibrarian.firebase.TestDispatcherProvider import com.omricat.maplibrarian.firebase.TestFixtures @@ -51,14 +50,16 @@ class FirebaseChartsRepositoryTest { val createdChart: DbChartModel = addChartResult.unwrap() - val editChartResult: Result = + val newTitle = "A different title" + val editChartResult = chartsRepository.saveEditedChart( user, createdChart.chartId, - UnsavedChartModel("A different title") + UnsavedChartModel(newTitle) ) - assertThat(editChartResult).isOk() + val expectedPostEdit = DbChartModel(newTitle, createdChart.chartId) + assertThat(editChartResult).isOk().isEqualTo(expectedPostEdit) } companion object {