From 99b0f7bc3761a158ee2ad207d1904a726b95f4c6 Mon Sep 17 00:00:00 2001 From: sgsk88 Date: Mon, 6 Nov 2023 22:47:06 +0900 Subject: [PATCH] =?UTF-8?q?[feature/api=5Fget=5Funiversity]=20BaseResponse?= =?UTF-8?q?=20=EC=A0=81=EC=9A=A9=20(#83)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../remote/onboarding/OnboardingDataSource.kt | 4 ++-- .../onboarding/OnboardingDataSourceImpl.kt | 7 +++--- .../com/everymeal/data/model/BaseResponse.kt | 14 +++++++++++ .../model/onboarding/GetUniversityData.kt | 23 +++++++------------ .../data/service/onboarding/OnboardingApi.kt | 6 ++--- 5 files changed, 31 insertions(+), 23 deletions(-) create mode 100644 data/src/main/java/com/everymeal/data/model/BaseResponse.kt diff --git a/data/src/main/java/com/everymeal/data/datasource/remote/onboarding/OnboardingDataSource.kt b/data/src/main/java/com/everymeal/data/datasource/remote/onboarding/OnboardingDataSource.kt index f2207ec5..aadb1bb7 100644 --- a/data/src/main/java/com/everymeal/data/datasource/remote/onboarding/OnboardingDataSource.kt +++ b/data/src/main/java/com/everymeal/data/datasource/remote/onboarding/OnboardingDataSource.kt @@ -1,7 +1,7 @@ package com.everymeal.data.datasource.remote.onboarding -import com.everymeal.data.model.onboarding.GetUniversityData +import com.everymeal.data.model.onboarding.UniversityData interface OnboardingDataSource { - suspend fun getUniversity(): Result + suspend fun getUniversity(): Result> } \ No newline at end of file diff --git a/data/src/main/java/com/everymeal/data/datasource/remote/onboarding/OnboardingDataSourceImpl.kt b/data/src/main/java/com/everymeal/data/datasource/remote/onboarding/OnboardingDataSourceImpl.kt index 0d62d44b..5cdafcc5 100644 --- a/data/src/main/java/com/everymeal/data/datasource/remote/onboarding/OnboardingDataSourceImpl.kt +++ b/data/src/main/java/com/everymeal/data/datasource/remote/onboarding/OnboardingDataSourceImpl.kt @@ -1,6 +1,7 @@ package com.everymeal.data.datasource.remote.onboarding -import com.everymeal.data.model.onboarding.GetUniversityData +import com.everymeal.data.model.onboarding.UniversityData +import com.everymeal.data.model.unwrapData import com.everymeal.data.service.onboarding.OnboardingApi import javax.inject.Inject @@ -8,7 +9,7 @@ class OnboardingDataSourceImpl @Inject constructor( private val onboardingApi: OnboardingApi ) : OnboardingDataSource { - override suspend fun getUniversity(): Result { - return runCatching { onboardingApi.getUniversity() } + override suspend fun getUniversity(): Result> { + return runCatching { onboardingApi.getUniversity() }.unwrapData() } } \ No newline at end of file diff --git a/data/src/main/java/com/everymeal/data/model/BaseResponse.kt b/data/src/main/java/com/everymeal/data/model/BaseResponse.kt new file mode 100644 index 00000000..ba187ad5 --- /dev/null +++ b/data/src/main/java/com/everymeal/data/model/BaseResponse.kt @@ -0,0 +1,14 @@ +package com.everymeal.data.model + +import kotlinx.serialization.Serializable + +@Serializable +data class BaseResponse( + val localDateTime: String, + val message: String, + val data: T, +) + +fun Result>.unwrapData(): Result { + return this.map { it.data } +} \ No newline at end of file diff --git a/data/src/main/java/com/everymeal/data/model/onboarding/GetUniversityData.kt b/data/src/main/java/com/everymeal/data/model/onboarding/GetUniversityData.kt index 0bd4386c..86e6779e 100644 --- a/data/src/main/java/com/everymeal/data/model/onboarding/GetUniversityData.kt +++ b/data/src/main/java/com/everymeal/data/model/onboarding/GetUniversityData.kt @@ -4,22 +4,15 @@ import com.everymeal.domain.model.onboarding.GetUniversityEntity import kotlinx.serialization.Serializable @Serializable -data class GetUniversityData( - val localDateTime: String, - val message: String, - val data: List -) { - @Serializable - data class UniversityData( - val idx: Int, - val universityName: String, - val campusName: String, - val universityShortName: String - ) -} +data class UniversityData( + val idx: Int, + val universityName: String, + val campusName: String, + val universityShortName: String +) -fun GetUniversityData.toUniversityEntity(): GetUniversityEntity { - val universityDataList = this.data.map { result -> +fun List.toUniversityEntity(): GetUniversityEntity { + val universityDataList = this.map { result -> GetUniversityEntity.UniversityData( idx = result.idx, universityName = result.universityName, diff --git a/data/src/main/java/com/everymeal/data/service/onboarding/OnboardingApi.kt b/data/src/main/java/com/everymeal/data/service/onboarding/OnboardingApi.kt index 11fabcca..e76d1a0a 100644 --- a/data/src/main/java/com/everymeal/data/service/onboarding/OnboardingApi.kt +++ b/data/src/main/java/com/everymeal/data/service/onboarding/OnboardingApi.kt @@ -1,11 +1,11 @@ package com.everymeal.data.service.onboarding -import com.everymeal.data.model.onboarding.GetUniversityData -import retrofit2.Response +import com.everymeal.data.model.BaseResponse +import com.everymeal.data.model.onboarding.UniversityData import retrofit2.http.GET interface OnboardingApi { @GET("/api/v1/universities") - suspend fun getUniversity(): GetUniversityData + suspend fun getUniversity(): BaseResponse> } \ No newline at end of file