Skip to content

Commit

Permalink
[refactor] : #83 찜꽁리스트 Room 부분 클린아키텍처 마이그레이션 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
SsongSik committed May 9, 2023
1 parent 78b0f11 commit 10ed1c2
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.myongsik.myongsikandroid.data.datasource.restaurant

import com.myongsik.myongsikandroid.domain.model.restaurant.InsertRestaurantEntity
import com.myongsik.myongsikandroid.domain.model.restaurant.RestaurantEntity

interface RestaurantDataSource {

suspend fun insertRestaurant(insertRestaurantEntity: InsertRestaurantEntity)
suspend fun insertRestaurant(restaurantEntity: RestaurantEntity)

suspend fun deleteRestaurant(restaurantEntity: RestaurantEntity)

suspend fun loveIs(id: String): RestaurantEntity
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.myongsik.myongsikandroid.data.datasource.restaurant

import com.myongsik.myongsikandroid.data.db.RestaurantDatabase
import com.myongsik.myongsikandroid.data.model.kakao.toInsertFoodData
import com.myongsik.myongsikandroid.domain.model.restaurant.InsertRestaurantEntity
import com.myongsik.myongsikandroid.data.model.kakao.toRestaurantData
import com.myongsik.myongsikandroid.data.model.kakao.toRestaurantEntity
import com.myongsik.myongsikandroid.domain.model.restaurant.RestaurantEntity
import javax.inject.Inject

class RestaurantDataSourceImpl @Inject constructor(
private val loveDb : RestaurantDatabase
) : RestaurantDataSource {

override suspend fun insertRestaurant(insertRestaurantEntity: InsertRestaurantEntity) {
loveDb.restaurantDao().insertGoodFood(insertRestaurantEntity.toInsertFoodData())
override suspend fun insertRestaurant(restaurantEntity: RestaurantEntity) {
loveDb.restaurantDao().insertGoodFood(restaurantEntity.toRestaurantData())
}

override suspend fun deleteRestaurant(restaurantEntity: RestaurantEntity) {
loveDb.restaurantDao().deleteBook(restaurantEntity.toRestaurantData())
}

override suspend fun loveIs(id: String): RestaurantEntity = loveDb.restaurantDao().loveIs(id).toRestaurantEntity()

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.myongsik.myongsikandroid.data.datasource.user

import com.myongsik.myongsikandroid.data.api.UserApi
import com.myongsik.myongsikandroid.data.db.RestaurantDatabase
import com.myongsik.myongsikandroid.data.model.kakao.toInsertFoodData
import com.myongsik.myongsikandroid.data.model.user.toRequestUserData
import com.myongsik.myongsikandroid.data.model.user.toResponseUserEntity
import com.myongsik.myongsikandroid.domain.model.restaurant.InsertRestaurantEntity
import com.myongsik.myongsikandroid.domain.model.user.RequestUserEntity
import com.myongsik.myongsikandroid.domain.model.user.ResponseUserEntity
import javax.inject.Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package com.myongsik.myongsikandroid.data.model.kakao
import android.os.Parcelable
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.myongsik.myongsikandroid.domain.model.restaurant.InsertRestaurantEntity
import com.myongsik.myongsikandroid.domain.model.restaurant.RestaurantEntity
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import kotlinx.parcelize.Parcelize
Expand Down Expand Up @@ -40,7 +40,7 @@ data class Restaurant(
val y: String
) : Parcelable

fun InsertRestaurantEntity.toInsertFoodData() = Restaurant(
fun RestaurantEntity.toRestaurantData() = Restaurant(
address_name = this.addressName,
category_group_code = this.categoryGroupCode,
category_group_name = this.categoryGroupName,
Expand All @@ -55,7 +55,7 @@ fun InsertRestaurantEntity.toInsertFoodData() = Restaurant(
y = this.y
)

fun Restaurant.toInsertFoodEntity() = InsertRestaurantEntity(
fun Restaurant.toRestaurantEntity() = RestaurantEntity(
addressName = this.address_name,
categoryGroupCode = this.category_group_code,
categoryGroupName = this.category_group_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,9 @@ interface FoodRepository {

suspend fun getCurrentWidgetType(): Flow<String?>

//Room
suspend fun deleteFoods(restaurant: Restaurant)

//Room PagingData
fun getFoods(): Flow<PagingData<Restaurant>>

fun loveIs(id: String): Restaurant

fun updateLove(id: String): Boolean

fun getLoveIsFood() : Flow<List<Restaurant>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,6 @@ class FoodRepositoryImpl @Inject constructor(
}
}

//장소 찜콩리스트에서 삭제
override suspend fun deleteFoods(restaurant: Restaurant) {
db.restaurantDao().deleteBook(restaurant)
}

//장소 현재 찜해두었는지 판단
override fun loveIs(id: String): Restaurant {
return db.restaurantDao().loveIs(id)
}

override fun updateLove(id: String): Boolean {
if (!db.restaurantDao().loveUpdate(id)) {
return false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.myongsik.myongsikandroid.data.repository.restaurant

import com.myongsik.myongsikandroid.data.datasource.restaurant.RestaurantDataSource
import com.myongsik.myongsikandroid.data.db.RestaurantDatabase
import com.myongsik.myongsikandroid.data.model.kakao.toInsertFoodData
import com.myongsik.myongsikandroid.domain.model.restaurant.InsertRestaurantEntity
import com.myongsik.myongsikandroid.domain.model.restaurant.RestaurantEntity
import com.myongsik.myongsikandroid.domain.repository.restaurant.RestaurantRepository
import javax.inject.Inject

class RestaurantRepositoryImpl @Inject constructor(
private val restaurantDataSource: RestaurantDataSource
) : RestaurantRepository {

override suspend fun insertRestaurant(insertRestaurantEntity: InsertRestaurantEntity) {
restaurantDataSource.insertRestaurant(insertRestaurantEntity)
override suspend fun insertRestaurant(restaurantEntity: RestaurantEntity) {
restaurantDataSource.insertRestaurant(restaurantEntity)
}

override suspend fun deleteRestaurant(restaurantEntity: RestaurantEntity) {
restaurantDataSource.deleteRestaurant(restaurantEntity)
}

override suspend fun loveIs(id: String): RestaurantEntity = restaurantDataSource.loveIs(id)

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.myongsik.myongsikandroid.domain.model.restaurant

data class InsertRestaurantEntity(
data class RestaurantEntity(
val addressName: String,
val categoryGroupCode: String,
val categoryGroupName: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.myongsik.myongsikandroid.domain.repository.restaurant

import com.myongsik.myongsikandroid.domain.model.restaurant.InsertRestaurantEntity
import com.myongsik.myongsikandroid.domain.model.restaurant.RestaurantEntity

interface RestaurantRepository {

suspend fun insertRestaurant(insertRestaurantEntity: InsertRestaurantEntity)
suspend fun insertRestaurant(restaurantEntity: RestaurantEntity)

suspend fun deleteRestaurant(restaurantEntity: RestaurantEntity)

suspend fun loveIs(id : String) : RestaurantEntity
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.myongsik.myongsikandroid.domain.usecase.restaurant

import com.myongsik.myongsikandroid.domain.repository.restaurant.RestaurantRepository
import com.myongsik.myongsikandroid.domain.model.restaurant.RestaurantEntity
import javax.inject.Inject

class DeleteRestaurantDataUseCase @Inject constructor(
private val restaurantRepository : RestaurantRepository
) {

suspend operator fun invoke(restaurantEntity: RestaurantEntity) =
restaurantRepository.deleteRestaurant(restaurantEntity)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.myongsik.myongsikandroid.domain.usecase.restaurant

import com.myongsik.myongsikandroid.domain.repository.restaurant.RestaurantRepository
import com.myongsik.myongsikandroid.domain.model.restaurant.InsertRestaurantEntity
import com.myongsik.myongsikandroid.domain.model.restaurant.RestaurantEntity
import javax.inject.Inject

class InsertRestaurantDataUseCase @Inject constructor(
private val restaurantRepository : RestaurantRepository
) {

suspend operator fun invoke(insertRestaurantEntity: InsertRestaurantEntity) =
restaurantRepository.insertRestaurant(insertRestaurantEntity)
suspend operator fun invoke(restaurantEntity: RestaurantEntity) =
restaurantRepository.insertRestaurant(restaurantEntity)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.myongsik.myongsikandroid.domain.usecase.restaurant

import com.myongsik.myongsikandroid.domain.repository.restaurant.RestaurantRepository
import javax.inject.Inject

class LoveIsRestaurantDataUseCase @Inject constructor(
private val restaurantRepository : RestaurantRepository
) {

suspend operator fun invoke(id : String) = restaurantRepository.loveIs(id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,42 @@ import com.myongsik.myongsikandroid.BaseViewModel
import com.myongsik.myongsikandroid.data.model.food.RequestScrap
import com.myongsik.myongsikandroid.data.model.food.ResponseScrap
import com.myongsik.myongsikandroid.data.model.kakao.Restaurant
import com.myongsik.myongsikandroid.data.model.kakao.toInsertFoodEntity
import com.myongsik.myongsikandroid.data.model.kakao.toRestaurantData
import com.myongsik.myongsikandroid.data.model.kakao.toRestaurantEntity
import com.myongsik.myongsikandroid.data.repository.food.FoodRepository
import com.myongsik.myongsikandroid.domain.usecase.restaurant.DeleteRestaurantDataUseCase
import com.myongsik.myongsikandroid.domain.usecase.restaurant.InsertRestaurantDataUseCase
import com.myongsik.myongsikandroid.domain.usecase.restaurant.LoveIsRestaurantDataUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class LoveViewModel @Inject constructor(
private val foodRepository: FoodRepository,
private val insertRestaurantDataUseCase: InsertRestaurantDataUseCase
private val insertRestaurantDataUseCase: InsertRestaurantDataUseCase,
private val deleteRestaurantDataUseCase: DeleteRestaurantDataUseCase,
private val loveIsRestaurantDataUseCase: LoveIsRestaurantDataUseCase
) : BaseViewModel() {

//Room
fun saveFoods(restaurant: Restaurant) = launch {
insertRestaurantDataUseCase(restaurant.toInsertFoodEntity())
insertRestaurantDataUseCase(restaurant.toRestaurantEntity())
}

fun deleteFoods(restaurant: Restaurant) = launch {
foodRepository.deleteFoods(restaurant)
deleteRestaurantDataUseCase(restaurant.toRestaurantEntity())
}

private val _loveIs = MutableLiveData<Restaurant>()
val loveIs: LiveData<Restaurant>
get() = _loveIs

fun loveIs(restaurant: Restaurant) = launch {
val restaurantLove = foodRepository.loveIs(restaurant.id)
_loveIs.postValue(restaurantLove)
loveIsRestaurantDataUseCase(restaurant.id).let{
_loveIs.postValue(it.toRestaurantData())
}
}

val loveIsFood: StateFlow<List<Restaurant>> = foodRepository.getLoveIsFood()
Expand Down

0 comments on commit 10ed1c2

Please sign in to comment.