Skip to content

Commit

Permalink
Implement CinemaxDatabase and UpcomingMovie (#60)
Browse files Browse the repository at this point in the history
* Add Constants

* Add UpcomingMovieEntity

* Add UpcomingMovieRemoteKeyEntity

* Add UpcomingMovieDao

* Add UpcomingMovieRemoteKeyDao

* Add Converters

* Implement CinemaxDatabase
  • Loading branch information
AfigAliyev committed Aug 12, 2022
1 parent b2ba2d9 commit 5d2dde3
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ object Constants {
const val API_KEY_QUERY_PARAM = "api_key"
}

object Tables {
const val UPCOMING_MOVIES_TABLE = "upcoming_movies"
const val UPCOMING_MOVIES_REMOTE_KEYS_TABLE = "upcoming_movies_remote_keys"
}

object Fields {
const val ID = "id"
const val REMOTE_ID = "remote_id"

const val PAGE = "page"
const val TOTAL_PAGES = "total_pages"
Expand All @@ -46,6 +52,9 @@ object Constants {
const val VIDEO = "video"
const val MAXIMUM = "maximum"
const val MINIMUM = "minimum"

const val PREV_PAGE = "prev_page"
const val NEXT_PAGE = "next_page"
}

object Messages {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2022 Maximillian Leonov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.maximillianleonov.cinemax.data.local.dao.upcoming

import androidx.paging.PagingSource
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.maximillianleonov.cinemax.core.data.local.common.ContentDao
import com.maximillianleonov.cinemax.core.data.util.Constants.Tables.UPCOMING_MOVIES_TABLE
import com.maximillianleonov.cinemax.data.local.entity.upcoming.UpcomingMovieEntity
import kotlinx.coroutines.flow.Flow

@Dao
interface UpcomingMovieDao : ContentDao<UpcomingMovieEntity> {
@Query("SELECT * FROM $UPCOMING_MOVIES_TABLE")
override fun getAll(): Flow<List<UpcomingMovieEntity>>

@Query("SELECT * FROM $UPCOMING_MOVIES_TABLE")
override fun getAllPaging(): PagingSource<Int, UpcomingMovieEntity>

@Insert(onConflict = OnConflictStrategy.REPLACE)
override suspend fun insertAll(entities: List<UpcomingMovieEntity>)

@Query("DELETE FROM $UPCOMING_MOVIES_TABLE")
override suspend fun deleteAll()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2022 Maximillian Leonov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.maximillianleonov.cinemax.data.local.dao.upcoming

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.maximillianleonov.cinemax.core.data.local.common.RemoteKeyDao
import com.maximillianleonov.cinemax.core.data.util.Constants.Tables.UPCOMING_MOVIES_REMOTE_KEYS_TABLE
import com.maximillianleonov.cinemax.data.local.entity.upcoming.UpcomingMovieRemoteKeyEntity

@Dao
interface UpcomingMovieRemoteKeyDao : RemoteKeyDao<UpcomingMovieRemoteKeyEntity> {
@Query("SELECT * FROM $UPCOMING_MOVIES_REMOTE_KEYS_TABLE WHERE id = :id")
override suspend fun getById(id: Int): UpcomingMovieRemoteKeyEntity

@Insert(onConflict = OnConflictStrategy.REPLACE)
override suspend fun insertAll(entities: List<UpcomingMovieRemoteKeyEntity>)

@Query("DELETE FROM $UPCOMING_MOVIES_REMOTE_KEYS_TABLE")
override suspend fun deleteAll()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2022 Maximillian Leonov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.maximillianleonov.cinemax.data.local.db

import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.maximillianleonov.cinemax.data.local.dao.upcoming.UpcomingMovieDao
import com.maximillianleonov.cinemax.data.local.dao.upcoming.UpcomingMovieRemoteKeyDao
import com.maximillianleonov.cinemax.data.local.entity.upcoming.UpcomingMovieEntity
import com.maximillianleonov.cinemax.data.local.entity.upcoming.UpcomingMovieRemoteKeyEntity

@Database(
entities = [
UpcomingMovieEntity::class, UpcomingMovieRemoteKeyEntity::class
],
version = 1,
exportSchema = false
)
@TypeConverters(Converters::class)
abstract class CinemaxDatabase : RoomDatabase() {
abstract val upcomingMovieDao: UpcomingMovieDao
abstract val upcomingMovieRemoteKeyDao: UpcomingMovieRemoteKeyDao

companion object {
const val CINEMAX_DATABASE = "cinemax.db"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2022 Maximillian Leonov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.maximillianleonov.cinemax.data.local.db

import androidx.room.TypeConverter
import com.maximillianleonov.cinemax.core.data.remote.common.JsonParser
import com.maximillianleonov.cinemax.core.data.remote.serializer.LocalDateSerializer
import kotlinx.datetime.LocalDate

object Converters {
@TypeConverter
fun localDateToJson(value: LocalDate): String = JsonParser.toJson(LocalDateSerializer(), value)

@TypeConverter
fun jsonToLocalDate(json: String): LocalDate = JsonParser.fromJson(LocalDateSerializer(), json)

@TypeConverter
fun intListToJson(value: List<Int>): String = JsonParser.toJson(value)

@TypeConverter
fun jsonToIntList(json: String): List<Int> = JsonParser.fromJson(json)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2022 Maximillian Leonov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.maximillianleonov.cinemax.data.local.entity.upcoming

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.maximillianleonov.cinemax.core.data.local.common.MovieEntity
import com.maximillianleonov.cinemax.core.data.util.Constants
import kotlinx.datetime.LocalDate

@Entity(tableName = Constants.Tables.UPCOMING_MOVIES_TABLE)
data class UpcomingMovieEntity(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = Constants.Fields.ID)
override val id: Int = 0,

@ColumnInfo(name = Constants.Fields.REMOTE_ID)
override val remoteId: Int,

@ColumnInfo(name = Constants.Fields.TITLE)
override val title: String,

@ColumnInfo(name = Constants.Fields.OVERVIEW)
override val overview: String,

@ColumnInfo(name = Constants.Fields.POPULARITY)
override val popularity: Double,

@ColumnInfo(name = Constants.Fields.RELEASE_DATE)
override val releaseDate: LocalDate,

@ColumnInfo(name = Constants.Fields.ADULT)
override val adult: Boolean,

@ColumnInfo(name = Constants.Fields.GENRE_IDS)
override val genreIds: List<Int>,

@ColumnInfo(name = Constants.Fields.ORIGINAL_TITLE)
override val originalTitle: String,

@ColumnInfo(name = Constants.Fields.ORIGINAL_LANGUAGE)
override val originalLanguage: String,

@ColumnInfo(name = Constants.Fields.VOTE_AVERAGE)
override val voteAverage: Double,

@ColumnInfo(name = Constants.Fields.VOTE_COUNT)
override val voteCount: Int,

@ColumnInfo(name = Constants.Fields.POSTER_PATH)
override val posterPath: String?,

@ColumnInfo(name = Constants.Fields.BACKDROP_PATH)
override val backdropPath: String?,

@ColumnInfo(name = Constants.Fields.VIDEO)
override val video: Boolean
) : MovieEntity
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2022 Maximillian Leonov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.maximillianleonov.cinemax.data.local.entity.upcoming

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.maximillianleonov.cinemax.core.data.local.common.RemoteKeyEntity
import com.maximillianleonov.cinemax.core.data.util.Constants

@Entity(tableName = Constants.Tables.UPCOMING_MOVIES_REMOTE_KEYS_TABLE)
data class UpcomingMovieRemoteKeyEntity(
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = Constants.Fields.ID)
override val id: Int,

@ColumnInfo(name = Constants.Fields.PREV_PAGE)
override val prevPage: Int?,

@ColumnInfo(name = Constants.Fields.NEXT_PAGE)
override val nextPage: Int?
) : RemoteKeyEntity

0 comments on commit 5d2dde3

Please sign in to comment.