Skip to content

Commit

Permalink
Updated base movie insertion
Browse files Browse the repository at this point in the history
Change aims to keep localized resources
  • Loading branch information
diareuse committed Nov 20, 2023
1 parent 95a7cfb commit ae509e4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
16 changes: 11 additions & 5 deletions feature-core-database/src/main/java/movie/core/db/dao/DaoBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ interface DaoBase<T> {
@Update
suspend fun update(model: T)

suspend fun insertOrUpdate(model: T) {
try {
insert(model)
} catch (e: Throwable) {
update(model)
suspend fun insertOrUpdate(model: T) = insertOrElse(model) {
update(model)
}

companion object {
suspend inline fun <T> DaoBase<T>.insertOrElse(model: T, otherwise: (T) -> Unit) {
try {
insert(model)
} catch (e: Throwable) {
otherwise(model)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package movie.core
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import movie.core.adapter.asStored
import movie.core.db.dao.DaoBase.Companion.insertOrElse
import movie.core.db.dao.MovieDao
import movie.core.db.dao.MovieDetailDao
import movie.core.db.dao.MovieMediaDao
Expand All @@ -26,7 +27,7 @@ class EventDetailFeatureStoring(
}

private suspend fun store(model: MovieDetail) {
movie.insertOrUpdate((model as Movie).asStored())
movie.insertOrElse((model as Movie).asStored()) {}
detail.insertOrUpdate(model.asStored())
for (item in model.media)
media.insertOrUpdate(item.asStored(model))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package movie.core
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import movie.core.adapter.asStored
import movie.core.db.dao.DaoBase.Companion.insertOrElse
import movie.core.db.dao.MovieDao
import movie.core.db.dao.MovieReferenceDao
import movie.core.db.dao.ShowingDao
Expand All @@ -20,7 +21,7 @@ class EventShowingsFeatureCinemaStoring(
override suspend fun get(date: Date) = origin.get(date).onSuccess {
scope.launch {
for ((movie, showings) in it) {
movieDao.insertOrUpdate((movie as Movie).asStored())
movieDao.insertOrElse((movie as Movie).asStored()) {}
referenceDao.insertOrUpdate(movie.asStored())
for (showing in showings)
showingDao.insertOrUpdate(showing.asStored(movie))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class EventDetailFeatureTest {
val testData = service_responds_success()
feature(this).get(item)
awaitChildJobCompletion()
verify(movie, times(1)).insertOrUpdate(any())
verify(movie, times(1)).insert(any())
verify(detail, times(1)).insertOrUpdate(any())
verify(media, times(testData.media.size)).insertOrUpdate(any())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class EventShowingsFeatureCinemaTest {
val testData = cinemaEvents_responds_success()
feature(this).get(Date()).getOrThrow()
awaitChildJobCompletion()
verify(movie, times(testData.movies.size)).insertOrUpdate(any())
verify(movie, times(testData.movies.size)).insert(any())
verify(reference, times(testData.movies.size)).insertOrUpdate(any())
verify(showing, times(testData.events.size)).insertOrUpdate(any())
}
Expand Down

0 comments on commit ae509e4

Please sign in to comment.