-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from msnodeve/feature/search
feat: Search 깃허브 아이디 room db 사용
- Loading branch information
Showing
9 changed files
with
245 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
app/src/androidTest/java/com/seok/gfd/room/dao/SearchGithubIdDaoTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.seok.gfd.room.dao | ||
|
||
import androidx.room.Room | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.seok.gfd.room.AppDatabase | ||
import com.seok.gfd.room.entity.SearchGithubId | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.After | ||
import org.junit.Before | ||
|
||
import org.junit.Assert.* | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class SearchGithubIdDaoTest { | ||
private lateinit var db : AppDatabase | ||
|
||
@Before | ||
fun createDb() { | ||
val context = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.seok.gfd", context.packageName) | ||
|
||
db = Room.inMemoryDatabaseBuilder( | ||
context, | ||
AppDatabase::class.java | ||
).build() | ||
} | ||
|
||
@After | ||
fun closeDb() = runBlocking { | ||
db.searchGithubIdDao().deleteAll() | ||
db.close() | ||
} | ||
|
||
@Test | ||
fun iWantToKnowTheDatabaseIsFind() = runBlocking{ | ||
val searchGithubId = SearchGithubId(gidName = "github") | ||
|
||
db.searchGithubIdDao().insert(searchGithubId) | ||
var entity = db.searchGithubIdDao().selectAll("g")[0] | ||
assertHabitEquals(searchGithubId, entity) | ||
|
||
db.searchGithubIdDao().delete(entity) | ||
assertEquals(0, db.searchGithubIdDao().selectAll("g").size) | ||
} | ||
|
||
private fun assertHabitEquals(expected: SearchGithubId, actual: SearchGithubId) { | ||
// id 는 자동생성되므로, 검증을 위해서 id의 동일성은 무시 | ||
assertEquals(expected.copy(), actual.copy()) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.seok.gfd.room | ||
|
||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.room.TypeConverters | ||
import com.seok.gfd.room.converter.DateConverter | ||
import com.seok.gfd.room.dao.SearchGithubIdDao | ||
import com.seok.gfd.room.entity.SearchGithubId | ||
|
||
@Database(entities = [SearchGithubId::class], version = 2) | ||
@TypeConverters(DateConverter::class) | ||
abstract class AppDatabase : RoomDatabase() { | ||
abstract fun searchGithubIdDao(): SearchGithubIdDao | ||
|
||
companion object { | ||
private var INSTANCE: AppDatabase? = null | ||
|
||
fun getInstance(context: Context): AppDatabase { | ||
if (INSTANCE == null) { | ||
synchronized(AppDatabase::class) { | ||
INSTANCE = Room.databaseBuilder( | ||
context.applicationContext, | ||
AppDatabase::class.java, "cat.db" | ||
) | ||
.fallbackToDestructiveMigration() | ||
.build() | ||
} | ||
} | ||
return INSTANCE!! | ||
} | ||
|
||
fun destroyInstance(){ | ||
INSTANCE = null | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/seok/gfd/room/converter/DateConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.seok.gfd.room.converter | ||
|
||
import androidx.room.TypeConverter | ||
import java.util.* | ||
|
||
class DateConverter { | ||
@TypeConverter | ||
fun fromTimestamp(value: Long?): Date? { | ||
return value?.let { Date(it) } | ||
} | ||
|
||
@TypeConverter | ||
fun dateToTimestamp(date: Date?): Long? { | ||
return date?.time?.toLong() | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/seok/gfd/room/dao/SearchGithubIdDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.seok.gfd.room.dao | ||
|
||
import androidx.room.* | ||
import com.seok.gfd.room.entity.SearchGithubId | ||
|
||
private const val TABLE_NAME = "search_github_ids" | ||
|
||
@Dao | ||
interface SearchGithubIdDao { | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insert(searchGithubId: SearchGithubId) : Long | ||
|
||
@Delete | ||
suspend fun delete(searchGithubId: SearchGithubId) | ||
|
||
@Query("SELECT * FROM $TABLE_NAME WHERE gid_name LIKE :gidName || '%' ORDER BY created DESC") | ||
suspend fun selectAll(gidName : String) : List<SearchGithubId> | ||
|
||
@Query("delete from $TABLE_NAME") | ||
suspend fun deleteAll() |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/seok/gfd/room/entity/SearchGithubId.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.seok.gfd.room.entity | ||
|
||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import java.util.* | ||
|
||
private const val TABLE_NAME = "search_github_ids" | ||
|
||
@Entity(tableName = TABLE_NAME) | ||
data class SearchGithubId( | ||
@ColumnInfo(name = "gid_name") val gidName: String? | ||
) { | ||
@PrimaryKey(autoGenerate = true) | ||
var gid: Int = 0 | ||
@ColumnInfo(name = "created", defaultValue = "CURRENT_TIMESTAMP") | ||
var created: Date? = Date() | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/seok/gfd/viewmodel/GithubIdViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.seok.gfd.viewmodel | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.AndroidViewModel | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.room.Room | ||
import com.seok.gfd.room.AppDatabase | ||
import com.seok.gfd.room.entity.SearchGithubId | ||
import kotlinx.coroutines.runBlocking | ||
|
||
class GithubIdViewModel(val context: Application) : AndroidViewModel(context){ | ||
private val TAG = this.javaClass.toString() | ||
|
||
private val _githubIds = MutableLiveData<List<SearchGithubId>>() | ||
|
||
val githubIds : LiveData<List<SearchGithubId>> | ||
get() = _githubIds | ||
|
||
fun insertGithubId(githubId: SearchGithubId){ | ||
val database = AppDatabase.getInstance(context) | ||
runBlocking { | ||
database.searchGithubIdDao().insert(githubId) | ||
} | ||
} | ||
|
||
fun getGithubId(name : String){ | ||
val database = AppDatabase.getInstance(context) | ||
runBlocking { | ||
_githubIds.value = database.searchGithubIdDao().selectAll(name) | ||
database.close() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters