Skip to content

Commit

Permalink
feat: 메뉴 API에 userId 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JJongmen committed Jun 2, 2024
1 parent 26e5c83 commit 3063052
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import kotlinx.coroutines.flow.flowOf
class FakeMenuRepositoryImpl : MenuRepository {
private var db = listOf<Menu>()

override suspend fun updateMenus(menus: List<Menu>) {
override suspend fun updateMenus(
menus: List<Menu>,
userId: Long,
) {
db = menus
}

override suspend fun getMenus(): Flow<List<Menu>> = flowOf(db)
override suspend fun getMenus(userId: Long): Flow<List<Menu>> = flowOf(db)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class MenuRepositoryImpl(
private val menuDao: MenuDao,
private val menuApi: MenuApi,
) : MenuRepository {
override suspend fun updateMenus(menus: List<Menu>) {
override suspend fun updateMenus(
menus: List<Menu>,
userId: Long,
) {
// 1. 서버에 메뉴 수정 요청
val updatedMenus =
menuApi.updateMenus(
Expand All @@ -31,15 +34,15 @@ class MenuRepositoryImpl(
)
}

override suspend fun getMenus(): Flow<List<Menu>> =
override suspend fun getMenus(userId: Long): Flow<List<Menu>> =
flow {
// 1. 로컬 DB에 있는 데이터 반환
val localData = menuDao.getAll().map { it.toDomainModel() }
emit(localData)

// 2. 서버에서 받은 데이터 동기화 및 반환
runCatching {
menuApi.getMenus()
menuApi.getMenus(userId)
}.onSuccess { remoteData ->
menuDao.updateAll(remoteData.map { it.toMenuEntity() })
emit(remoteData.map { it.toDomainModel() })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package com.erica.gamsung.menu.domain
import kotlinx.coroutines.flow.Flow

interface MenuRepository {
suspend fun updateMenus(menus: List<Menu>)
suspend fun updateMenus(
menus: List<Menu>,
userId: Long,
)

suspend fun getMenus(): Flow<List<Menu>>
suspend fun getMenus(userId: Long): Flow<List<Menu>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.erica.gamsung.menu.presentation
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.erica.gamsung.login.domain.LoginRepository
import com.erica.gamsung.menu.data.repository.FakeMenuRepositoryImpl
import com.erica.gamsung.menu.domain.Menu
import com.erica.gamsung.menu.domain.MenuRepository
Expand All @@ -21,6 +22,7 @@ class MenuViewModel
constructor(
state: SavedStateHandle,
private val menuRepository: MenuRepository = FakeMenuRepositoryImpl(),
private val loginRepository: LoginRepository,
) : ViewModel() {
private var _menusState = MutableStateFlow(state.get<List<Menu>>(MENUS) ?: emptyList())
val menusState = _menusState.asStateFlow()
Expand All @@ -36,7 +38,7 @@ class MenuViewModel

private fun loadMenus() {
viewModelScope.launch(Dispatchers.IO) {
menuRepository.getMenus().collect { menus ->
menuRepository.getMenus(loginRepository.getMemberId()).collect { menus ->
_menusState.update { menus }
}
}
Expand Down Expand Up @@ -113,7 +115,7 @@ class MenuViewModel
}
} else {
CoroutineScope(Dispatchers.IO).launch {
menuRepository.updateMenus(menus)
menuRepository.updateMenus(menus, loginRepository.getMemberId())
}
}
}
Expand Down

0 comments on commit 3063052

Please sign in to comment.