Skip to content

Commit

Permalink
feat(firebase): Implement saveEditedChart()
Browse files Browse the repository at this point in the history
  • Loading branch information
grodin committed Nov 21, 2024
1 parent 9607d38 commit 101fdf0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<DbChartModel, SaveEditedChartError> {
TODO("not implemented")
}
): Result<DbChartModel, SaveEditedChartError> =
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface ChartsRepository {
newChart: UnsavedChartModel
): Result<DbChartModel, AddNewChartError>

public fun saveEditedChart(
public suspend fun saveEditedChart(
user: User,
chartId: ChartId,
model: ChartModel
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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>(State.ShowingDetails(chart))
.row(State.EditingChart(chart))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -51,14 +50,16 @@ class FirebaseChartsRepositoryTest {

val createdChart: DbChartModel = addChartResult.unwrap()

val editChartResult: Result<DbChartModel, ChartsRepository.SaveEditedChartError> =
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 {
Expand Down

0 comments on commit 101fdf0

Please sign in to comment.