From eebc8a91eb251d9055edc3e75f36089dd2783955 Mon Sep 17 00:00:00 2001 From: murjune Date: Fri, 12 Aug 2022 05:51:22 +0900 Subject: [PATCH] =?UTF-8?q?#178=20[refact]=20data=EB=AA=A8=EB=93=88=20?= =?UTF-8?q?=EB=82=B4=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/hous/data/api/HomeApi.kt | 26 +++-- .../main/java/com/hous/data/api/RulesApi.kt | 20 ++-- .../java/com/hous/data/entity/Category.kt | 4 +- .../main/java/com/hous/data/entity/Homie.kt | 4 +- .../main/java/com/hous/data/entity/Rule.kt | 17 +++- .../data/model/request/TempManagerRequest.kt | 2 +- .../model/response/TempManagerResponse.kt | 2 +- .../data/repository/NewRulesRepositoryImpl.kt | 8 +- .../repository/RulesTodayRepositoryImpl.kt | 55 +++++------ .../remote/RemoteRulesTodayDataSource.kt | 20 ++-- .../remote/RemoteRulesTodayDataSourceImpl.kt | 97 +++++++++++++++---- 11 files changed, 164 insertions(+), 91 deletions(-) diff --git a/data/src/main/java/com/hous/data/api/HomeApi.kt b/data/src/main/java/com/hous/data/api/HomeApi.kt index 7850477b..4657e482 100644 --- a/data/src/main/java/com/hous/data/api/HomeApi.kt +++ b/data/src/main/java/com/hous/data/api/HomeApi.kt @@ -6,46 +6,52 @@ import com.hous.data.entity.ResultData import com.hous.data.model.WrapperClass import com.hous.data.model.request.EventListRequest import com.hous.data.model.response.HomeResponse -import retrofit2.http.* +import retrofit2.http.Body +import retrofit2.http.DELETE +import retrofit2.http.GET +import retrofit2.http.POST +import retrofit2.http.PUT +import retrofit2.http.Path + interface HomeApi { @GET("room/{roomId}/home") suspend fun getHomeList( @Path("roomId") roomId: String - ): com.hous.data.model.WrapperClass + ): WrapperClass @DELETE("room/{roomId}/event/{eventId}") suspend fun deleteEvent( @Path("roomId") roomId: String, @Path("eventId") eventId: String - ): com.hous.data.model.WrapperClass + ): WrapperClass @GET("room/{roomId}/event/{eventId}") suspend fun getEventList( @Path("roomId") roomId: String, @Path("eventId") eventId: String - ): com.hous.data.model.WrapperClass + ): WrapperClass @PUT("room/{roomId}/event/{eventId}") suspend fun putEventList( @Path("roomId") roomId: String, @Path("eventId") eventId: String, - @Body body: com.hous.data.model.request.EventListRequest - ): com.hous.data.model.WrapperClass + @Body body: EventListRequest + ): WrapperClass @POST("room/{roomId}/event") suspend fun addEvent( @Path("roomId") roomId: String, - @Body body: com.hous.data.model.request.EventListRequest - ): com.hous.data.model.WrapperClass + @Body body: EventListRequest + ): WrapperClass @GET("user/{homieId}") suspend fun getHomieList( @Path("homieId") homieId: String - ): com.hous.data.model.WrapperClass + ): WrapperClass @GET("user/{userId}/type") suspend fun getHomieResult( @Path("userId") userId: String - ): com.hous.data.model.WrapperClass + ): WrapperClass } diff --git a/data/src/main/java/com/hous/data/api/RulesApi.kt b/data/src/main/java/com/hous/data/api/RulesApi.kt index ae041bd4..96d4e907 100644 --- a/data/src/main/java/com/hous/data/api/RulesApi.kt +++ b/data/src/main/java/com/hous/data/api/RulesApi.kt @@ -1,5 +1,11 @@ package com.hous.data.api +import com.hous.data.entity.Rule +import com.hous.data.model.WrapperClass +import com.hous.data.model.request.MyToDoCheckRequest +import com.hous.data.model.request.TempManagerRequest +import com.hous.data.model.response.RulesTodayInfoListResponse +import com.hous.data.model.response.TempManagerResponse import retrofit2.http.Body import retrofit2.http.GET import retrofit2.http.PUT @@ -9,32 +15,32 @@ interface RulesApi { @GET("room/{roomId}/rules") suspend fun getTodayTodayInfoList( @Path("roomId") roomId: String - ): com.hous.data.model.WrapperClass + ): WrapperClass @GET("/room/{roomId}/rule/{ruleId}/today") suspend fun getTempManagerInfoList( @Path("roomId") roomId: String, @Path("ruleId") ruleId: String - ): com.hous.data.model.WrapperClass + ): WrapperClass @PUT("/room/{roomId}/rule/{ruleId}/today") suspend fun putTempManagerInfoList( @Path("roomId") roomId: String, @Path("ruleId") ruleId: String, - @Body tmpRuleMembers: com.hous.data.model.response.TempManagerRequest - ): com.hous.data.model.WrapperClass + @Body tmpRuleMembers: TempManagerRequest + ): Any? @GET("/room/{roomId}/rules/me") suspend fun getMyTodoInfoList( @Path("roomId") roomId: String - ): com.hous.data.model.WrapperClass> + ): WrapperClass> @PUT("room/{roomId}/rule/{ruleId}/check") suspend fun putMyToDoCheckList( @Path("roomId") roomId: String, @Path("ruleId") ruleId: String, - @Body isCheck: com.hous.data.model.request.MyToDoCheckRequest - ): com.hous.data.model.WrapperClass + @Body isCheck: MyToDoCheckRequest + ): WrapperClass @GET("/room/{roomId}/category/{categoryId}/rule") suspend fun getRuleTableInfoList( diff --git a/data/src/main/java/com/hous/data/entity/Category.kt b/data/src/main/java/com/hous/data/entity/Category.kt index 44e2b084..0b671f8d 100644 --- a/data/src/main/java/com/hous/data/entity/Category.kt +++ b/data/src/main/java/com/hous/data/entity/Category.kt @@ -1,7 +1,7 @@ package com.hous.data.entity import com.google.gson.annotations.SerializedName -import com.hous.domain.model.Category +import com.hous.domain.model.CategoryInfo data class Category( @SerializedName("_id") val id: String = "", @@ -10,5 +10,5 @@ data class Category( val ruleName: String = "", var isChecked: Boolean = false ) { - fun toCategory() = Category(id, categoryName, categoryIcon, ruleName, isChecked) + fun toCategoryInfo() = CategoryInfo(id, categoryName, categoryIcon, ruleName, isChecked) } diff --git a/data/src/main/java/com/hous/data/entity/Homie.kt b/data/src/main/java/com/hous/data/entity/Homie.kt index c64987ef..709d1ebb 100644 --- a/data/src/main/java/com/hous/data/entity/Homie.kt +++ b/data/src/main/java/com/hous/data/entity/Homie.kt @@ -1,7 +1,7 @@ package com.hous.data.entity import com.google.gson.annotations.SerializedName -import com.hous.domain.model.Homie +import com.hous.domain.model.HomieInfo data class Homie( @SerializedName("_id") val id: String? = null, @@ -16,7 +16,7 @@ data class Homie( var isChecked: Boolean = false, val notificationState: Boolean = false ) { - fun toHomie(): Homie = Homie( + fun toHomieInfo(): HomieInfo = HomieInfo( id, userName, typeName, diff --git a/data/src/main/java/com/hous/data/entity/Rule.kt b/data/src/main/java/com/hous/data/entity/Rule.kt index 431e58b4..366e1157 100644 --- a/data/src/main/java/com/hous/data/entity/Rule.kt +++ b/data/src/main/java/com/hous/data/entity/Rule.kt @@ -1,6 +1,7 @@ package com.hous.data.entity import com.google.gson.annotations.SerializedName +import com.hous.domain.model.RuleInfo data class Rule( @SerializedName("_id") val id: String = "", @@ -11,5 +12,17 @@ data class Rule( val isAllChecked: Boolean = false, val membersCnt: Int = 0, val typeColors: List = emptyList(), - val categoryIcon: String = "", -) + val categoryIcon: String = "" +) { + fun toRuleInfo() = RuleInfo( + id, + isChecked, + ruleName, + todayMembersWithTypeColor.map { it.toHomieInfo() }, + isTmpMember, + isAllChecked, + membersCnt, + typeColors, + categoryIcon + ) +} diff --git a/data/src/main/java/com/hous/data/model/request/TempManagerRequest.kt b/data/src/main/java/com/hous/data/model/request/TempManagerRequest.kt index d88975bd..a0b7b1c6 100644 --- a/data/src/main/java/com/hous/data/model/request/TempManagerRequest.kt +++ b/data/src/main/java/com/hous/data/model/request/TempManagerRequest.kt @@ -1,4 +1,4 @@ -package com.hous.data.model.response +package com.hous.data.model.request data class TempManagerRequest( val tmpRuleMembers: List = emptyList() diff --git a/data/src/main/java/com/hous/data/model/response/TempManagerResponse.kt b/data/src/main/java/com/hous/data/model/response/TempManagerResponse.kt index 9b51706e..50d0e053 100644 --- a/data/src/main/java/com/hous/data/model/response/TempManagerResponse.kt +++ b/data/src/main/java/com/hous/data/model/response/TempManagerResponse.kt @@ -7,4 +7,4 @@ data class TempManagerResponse( @SerializedName("_id") val id: String = "", val homies: List = emptyList() -) +) \ No newline at end of file diff --git a/data/src/main/java/com/hous/data/repository/NewRulesRepositoryImpl.kt b/data/src/main/java/com/hous/data/repository/NewRulesRepositoryImpl.kt index 33a8f07b..4dee5dc1 100644 --- a/data/src/main/java/com/hous/data/repository/NewRulesRepositoryImpl.kt +++ b/data/src/main/java/com/hous/data/repository/NewRulesRepositoryImpl.kt @@ -4,7 +4,7 @@ import android.util.Log import com.hous.data.model.request.Member import com.hous.data.model.request.NewRulesRequest import com.hous.data.source.remote.RemoteNewRulesDataSource -import com.hous.domain.model.DayData +import com.hous.domain.model.DayDataInfo import com.hous.domain.model.Manager import com.hous.domain.model.NewRuleInfo import com.hous.domain.model.State @@ -43,12 +43,12 @@ class NewRulesRepositoryImpl @Inject constructor( runCatching { val response = remoteNewRulesDataSource.getNewRuleList(roomId) NewRuleInfo( - response.data!!.ruleCategories.map { it.toCategory() }, - response.data.homies.map { it.toHomie() } + response.data!!.ruleCategories.map { it.toCategoryInfo() }, + response.data.homies.map { it.toHomieInfo() } ) } - private fun dayToInt(dayDataList: List): List { + private fun dayToInt(dayDataList: List): List { val tempList = mutableListOf() for (index in 0 until 7) { if (dayDataList[index].dayState == State.SELECT) tempList.add(index) diff --git a/data/src/main/java/com/hous/data/repository/RulesTodayRepositoryImpl.kt b/data/src/main/java/com/hous/data/repository/RulesTodayRepositoryImpl.kt index bbf4f730..21a92d32 100644 --- a/data/src/main/java/com/hous/data/repository/RulesTodayRepositoryImpl.kt +++ b/data/src/main/java/com/hous/data/repository/RulesTodayRepositoryImpl.kt @@ -3,63 +3,54 @@ package com.hous.data.repository import com.hous.data.entity.Rule import com.hous.data.model.WrapperClass import com.hous.data.model.request.MyToDoCheckRequest +import com.hous.data.model.request.TempManagerRequest import com.hous.data.model.response.RulesTableResponse -import com.hous.data.model.response.RulesTodayInfoListResponse -import com.hous.data.model.response.TempManagerRequest -import com.hous.data.model.response.TempManagerResponse import com.hous.data.source.remote.RemoteRulesTodayDataSource +import com.hous.domain.model.RuleInfo +import com.hous.domain.model.TempManagerInfo +import com.hous.domain.model.rules.RulesTableInfo +import com.hous.domain.model.rules.RulesTodayInfo import javax.inject.Inject class RulesTodayRepositoryImpl @Inject constructor( private val remoteRulesTodayDataSource: RemoteRulesTodayDataSource ) : RulesTodayRepository { - override suspend fun getTodayTodayInfoList(roomId: String): Result> { - return runCatching { remoteRulesTodayDataSource.getTodayTodayInfoList(roomId) } - } + override suspend fun getTodayTodayInfoList(roomId: String): RulesTodayInfo? = + remoteRulesTodayDataSource.getTodayTodayInfoList(roomId) override suspend fun getTempManagerInfoList( roomId: String, rulesId: String - ): Result> { - return runCatching { remoteRulesTodayDataSource.getTempManagerInfoList(roomId, rulesId) } - } + ): TempManagerInfo? = remoteRulesTodayDataSource.getTempManagerInfoList(roomId, rulesId) override suspend fun putTempManagerInfoList( roomId: String, ruleId: String, - tmpRuleMembers: TempManagerRequest - ): Result> { - return runCatching { - remoteRulesTodayDataSource.putTempManagerInfoList( - roomId, - ruleId, - tmpRuleMembers - ) - } + tmpRuleMembers: List + ): Any? { + return remoteRulesTodayDataSource.putTempManagerInfoList( + roomId, + ruleId, + TempManagerRequest(tmpRuleMembers) + ) } - override suspend fun getMyTodoInfoList(roomId: String): Result>> { - return runCatching { - remoteRulesTodayDataSource.getMyToDoInfoList(roomId) - } + override suspend fun getMyTodoInfoList(roomId: String): List? { + return remoteRulesTodayDataSource.getMyToDoInfoList(roomId) + } override suspend fun putMyToDoCheckLust( roomId: String, ruleId: String, - isCheck: MyToDoCheckRequest - ): Result> { - return runCatching { - remoteRulesTodayDataSource.putMyToDoCheckLust(roomId, ruleId, isCheck) - } + isCheck: Boolean + ) { + remoteRulesTodayDataSource.putMyToDoCheckLust(roomId, ruleId, MyToDoCheckRequest(isCheck)) } override suspend fun getRuleTableInfoList( roomId: String, categoryId: String - ): Result> { - return runCatching { - remoteRulesTodayDataSource.getRuleTableInfoList(roomId, categoryId) - } - } + ): RulesTableInfo? = + remoteRulesTodayDataSource.getRuleTableInfoList(roomId, categoryId) } diff --git a/data/src/main/java/com/hous/data/source/remote/RemoteRulesTodayDataSource.kt b/data/src/main/java/com/hous/data/source/remote/RemoteRulesTodayDataSource.kt index c2e95549..d531937f 100644 --- a/data/src/main/java/com/hous/data/source/remote/RemoteRulesTodayDataSource.kt +++ b/data/src/main/java/com/hous/data/source/remote/RemoteRulesTodayDataSource.kt @@ -4,34 +4,36 @@ import com.hous.data.entity.Rule import com.hous.data.model.WrapperClass import com.hous.data.model.request.MyToDoCheckRequest import com.hous.data.model.response.RulesTableResponse -import com.hous.data.model.response.RulesTodayInfoListResponse -import com.hous.data.model.response.TempManagerRequest -import com.hous.data.model.response.TempManagerResponse +import com.hous.data.model.request.TempManagerRequest +import com.hous.domain.model.RuleInfo +import com.hous.domain.model.rules.RulesTodayInfo +import com.hous.domain.model.TempManagerInfo +import com.hous.domain.model.rules.RulesTableInfo interface RemoteRulesTodayDataSource { - suspend fun getTodayTodayInfoList(roomId: String): WrapperClass + suspend fun getTodayTodayInfoList(roomId: String): RulesTodayInfo? suspend fun getTempManagerInfoList( roomId: String, ruleId: String - ): WrapperClass + ): TempManagerInfo? suspend fun putTempManagerInfoList( roomId: String, ruleId: String, tmpRuleMembers: TempManagerRequest - ): WrapperClass + ): Any? - suspend fun getMyToDoInfoList(roomId: String): WrapperClass> + suspend fun getMyToDoInfoList(roomId: String): List? suspend fun putMyToDoCheckLust( roomId: String, ruleId: String, isCheck: MyToDoCheckRequest - ): WrapperClass + ) suspend fun getRuleTableInfoList( roomId: String, categoryId: String - ): WrapperClass + ): RulesTableInfo? } diff --git a/data/src/main/java/com/hous/data/source/remote/RemoteRulesTodayDataSourceImpl.kt b/data/src/main/java/com/hous/data/source/remote/RemoteRulesTodayDataSourceImpl.kt index 64db47b4..c883c87c 100644 --- a/data/src/main/java/com/hous/data/source/remote/RemoteRulesTodayDataSourceImpl.kt +++ b/data/src/main/java/com/hous/data/source/remote/RemoteRulesTodayDataSourceImpl.kt @@ -2,13 +2,13 @@ package com.hous.data.source.remote import com.hous.data.BuildConfig import com.hous.data.api.RulesApi -import com.hous.data.entity.Rule -import com.hous.data.model.WrapperClass import com.hous.data.model.request.MyToDoCheckRequest -import com.hous.data.model.response.RulesTableResponse -import com.hous.data.model.response.RulesTodayInfoListResponse -import com.hous.data.model.response.TempManagerRequest -import com.hous.data.model.response.TempManagerResponse +import com.hous.data.model.request.TempManagerRequest +import com.hous.domain.model.RuleInfo +import com.hous.domain.model.TempManagerInfo +import com.hous.domain.model.rules.RulesTableInfo +import com.hous.domain.model.rules.RulesTodayInfo +import timber.log.Timber import javax.inject.Inject class RemoteRulesTodayDataSourceImpl @Inject constructor( @@ -17,41 +17,96 @@ class RemoteRulesTodayDataSourceImpl @Inject constructor( RemoteRulesTodayDataSource { private val ROOM_ID = BuildConfig.ROOM_ID - override suspend fun getTodayTodayInfoList(roomId: String): WrapperClass { - return rulesApi.getTodayTodayInfoList(ROOM_ID) - } + override suspend fun getTodayTodayInfoList(roomId: String): RulesTodayInfo? = + runCatching { + rulesApi.getTodayTodayInfoList(ROOM_ID) + }.fold( + { response -> + return RulesTodayInfo( + response.data?.homeRuleCategories!!.map { it.toCategoryInfo() }, + response.data.todayTodoRules.map { it.toRuleInfo() }) + }, + { + Timber.e(it.message) + return null + } + ) override suspend fun getTempManagerInfoList( roomId: String, ruleId: String - ): WrapperClass { - return rulesApi.getTempManagerInfoList(ROOM_ID, ruleId) - } + ): TempManagerInfo? = + runCatching { + rulesApi.getTempManagerInfoList(ROOM_ID, ruleId) + }.fold( + { response -> + return TempManagerInfo( + response.data!!.id, + response.data.homies.map { it.toHomieInfo() } + ) + }, + { + Timber.e(it.message) + return null + } + ) + override suspend fun putTempManagerInfoList( roomId: String, ruleId: String, tmpRuleMembers: TempManagerRequest - ): WrapperClass { - return rulesApi.putTempManagerInfoList(ROOM_ID, ruleId, tmpRuleMembers) + ): Any? { + return runCatching { + rulesApi.putTempManagerInfoList( + ROOM_ID, + ruleId, + tmpRuleMembers + ) + }.fold( + {}, { + Timber.e(it.message) + null + } + ) } - override suspend fun getMyToDoInfoList(roomId: String): WrapperClass> { - return rulesApi.getMyTodoInfoList(ROOM_ID) + override suspend fun getMyToDoInfoList(roomId: String): List? { + return runCatching { rulesApi.getMyTodoInfoList(ROOM_ID) }.fold( + { response -> + response.data?.map { it.toRuleInfo() } + }, + { + Timber.e(it.message) + null + } + ) } override suspend fun putMyToDoCheckLust( roomId: String, ruleId: String, isCheck: MyToDoCheckRequest - ): WrapperClass { - return rulesApi.putMyToDoCheckList(ROOM_ID, ruleId, isCheck) + ) { + runCatching { rulesApi.putMyToDoCheckList(ROOM_ID, ruleId, isCheck) }.onFailure { + Timber.e(it.message) + } } override suspend fun getRuleTableInfoList( roomId: String, categoryId: String - ): WrapperClass { - return rulesApi.getRuleTableInfoList(ROOM_ID, categoryId) - } + ): RulesTableInfo? = + runCatching { rulesApi.getRuleTableInfoList(ROOM_ID, categoryId) }.fold( + { response -> + return RulesTableInfo( + response.data?.keyRules!!.map { it.toRuleInfo() }, + response.data.rules.map { it.toRuleInfo() } + ) + }, + { + Timber.e(it.message) + null + } + ) }