Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kibettheophilus committed May 31, 2024
1 parent d801888 commit 60e3d77
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CaloreeDatabase> { 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<CaloreeDatabase>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CaloreeEntity>)

/**
* retreives all the available calories from the db
*/
@Query("SELECT * FROM caloree_table")
suspend fun getCalories(): List<CaloreeEntity>

/**
* 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<String>): List<CaloreeEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CaloreeDatabase> { createDatabase() }
}

/**
* creates the db
*/
fun createDatabase(): CaloreeDatabase {
val dbFilePath = NSHomeDirectory() + "/caloree.db"
return Room.databaseBuilder<CaloreeDatabase>(
Expand Down

0 comments on commit 60e3d77

Please sign in to comment.