Skip to content

Commit

Permalink
Merge pull request #107 from MYONGSIK/refactor/clean
Browse files Browse the repository at this point in the history
[refact/clean] : #83 LoveViewModel 클린아키텍처 마이그레이션
  • Loading branch information
SsongSik committed May 10, 2023
2 parents fcaccfa + 475dc4e commit f6f443e
Show file tree
Hide file tree
Showing 42 changed files with 594 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package com.myongsik.myongsikandroid.data.api
import com.myongsik.myongsikandroid.data.model.food.*
import com.myongsik.myongsikandroid.data.model.review.RequestReviewData
import com.myongsik.myongsikandroid.data.model.review.ResponseReviewData
import com.myongsik.myongsikandroid.data.model.user.RequestUserData
import com.myongsik.myongsikandroid.data.model.user.ResponseUserData
import retrofit2.Response
import retrofit2.http.*

Expand All @@ -26,16 +24,6 @@ interface HomeFoodApi {
@Body body: RequestReviewData
): Response<ResponseReviewData>

@POST("/api/v2/meals/evaluate")
suspend fun postMeal(
@Body body: RequestMealData
): Response<ResponseMealData>

@POST("/api/v2/scraps")
suspend fun postRestaurantScrap(
@Body body: RequestScrap
): Response<ResponseScrap>

