Skip to content

Commit 244a383

Browse files
committed
feat: room db 데이터 삽입, 조회 완료(github id)
1 parent 5ded1e6 commit 244a383

7 files changed

Lines changed: 129 additions & 35 deletions

File tree

app/src/androidTest/java/com/seok/gfd/room/dao/SearchGithubIdDaoTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SearchGithubIdDaoTest {
3636

3737
@Test
3838
fun iWantToKnowTheDatabaseIsFind() = runBlocking{
39-
val searchGithubId = SearchGithubId(gid = 0, gidName = "github")
39+
val searchGithubId = SearchGithubId(gidName = "github")
4040

4141
db.searchGithubIdDao().insert(searchGithubId)
4242
var entity = db.searchGithubIdDao().selectAll("g")[0]
@@ -47,8 +47,8 @@ class SearchGithubIdDaoTest {
4747
}
4848

4949
private fun assertHabitEquals(expected: SearchGithubId, actual: SearchGithubId) {
50-
// id 는 자동생성되므로, 검증을 위해서 id의 동일성은 무시하자.
51-
assertEquals(expected.copy(gid = 0), actual.copy(gid = 0))
50+
// id 는 자동생성되므로, 검증을 위해서 id의 동일성은 무시
51+
assertEquals(expected.copy(), actual.copy())
5252
}
5353

5454
}
Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
package com.seok.gfd.room
22

3+
import android.content.Context
34
import androidx.room.Database
5+
import androidx.room.Room
46
import androidx.room.RoomDatabase
7+
import androidx.room.TypeConverters
8+
import com.seok.gfd.room.converter.DateConverter
59
import com.seok.gfd.room.dao.SearchGithubIdDao
610
import com.seok.gfd.room.entity.SearchGithubId
711

8-
@Database(entities = [SearchGithubId::class], version = 1)
9-
abstract class AppDatabase : RoomDatabase(){
12+
@Database(entities = [SearchGithubId::class], version = 2)
13+
@TypeConverters(DateConverter::class)
14+
abstract class AppDatabase : RoomDatabase() {
1015
abstract fun searchGithubIdDao(): SearchGithubIdDao
16+
17+
companion object {
18+
private var INSTANCE: AppDatabase? = null
19+
20+
fun getInstance(context: Context): AppDatabase {
21+
if (INSTANCE == null) {
22+
synchronized(AppDatabase::class) {
23+
INSTANCE = Room.databaseBuilder(
24+
context.applicationContext,
25+
AppDatabase::class.java, "cat.db"
26+
)
27+
.fallbackToDestructiveMigration()
28+
.build()
29+
}
30+
}
31+
return INSTANCE!!
32+
}
33+
34+
fun destroyInstance(){
35+
INSTANCE = null
36+
}
37+
}
1138
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.seok.gfd.room.converter
2+
3+
import androidx.room.TypeConverter
4+
import java.util.*
5+
6+
class DateConverter {
7+
@TypeConverter
8+
fun fromTimestamp(value: Long?): Date? {
9+
return value?.let { Date(it) }
10+
}
11+
12+
@TypeConverter
13+
fun dateToTimestamp(date: Date?): Long? {
14+
return date?.time?.toLong()
15+
}
16+
}

app/src/main/java/com/seok/gfd/room/dao/SearchGithubIdDao.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ interface SearchGithubIdDao {
1313
@Delete
1414
suspend fun delete(searchGithubId: SearchGithubId)
1515

16-
@Query("SELECT * FROM $TABLE_NAME WHERE gid_name LIKE :gidName || '%'")
16+
@Query("SELECT * FROM $TABLE_NAME WHERE gid_name LIKE :gidName || '%' ORDER BY created DESC")
1717
suspend fun selectAll(gidName : String) : List<SearchGithubId>
1818

1919
@Query("delete from $TABLE_NAME")
2020
suspend fun deleteAll()
21-
}

app/src/main/java/com/seok/gfd/room/entity/SearchGithubId.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package com.seok.gfd.room.entity
33
import androidx.room.ColumnInfo
44
import androidx.room.Entity
55
import androidx.room.PrimaryKey
6+
import java.util.*
67

78
private const val TABLE_NAME = "search_github_ids"
89

910
@Entity(tableName = TABLE_NAME)
1011
data class SearchGithubId(
11-
@PrimaryKey(autoGenerate = true) var gid: Long,
1212
@ColumnInfo(name = "gid_name") val gidName: String?
13-
// @ColumnInfo(name = "created", defaultValue = "CURRENT_TIMESTAMP") val created: Date? = Date()
14-
)
13+
) {
14+
@PrimaryKey(autoGenerate = true)
15+
var gid: Int = 0
16+
@ColumnInfo(name = "created", defaultValue = "CURRENT_TIMESTAMP")
17+
var created: Date? = Date()
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.seok.gfd.viewmodel
2+
3+
import android.app.Application
4+
import androidx.lifecycle.AndroidViewModel
5+
import androidx.lifecycle.LiveData
6+
import androidx.lifecycle.MutableLiveData
7+
import androidx.room.Room
8+
import com.seok.gfd.room.AppDatabase
9+
import com.seok.gfd.room.entity.SearchGithubId
10+
import kotlinx.coroutines.runBlocking
11+
12+
class GithubIdViewModel(val context: Application) : AndroidViewModel(context){
13+
private val TAG = this.javaClass.toString()
14+
15+
private val _githubIds = MutableLiveData<List<SearchGithubId>>()
16+
17+
val githubIds : LiveData<List<SearchGithubId>>
18+
get() = _githubIds
19+
20+
fun insertGithubId(githubId: SearchGithubId){
21+
val database = AppDatabase.getInstance(context)
22+
runBlocking {
23+
database.searchGithubIdDao().insert(githubId)
24+
}
25+
}
26+
27+
fun getGithubId(name : String){
28+
val database = AppDatabase.getInstance(context)
29+
runBlocking {
30+
_githubIds.value = database.searchGithubIdDao().selectAll(name)
31+
database.close()
32+
}
33+
}
34+
}

app/src/main/java/com/seok/gfd/views/SearchActivity.kt

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,37 @@ import android.util.Log
55
import android.view.WindowManager
66
import android.view.animation.AnimationUtils
77
import androidx.appcompat.app.AppCompatActivity
8+
import androidx.lifecycle.Observer
9+
import androidx.lifecycle.ViewModelProviders
810
import androidx.room.Room
911
import com.seok.gfd.R
1012
import com.seok.gfd.room.AppDatabase
1113
import com.seok.gfd.room.entity.SearchGithubId
14+
import com.seok.gfd.viewmodel.GithubIdViewModel
1215
import kotlinx.android.synthetic.main.activity_search.*
1316
import kotlinx.coroutines.runBlocking
17+
import java.util.*
1418

1519
class SearchActivity : AppCompatActivity() {
20+
private lateinit var githubIdsViewModel: GithubIdViewModel
21+
1622
override fun onCreate(savedInstanceState: Bundle?) {
1723
super.onCreate(savedInstanceState)
1824
setContentView(R.layout.activity_search)
1925
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
2026

27+
init()
2128
setAnimation()
29+
}
2230

23-
24-
iWantToKnowTheDatabaseIsFind()
31+
private fun init() {
32+
githubIdsViewModel = ViewModelProviders.of(this).get(GithubIdViewModel::class.java)
33+
githubIdsViewModel.githubIds.observe(this, Observer {
34+
for(a in it){
35+
println("data${a.gid},${a.gidName},${a.created}")
36+
}
37+
})
38+
githubIdsViewModel.getGithubId("t")
2539
}
2640

2741
private fun setAnimation() {
@@ -35,27 +49,27 @@ class SearchActivity : AppCompatActivity() {
3549
search_layout_id.startAnimation(leftToRight)
3650
}
3751

38-
private fun iWantToKnowTheDatabaseIsFind() {
39-
// 테스트 용으로 메모리상 생성
40-
val database = Room.inMemoryDatabaseBuilder(
41-
this,
42-
AppDatabase::class.java
43-
).build()
44-
45-
// id를 0 으로 설정해주어서 id가 autoGeneration 되게 한다.
46-
val test = SearchGithubId(gid = 0, gidName = "test")
47-
runBlocking {
48-
// 습관을 씁니다.
49-
database.searchGithubIdDao().insert(test)
50-
51-
// 실제 DB에 써진 것을 확인합니다.
52-
var dbHabitSchema = database.searchGithubIdDao().selectAll("t")[0]
53-
Log.d(this.javaClass.name, "방금 넣은 것 $dbHabitSchema")
54-
55-
// 지우는 것도 잘 동작하는지 확인해 봅니다.
56-
database.searchGithubIdDao().delete(dbHabitSchema)
57-
58-
Log.d(this.javaClass.name, "방금 지워서 아무것도 없음. ${database.searchGithubIdDao().selectAll("t")}")
59-
}
60-
}
52+
// private fun iWantToKnowTheDatabaseIsFind() {
53+
// // 테스트 용으로 메모리상 생성
54+
// val database = Room.inMemoryDatabaseBuilder(
55+
// this,
56+
// AppDatabase::class.java
57+
// ).build()
58+
//
59+
// // id를 0 으로 설정해주어서 id가 autoGeneration 되게 한다.
60+
// val test = SearchGithubId(gidName = "test")
61+
// runBlocking {
62+
// // 습관을 씁니다.
63+
// database.searchGithubIdDao().insert(test)
64+
//
65+
// // 실제 DB에 써진 것을 확인합니다.
66+
// var dbHabitSchema = database.searchGithubIdDao().selectAll("t")[0]
67+
// Log.d(this.javaClass.name, "방금 넣은 것 $dbHabitSchema")
68+
//
69+
// // 지우는 것도 잘 동작하는지 확인해 봅니다.
70+
// database.searchGithubIdDao().delete(dbHabitSchema)
71+
//
72+
// Log.d(this.javaClass.name, "방금 지워서 아무것도 없음. ${database.searchGithubIdDao().selectAll("t")}")
73+
// }
74+
// }
6175
}

0 commit comments

Comments
 (0)