Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kuring-38 [수정] 서버에서 학과 이름이 바뀌었을 때 로컬 DB에도 반영하도록 수정 #65

Merged
merged 12 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ interface DepartmentDao {
@Query("SELECT * FROM departments WHERE koreanName LIKE '%' || :koreanName || '%'")
suspend fun getDepartmentsByKoreanName(koreanName: String): List<DepartmentEntity>

@Query("SELECT COUNT(*) FROM departments")
suspend fun getDepartmentsSize(): Int
yeon-kyu marked this conversation as resolved.
Show resolved Hide resolved

@Query("UPDATE departments SET shortName = :shortName, koreanName = :koreanName WHERE name = :name")
suspend fun updateDepartment(name: String, shortName: String, koreanName: String)

@Query("SELECT * FROM departments WHERE isSubscribed = :isSubscribed")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DB에 관심이 많아 한가지 댓글 남겨봅니다!
SELECT 문에서 *는 지양하는 것 이 좋습니다!! 여러 이유가 있지만 다음 글이 잘 설명해주는 것 같아 링크 남겨봅니다
관련 링크 : https://hyewoncc.github.io/sql-no-wildcard/

Copy link
Member

@yeon-kyu yeon-kyu Aug 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

select *를 지양하는 이유가 7가지나 있군요! Entity를 가져와서 비즈니스로직을 처리하는곳에서는 어쩔수없이 select * 를 사용해야겠지만(꽤 많은경우가 이렇긴할것같아요 ㅠ) 단순한 로직에선 지양하는게 좋을것 같네요
정보공유 감사합니다~!

suspend fun getDepartmentsBySubscribed(isSubscribed: Boolean): List<DepartmentEntity>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class DepartmentRepositoryImpl @Inject constructor(
override suspend fun insertAllDepartmentsFromRemote() {
val departments = fetchAllDepartmentsFromRemote()
departments?.let {
departmentDao.insertDepartments(departments.map { it.toEntity() })
if (departmentDao.getDepartmentsSize() == 0) {
departmentDao.insertDepartments(departments.toEntityList())
} else {
updateDepartmentsName(it)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

학과 DB가 비어 있다면 서버에서 가져온 데이터를 그대로 삽입하고, 비어 있지 않다면 서버에서 가져온 데이터와 비교하여 DB를 업데이트합니다.

}
}
}

Expand All @@ -36,6 +40,15 @@ class DepartmentRepositoryImpl @Inject constructor(
}.getOrNull()
}

private suspend fun updateDepartmentsName(departments: List<Department>) {
withContext(ioDispatcher) {
departments.forEach { (name, shortName, koreanName, _) ->
departmentDao.updateDepartment(name, shortName, koreanName)
}
}
this.departments = null
yeon-kyu marked this conversation as resolved.
Show resolved Hide resolved
}

override suspend fun insertDepartment(department: Department) {
withContext(ioDispatcher) {
departmentDao.insertDepartment(department.toEntity())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ku_stacks.ku_ring.persistence
package com.ku_stacks.ku_ring
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dao 테스트 이외에도 다른 integrated test에서 사용할 수 있을 것 같아 상위 패키지로 옮겼습니다.


import androidx.room.Room
import androidx.test.core.app.ApplicationProvider.getApplicationContext
Expand Down
8 changes: 8 additions & 0 deletions app/src/test/java/com/ku_stacks/ku_ring/MockUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.ku_stacks.ku_ring.data.api.response.NoticeResponse
import com.ku_stacks.ku_ring.data.api.response.SubscribeListResponse
import com.ku_stacks.ku_ring.data.api.response.UserListResponse
import com.ku_stacks.ku_ring.data.api.response.UserResponse
import com.ku_stacks.ku_ring.data.db.DepartmentEntity
import com.ku_stacks.ku_ring.data.db.NoticeEntity
import com.ku_stacks.ku_ring.data.db.PushEntity
import com.ku_stacks.ku_ring.data.model.Notice
Expand Down Expand Up @@ -58,6 +59,13 @@ object MockUtil {
isReadOnStorage = false,
)

fun mockDepartmentEntity() = DepartmentEntity(
name = "smart_ict_convergence",
shortName = "sicte",
koreanName = "스마트ICT융합공학과",
isSubscribed = false,
)

fun mockNotice() = Notice(
postedDate = "20220203",
subject = "2022학년도 1학기 재입학 합격자 유의사항 안내",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.ku_stacks.ku_ring.data.api.NoticeClient
import com.ku_stacks.ku_ring.data.db.NoticeDao
import com.ku_stacks.ku_ring.data.db.NoticeEntity
import com.ku_stacks.ku_ring.data.source.DepartmentNoticeMediator
import com.ku_stacks.ku_ring.persistence.LocalDbAbstract
import com.ku_stacks.ku_ring.LocalDbAbstract
import com.ku_stacks.ku_ring.util.PreferenceUtil
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.ku_stacks.ku_ring.persistence

import com.ku_stacks.ku_ring.LocalDbAbstract
import com.ku_stacks.ku_ring.MockUtil
import com.ku_stacks.ku_ring.data.db.DepartmentDao
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(RobolectricTestRunner::class)
class DepartmentDaoTest : LocalDbAbstract() {
private lateinit var departmentDao: DepartmentDao

@Before
fun setup() {
departmentDao = db.departmentDao()
}

@Test
fun `insertDepartment and updateDepartment test`() = runTest {
// given
val entity = MockUtil.mockDepartmentEntity()
departmentDao.insertDepartment(entity)

// when
val newShortName = "ict"
val newKoreanName = "스융공"
departmentDao.updateDepartment(
name = entity.name,
shortName = newShortName,
koreanName = newKoreanName
)

// then
val result = departmentDao.getDepartmentsByName(entity.name)
assert(result.isNotEmpty())
assertEquals(1, result.size)
assertEquals(result[0], entity.copy(shortName = newShortName, koreanName = newKoreanName))
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ku_stacks.ku_ring.persistence

import androidx.paging.PagingSource
import com.ku_stacks.ku_ring.LocalDbAbstract
import com.ku_stacks.ku_ring.data.db.NoticeDao
import com.ku_stacks.ku_ring.data.db.NoticeEntity
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ku_stacks.ku_ring.persistence

import com.ku_stacks.ku_ring.LocalDbAbstract
import com.ku_stacks.ku_ring.data.db.PushDao
import com.ku_stacks.ku_ring.data.db.PushEntity
import kotlinx.coroutines.runBlocking
Expand Down