Skip to content

Commit

Permalink
Update local resource
Browse files Browse the repository at this point in the history
  • Loading branch information
muhxdan committed Dec 25, 2023
1 parent 86cc781 commit dedae3b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package com.salt.apps.moov.data.source.local

import com.salt.apps.moov.data.source.local.entity.MoovEntity
import com.salt.apps.moov.data.source.local.room.MoovDao
import com.salt.apps.moov.data.source.local.entity.MovieEntity
import com.salt.apps.moov.data.source.local.room.MovieDao
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class LocalDataSource @Inject constructor(
private val moovDao: MoovDao
private val movieDao: MovieDao
) {
suspend fun insertMoovs(moovs: List<MoovEntity>) = moovDao.insertAllMoov(moovs)
suspend fun insertMovies(movies: List<MovieEntity>) = movieDao.insertMovies(movies)

// suspend fun insertMovie(moovs: MovieEntity) = movieDao.insertMovie(moovs)
fun getMovies(type: String) = movieDao.getMovies(type)

fun getMoovs(type: String) = moovDao.getAllMoov(type)
fun getFavoriteMovies() = movieDao.getFavoriteMovies()

// fun getFavoriteMoovs() = moovDao.getFavoriteMoov()
fun getMovieById(moovId: Int) = movieDao.getMovieById(moovId)

// fun isFavoriteMoov(movieId: Int) = moovDao.isFavoriteMovie(movieId)

// suspend fun updateMoov(movieId: Int, isFavorite: Boolean) = moovDao.updateMoovById(movieId, isFavorite)
suspend fun updateMovieById(movieId: Int, isFavorite: Boolean) =
movieDao.updateMovieById(movieId, isFavorite)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ package com.salt.apps.moov.data.source.local.entity
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "moov_entity")
data class MoovEntity(
@Entity(tableName = "moov")
data class MovieEntity(
@PrimaryKey
var id: Int,
val overview: String? = null,
var backdropPath: String? = null,
var posterPath: String? = null,
var originalLanguage: String,
var releaseDate: String? = null,
var voteCount: Int? = null,
var voteAverage: Double? = null,
var moovType: String? = null,
var genreIds: List<Int?>,
var movieType: String? = null,
var title: String,
var isFavorite: Boolean = false
)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.salt.apps.moov.data.source.local.room

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.salt.apps.moov.data.source.local.entity.MovieEntity
import kotlinx.coroutines.flow.Flow

@Dao
interface MovieDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertMovies(movies: List<MovieEntity>)

@Query("SELECT * FROM moov WHERE movieType = :movieType")
fun getMovies(movieType: String): Flow<List<MovieEntity>>

@Query("SELECT * FROM moov WHERE id=:movieId ORDER BY id")
fun getMovieById(movieId: Int): Flow<MovieEntity>

@Query("UPDATE moov SET isFavorite = :isFavorite WHERE id = :movieId")
suspend fun updateMovieById(movieId: Int, isFavorite: Boolean)

@Query("SELECT * FROM moov WHERE isFavorite = 1")
fun getFavoriteMovies(): Flow<List<MovieEntity>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.salt.apps.moov.data.source.local.room

import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverter
import androidx.room.TypeConverters
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.salt.apps.moov.data.source.local.entity.MovieEntity
import com.salt.apps.moov.utilities.Constants.DB_VERSION

@Database(
entities = [MovieEntity::class],
version = DB_VERSION,
exportSchema = false
)

@TypeConverters(Converters::class)
abstract class MovieDatabase : RoomDatabase() {
abstract fun movieDao(): MovieDao
}

class Converters {

@TypeConverter
fun fromString(value: String?): List<Int>? {
if (value == null) {
return emptyList()
}
val listType = object : TypeToken<List<Int>>() {}.type
return Gson().fromJson(value, listType)
}

@TypeConverter
fun toString(list: List<Int>?): String {
return Gson().toJson(list)
}
}

0 comments on commit dedae3b

Please sign in to comment.