diff --git a/data/src/main/kotlin/ac/dnd/bookkeeping/android/data/remote/local/database/BookKeepingDatabase.kt b/data/src/main/kotlin/ac/dnd/bookkeeping/android/data/remote/local/database/BookKeepingDatabase.kt new file mode 100644 index 00000000..2e231217 --- /dev/null +++ b/data/src/main/kotlin/ac/dnd/bookkeeping/android/data/remote/local/database/BookKeepingDatabase.kt @@ -0,0 +1,15 @@ +package ac.dnd.bookkeeping.android.data.remote.local.database + +import ac.dnd.bookkeeping.android.data.remote.local.database.sample.SampleDao +import ac.dnd.bookkeeping.android.data.remote.local.database.sample.SampleEntity +import androidx.room.Database +import androidx.room.RoomDatabase + +@Database(entities = [SampleEntity::class], version = 1) +abstract class BookKeepingDatabase : RoomDatabase() { + abstract fun sampleDao(): SampleDao + + companion object { + const val DATABASE_NAME = "bookkeeping" + } +} diff --git a/data/src/main/kotlin/ac/dnd/bookkeeping/android/data/remote/local/database/sample/SampleDao.kt b/data/src/main/kotlin/ac/dnd/bookkeeping/android/data/remote/local/database/sample/SampleDao.kt new file mode 100644 index 00000000..1a266ca6 --- /dev/null +++ b/data/src/main/kotlin/ac/dnd/bookkeeping/android/data/remote/local/database/sample/SampleDao.kt @@ -0,0 +1,26 @@ +package ac.dnd.bookkeeping.android.data.remote.local.database.sample + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query +import androidx.room.Update + +@Dao +interface SampleDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + suspend fun insert(vararg users: SampleEntity) + + @Query("SELECT * FROM sample WHERE id = :id LIMIT 1") + suspend fun get(id: Int): SampleEntity + + @Query("SELECT * FROM sample") + suspend fun getAll(): List + + @Update + suspend fun update(vararg users: SampleEntity) + + @Delete + suspend fun delete(vararg users: SampleEntity) +} diff --git a/data/src/main/kotlin/ac/dnd/bookkeeping/android/data/remote/local/database/sample/SampleEntity.kt b/data/src/main/kotlin/ac/dnd/bookkeeping/android/data/remote/local/database/sample/SampleEntity.kt new file mode 100644 index 00000000..6ca4c027 --- /dev/null +++ b/data/src/main/kotlin/ac/dnd/bookkeeping/android/data/remote/local/database/sample/SampleEntity.kt @@ -0,0 +1,12 @@ +package ac.dnd.bookkeeping.android.data.remote.local.database.sample + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.PrimaryKey + +@Entity(tableName = "sample") +data class SampleEntity( + @PrimaryKey val id: Long, + @ColumnInfo(name = "first_name") val firstName: String, + @ColumnInfo(name = "last_name") val lastName: String +) diff --git a/data/src/main/kotlin/ac/dnd/bookkeeping/android/data/remote/local/di/LocalModule.kt b/data/src/main/kotlin/ac/dnd/bookkeeping/android/data/remote/local/di/LocalModule.kt index 0a4e81a9..00a21389 100644 --- a/data/src/main/kotlin/ac/dnd/bookkeeping/android/data/remote/local/di/LocalModule.kt +++ b/data/src/main/kotlin/ac/dnd/bookkeeping/android/data/remote/local/di/LocalModule.kt @@ -1,7 +1,10 @@ package ac.dnd.bookkeeping.android.data.remote.local.di -import android.content.Context import ac.dnd.bookkeeping.android.data.remote.local.SharedPreferencesManager +import ac.dnd.bookkeeping.android.data.remote.local.database.BookKeepingDatabase +import ac.dnd.bookkeeping.android.data.remote.local.database.sample.SampleDao +import android.content.Context +import androidx.room.Room import dagger.Module import dagger.Provides import dagger.hilt.InstallIn @@ -20,4 +23,24 @@ internal object LocalModule { ): SharedPreferencesManager { return SharedPreferencesManager(context) } + + @Provides + @Singleton + fun provideBookKeepingDatabase( + @ApplicationContext context: Context + ): BookKeepingDatabase { + return Room.databaseBuilder( + context, + BookKeepingDatabase::class.java, + BookKeepingDatabase.DATABASE_NAME + ).fallbackToDestructiveMigration().build() + } + + @Provides + @Singleton + fun provideSampleDao( + bookKeepingDatabase: BookKeepingDatabase + ): SampleDao { + return bookKeepingDatabase.sampleDao() + } }