Skip to content

Commit

Permalink
[feature/api_get_university] BaseResponse 적용 (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
SsongSik committed Nov 6, 2023
1 parent 48ebc88 commit 99b0f7b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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<GetUniversityData>
suspend fun getUniversity(): Result<List<UniversityData>>
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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

class OnboardingDataSourceImpl @Inject constructor(
private val onboardingApi: OnboardingApi
) : OnboardingDataSource {

override suspend fun getUniversity(): Result<GetUniversityData> {
return runCatching { onboardingApi.getUniversity() }
override suspend fun getUniversity(): Result<List<UniversityData>> {
return runCatching { onboardingApi.getUniversity() }.unwrapData()
}
}
14 changes: 14 additions & 0 deletions data/src/main/java/com/everymeal/data/model/BaseResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.everymeal.data.model

import kotlinx.serialization.Serializable

@Serializable
data class BaseResponse<T>(
val localDateTime: String,
val message: String,
val data: T,
)

fun <T> Result<BaseResponse<T>>.unwrapData(): Result<T> {
return this.map { it.data }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<UniversityData>
) {
@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<UniversityData>.toUniversityEntity(): GetUniversityEntity {
val universityDataList = this.map { result ->
GetUniversityEntity.UniversityData(
idx = result.idx,
universityName = result.universityName,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<UniversityData>>
}

0 comments on commit 99b0f7b

Please sign in to comment.