@GET("/api/v2/scraps/store")
suspend fun getRankRestaurant(
@Query("sort") sort : String = "scrapCount,desc",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.myongsik.myongsikandroid.data.api

import com.myongsik.myongsikandroid.data.model.restaurant.RequestScrap
import com.myongsik.myongsikandroid.data.model.restaurant.ResponseScrap
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.POST

interface RestaurantApi {

@POST("/api/v2/scraps")
suspend fun postRestaurantScrap(
@Body body: RequestScrap
): Response<ResponseScrap>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.myongsik.myongsikandroid.data.datasource.restaurant

import androidx.paging.PagingData
import com.myongsik.myongsikandroid.domain.model.restaurant.RequestScrapEntity
import com.myongsik.myongsikandroid.domain.model.restaurant.ResponseScrapEntity
import com.myongsik.myongsikandroid.domain.model.restaurant.RestaurantEntity
import kotlinx.coroutines.flow.Flow

interface RestaurantDataSource {

suspend fun insertRestaurant(restaurantEntity: RestaurantEntity)

suspend fun deleteRestaurant(restaurantEntity: RestaurantEntity)

suspend fun loveIs(id: String): RestaurantEntity

suspend fun getLoveRestaurant(): Flow<PagingData<RestaurantEntity>>

suspend fun getLoveListRestaurant() : Flow<List<RestaurantEntity>>

suspend fun postScrapRestaurant(requestScrapEntity: RequestScrapEntity): ResponseScrapEntity?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.myongsik.myongsikandroid.data.datasource.restaurant

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.map
import com.myongsik.myongsikandroid.data.api.RestaurantApi
import com.myongsik.myongsikandroid.data.db.RestaurantDatabase
import com.myongsik.myongsikandroid.data.model.kakao.toRestaurantData
import com.myongsik.myongsikandroid.data.model.kakao.toRestaurantEntity
import com.myongsik.myongsikandroid.data.model.restaurant.toRequestScrapData
import com.myongsik.myongsikandroid.data.model.restaurant.toResponseScrapEntity
import com.myongsik.myongsikandroid.domain.model.restaurant.RequestScrapEntity
import com.myongsik.myongsikandroid.domain.model.restaurant.ResponseScrapEntity
import com.myongsik.myongsikandroid.domain.model.restaurant.RestaurantEntity
import com.myongsik.myongsikandroid.util.Constant
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

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

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()

override suspend fun getLoveRestaurant(): Flow<PagingData<RestaurantEntity>> {
val pagingSourceFactory = {
loveDb.restaurantDao().getFoods()
}

return Pager(
config = PagingConfig(
pageSize = Constant.PAGING_SIZE,
enablePlaceholders = false,
maxSize = Constant.PAGING_SIZE * 3
),
pagingSourceFactory = pagingSourceFactory
).flow
.map { pagingData -> pagingData.map { it.toRestaurantEntity() } }
}

override suspend fun getLoveListRestaurant(): Flow<List<RestaurantEntity>> {
return loveDb.restaurantDao().getIsLoveFood()
.map { restaurantList -> restaurantList.map { it.toRestaurantEntity() } }
}

override suspend fun postScrapRestaurant(requestScrapEntity: RequestScrapEntity): ResponseScrapEntity? {
val response = restaurantApi.postRestaurantScrap(requestScrapEntity.toRequestScrapData())
return if(response.isSuccessful) {
response.body()?.toResponseScrapEntity()
} else {
null
}
}

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

import com.myongsik.myongsikandroid.domain.model.user.RequestUserEntity
import com.myongsik.myongsikandroid.domain.model.user.ResponseUserEntity

interface UserDataSource {

suspend fun postUser(requestUserEntity: RequestUserEntity): ResponseUserEntity?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.myongsik.myongsikandroid.data.datasource.user

import com.myongsik.myongsikandroid.data.api.UserApi
import com.myongsik.myongsikandroid.data.model.user.toRequestUserData
import com.myongsik.myongsikandroid.data.model.user.toResponseUserEntity
import com.myongsik.myongsikandroid.domain.model.user.RequestUserEntity
import com.myongsik.myongsikandroid.domain.model.user.ResponseUserEntity
import javax.inject.Inject

class UserDataSourceImpl @Inject constructor(
private val userApi: UserApi
) : UserDataSource {

override suspend fun postUser(requestUserEntity: RequestUserEntity): ResponseUserEntity? {
val response = userApi.postUser(requestUserEntity.toRequestUserData())
return if (response.isSuccessful) {
response.body()?.toResponseUserEntity()
} else {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface RestaurantDao {

//관심목록에 담기를 클릭했을 때 이미 있는지 판단하는 쿼리
@Query("SELECT * FROM love_list WHERE id = :id")
fun loveIs(id : String) : Restaurant
suspend fun loveIs(id : String) : Restaurant

//찜꽁 리스트에서 삭제하는 쿼리
@Delete
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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.RestaurantEntity
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import kotlinx.parcelize.Parcelize
Expand Down Expand Up @@ -37,4 +38,34 @@ data class Restaurant(
val x: String,
@field:Json(name = "y")
val y: String
) : Parcelable
) : Parcelable

fun RestaurantEntity.toRestaurantData() = Restaurant(
address_name = this.addressName,
category_group_code = this.categoryGroupCode,
category_group_name = this.categoryGroupName,
category_name = this.categoryName,
distance = this.distance,
id = this.id,
phone = this.phone,
place_name = this.placeName,
place_url = this.placeUrl,
road_address_name = this.roadAddressName,
x = this.x,
y = this.y
)

fun Restaurant.toRestaurantEntity() = RestaurantEntity(
addressName = this.address_name,
categoryGroupCode = this.category_group_code,
categoryGroupName = this.category_group_name,
categoryName = this.category_name,
distance = this.distance,
id = this.id,
phone = this.phone,
placeName = this.place_name,
placeUrl = this.place_url,
roadAddressName = this.road_address_name,
x = this.x,
y = this.y
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.myongsik.myongsikandroid.data.model.restaurant

import com.myongsik.myongsikandroid.domain.model.restaurant.RequestScrapEntity

data class RequestScrap(
val address : String,
val campus : String,
val category : String,
val code : String,
val contact : String,
val distance : String,
val name : String,
val phoneId : String,
val urlAddress : String,
val latitude : String,
val longitude : String
)

fun RequestScrap.toRequestScrapEntity() = RequestScrapEntity(
address = this.address,
campus = this.campus,
category = this.category,
code = this.code,
contact = this.contact,
distance = this.distance,
name = this.name,
phoneId = this.phoneId,
urlAddress = this.urlAddress,
latitude = this.latitude,
longitude = this.longitude
)

fun RequestScrapEntity.toRequestScrapData() = RequestScrap(
address = this.address,
campus = this.campus,
category = this.category,
code = this.code,
contact = this.contact,
distance = this.distance,
name = this.name,
phoneId = this.phoneId,
urlAddress = this.urlAddress,
latitude = this.latitude,
longitude = this.longitude
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.myongsik.myongsikandroid.data.model.restaurant

import com.myongsik.myongsikandroid.domain.model.restaurant.ResponseScrapEntity

data class ResponseScrap(
val httpCode : Int,
val httpStatus : String,
val localDateTime : String,
val message : String,
val success : Boolean,
val data : ResponseScrapData
) {
data class ResponseScrapData(
val id: Int,
val address : String,
val category : String,
val code : String,
val contact : String,
val distance : String,
val name : String,
val urlAddress : String,
)
}

fun ResponseScrap.toResponseScrapEntity() = ResponseScrapEntity(
httpCode = this.httpCode,
httpStatus = this.httpStatus,
localDateTime = this.localDateTime,
message = this.message,
success = this.success,
id = this.data.id,
address = this.data.address,
category = this.data.category,
code = this.data.code,
contact = this.data.contact,
distance = this.data.distance,
name = this.data.name,
urlAddress = this.data.urlAddress
)

fun ResponseScrapEntity.toResponseScrapData() = ResponseScrap(
httpCode = this.httpCode,
httpStatus = this.httpStatus,
localDateTime = this.localDateTime,
message = this.message,
success = this.success,
data = ResponseScrap.ResponseScrapData(
id = this.id,
address = this.address,
category = this.category,
code = this.code,
contact = this.contact,
distance = this.distance,
name = this.name,
urlAddress = this.urlAddress
)
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.myongsik.myongsikandroid.data.model.user

import com.myongsik.myongsikandroid.domain.model.RequestUserEntity
import com.myongsik.myongsikandroid.domain.model.user.RequestUserEntity


data class RequestUserData(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.myongsik.myongsikandroid.data.model.user

import com.myongsik.myongsikandroid.domain.model.ResponseUserEntity
import com.myongsik.myongsikandroid.domain.model.user.ResponseUserEntity

data class ResponseUserData(
val httpCode: Int,
Expand Down
Loading

0 comments on commit f6f443e

Please sign in to comment.