diff --git a/core/local/src/androidMain/kotlin/com/theophiluskibet/caloree/local/di/LocalModule.kt b/core/local/src/androidMain/kotlin/com/theophiluskibet/caloree/local/di/LocalModule.kt index ad35713..ef1a83e 100644 --- a/core/local/src/androidMain/kotlin/com/theophiluskibet/caloree/local/di/LocalModule.kt +++ b/core/local/src/androidMain/kotlin/com/theophiluskibet/caloree/local/di/LocalModule.kt @@ -7,11 +7,18 @@ import com.theophiluskibet.caloree.local.database.CaloreeDatabase import kotlinx.coroutines.Dispatchers import org.koin.dsl.module +/** + * provides an instance of db implementation in android + */ actual fun localModule() = module { single { createDatabase(context = get()) } } +/** + * creates the db + * @param context current state of the application + */ fun createDatabase(context: Context): CaloreeDatabase { val dbFile = context.getDatabasePath("caloree.db") return Room.databaseBuilder( diff --git a/core/local/src/commonMain/kotlin/com/theophiluskibet/caloree/local/dao/CaloreeDao.kt b/core/local/src/commonMain/kotlin/com/theophiluskibet/caloree/local/dao/CaloreeDao.kt index 3c8504c..5f69ec4 100644 --- a/core/local/src/commonMain/kotlin/com/theophiluskibet/caloree/local/dao/CaloreeDao.kt +++ b/core/local/src/commonMain/kotlin/com/theophiluskibet/caloree/local/dao/CaloreeDao.kt @@ -6,17 +6,39 @@ import androidx.room.OnConflictStrategy import androidx.room.Query import com.theophiluskibet.caloree.local.entities.CaloreeEntity +/** + * api to provide functionality to access the database + */ @Dao interface CaloreeDao { + /** + * adds a list of calories to db + */ @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun saveCalories(list: List) + /** + * retreives all the available calories from the db + */ @Query("SELECT * FROM caloree_table") suspend fun getCalories(): List + /** + * retreives a single calorie matching provided name from the db + */ @Query("SELECT * FROM caloree_table WHERE name=:name") suspend fun getCalory(name: String): CaloreeEntity + /** + * retreives a list of calories matching the provided names from the getCaloriesByNames + * @sample + * ``` + * val names = listOf("rice","banana") + * + * getCalories(names) + * + * ``` + */ @Query("SELECT * FROM caloree_table WHERE name IN(:names)") suspend fun getCaloriesByNames(names: List): List } diff --git a/core/local/src/commonMain/kotlin/com/theophiluskibet/caloree/local/database/CaloreeDatabase.kt b/core/local/src/commonMain/kotlin/com/theophiluskibet/caloree/local/database/CaloreeDatabase.kt index 65b1f06..803e521 100644 --- a/core/local/src/commonMain/kotlin/com/theophiluskibet/caloree/local/database/CaloreeDatabase.kt +++ b/core/local/src/commonMain/kotlin/com/theophiluskibet/caloree/local/database/CaloreeDatabase.kt @@ -5,6 +5,11 @@ import androidx.room.RoomDatabase import com.theophiluskibet.caloree.local.dao.CaloreeDao import com.theophiluskibet.caloree.local.entities.CaloreeEntity +/** + * used to create the database + * @param entities list of available tables in the app + * @param version current version of the db + */ @Database(entities = [CaloreeEntity::class], version = 1) abstract class CaloreeDatabase : RoomDatabase() { abstract fun caloreeDao(): CaloreeDao diff --git a/core/local/src/commonMain/kotlin/com/theophiluskibet/caloree/local/entities/CaloreeEntity.kt b/core/local/src/commonMain/kotlin/com/theophiluskibet/caloree/local/entities/CaloreeEntity.kt index 59b1ce1..620306c 100644 --- a/core/local/src/commonMain/kotlin/com/theophiluskibet/caloree/local/entities/CaloreeEntity.kt +++ b/core/local/src/commonMain/kotlin/com/theophiluskibet/caloree/local/entities/CaloreeEntity.kt @@ -3,6 +3,9 @@ package com.theophiluskibet.caloree.local.entities import androidx.room.Entity import androidx.room.PrimaryKey +/** + * represents a single calorie in the db + */ @Entity(tableName = "caloree_table") data class CaloreeEntity( @PrimaryKey(autoGenerate = false) diff --git a/core/local/src/iosMain/kotlin/com/theophiluskibet/caloree/local/di/LocalModule.kt b/core/local/src/iosMain/kotlin/com/theophiluskibet/caloree/local/di/LocalModule.kt index fa96a2b..64114f9 100644 --- a/core/local/src/iosMain/kotlin/com/theophiluskibet/caloree/local/di/LocalModule.kt +++ b/core/local/src/iosMain/kotlin/com/theophiluskibet/caloree/local/di/LocalModule.kt @@ -9,11 +9,17 @@ import kotlinx.coroutines.IO import org.koin.dsl.module import platform.Foundation.NSHomeDirectory +/** + * provides an instance of db implementation in ios +*/ actual fun localModule() = module { single { createDatabase() } } +/** + * creates the db + */ fun createDatabase(): CaloreeDatabase { val dbFilePath = NSHomeDirectory() + "/caloree.db" return Room.databaseBuilder(