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 3 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