From 386777be1b2d73be8c70e25ec0ae24dc9d3d9c9e Mon Sep 17 00:00:00 2001 From: b1urrrr Date: Fri, 12 May 2023 23:17:06 +0900 Subject: [PATCH] =?UTF-8?q?[ADD/#8]=20get=20follower=20list=20=EC=84=9C?= =?UTF-8?q?=EB=B2=84=ED=86=B5=EC=8B=A0=20=ED=95=A8=EC=88=98=20=EC=84=A4?= =?UTF-8?q?=EA=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 1 + .../response/ResponseGetFollowerListDto.kt | 51 +++++++++++++++++++ .../data/repository/FollowerRepositoryImpl.kt | 15 ++++++ .../go/sopt/data/service/FollowerService.kt | 12 +++++ .../data/source/remote/FollowerDataSource.kt | 12 +++++ .../org/android/go/sopt/di/RetrofitModule.kt | 21 +++++++- .../org/android/go/sopt/di/ServiceModule.kt | 6 +++ .../android/go/sopt/domain/model/Follower.kt | 7 +++ .../domain/repository/FollowerRepository.kt | 7 +++ .../android/go/sopt/util/type/BaseUrlType.kt | 5 ++ 10 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/org/android/go/sopt/data/entity/remote/response/ResponseGetFollowerListDto.kt create mode 100644 app/src/main/java/org/android/go/sopt/data/repository/FollowerRepositoryImpl.kt create mode 100644 app/src/main/java/org/android/go/sopt/data/service/FollowerService.kt create mode 100644 app/src/main/java/org/android/go/sopt/data/source/remote/FollowerDataSource.kt create mode 100644 app/src/main/java/org/android/go/sopt/domain/model/Follower.kt create mode 100644 app/src/main/java/org/android/go/sopt/domain/repository/FollowerRepository.kt create mode 100644 app/src/main/java/org/android/go/sopt/util/type/BaseUrlType.kt diff --git a/app/build.gradle b/app/build.gradle index b04f5ca..3fce873 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,6 +25,7 @@ android { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" buildConfigField "String", "BASE_URL", properties['base.url'] + buildConfigField "String", "REQRES_URL", properties['reqres.url'] } buildTypes { diff --git a/app/src/main/java/org/android/go/sopt/data/entity/remote/response/ResponseGetFollowerListDto.kt b/app/src/main/java/org/android/go/sopt/data/entity/remote/response/ResponseGetFollowerListDto.kt new file mode 100644 index 0000000..57b83cc --- /dev/null +++ b/app/src/main/java/org/android/go/sopt/data/entity/remote/response/ResponseGetFollowerListDto.kt @@ -0,0 +1,51 @@ +package org.android.go.sopt.data.entity.remote.response + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import org.android.go.sopt.domain.model.Follower + +@Serializable +data class ResponseGetFollowerListDto( + @SerialName("page") + val page: Int, + @SerialName("per_page") + val perPage: Int, + @SerialName("total") + val total: Int, + @SerialName("total_pages") + val totalPages: Int, + @SerialName("data") + val data: List, + @SerialName("support") + val support: Support, +) { + @Serializable + data class Follower( + @SerialName("id") + val id: Int, + @SerialName("email") + val email: String, + @SerialName("first_name") + val firstName: String, + @SerialName("last_name") + val lastName: String, + @SerialName("avatar") + val avatar: String, + ) + + @Serializable + data class Support( + @SerialName("url") + val url: String, + @SerialName("text") + val text: String, + ) + + fun toFollower() = data.map { follower -> + Follower( + name = "${follower.firstName} ${follower.lastName}", + profile = follower.avatar, + email = follower.email, + ) + } +} diff --git a/app/src/main/java/org/android/go/sopt/data/repository/FollowerRepositoryImpl.kt b/app/src/main/java/org/android/go/sopt/data/repository/FollowerRepositoryImpl.kt new file mode 100644 index 0000000..066452f --- /dev/null +++ b/app/src/main/java/org/android/go/sopt/data/repository/FollowerRepositoryImpl.kt @@ -0,0 +1,15 @@ +package org.android.go.sopt.data.repository + +import org.android.go.sopt.data.source.remote.FollowerDataSource +import org.android.go.sopt.domain.model.Follower +import org.android.go.sopt.domain.repository.FollowerRepository +import javax.inject.Inject + +class FollowerRepositoryImpl @Inject constructor( + private val followerDataSource: FollowerDataSource, +) : FollowerRepository { + override suspend fun getFollowerList(page: Int): Result> = + runCatching { + followerDataSource.getFollowerList(page).toFollower() + } +} diff --git a/app/src/main/java/org/android/go/sopt/data/service/FollowerService.kt b/app/src/main/java/org/android/go/sopt/data/service/FollowerService.kt new file mode 100644 index 0000000..6d6f72d --- /dev/null +++ b/app/src/main/java/org/android/go/sopt/data/service/FollowerService.kt @@ -0,0 +1,12 @@ +package org.android.go.sopt.data.service + +import org.android.go.sopt.data.entity.remote.response.ResponseGetFollowerListDto +import retrofit2.http.GET +import retrofit2.http.Query + +interface FollowerService { + @GET("users") + fun getFollowerList( + @Query("page") page: Int, + ): ResponseGetFollowerListDto +} diff --git a/app/src/main/java/org/android/go/sopt/data/source/remote/FollowerDataSource.kt b/app/src/main/java/org/android/go/sopt/data/source/remote/FollowerDataSource.kt new file mode 100644 index 0000000..9896650 --- /dev/null +++ b/app/src/main/java/org/android/go/sopt/data/source/remote/FollowerDataSource.kt @@ -0,0 +1,12 @@ +package org.android.go.sopt.data.source.remote + +import org.android.go.sopt.data.entity.remote.response.ResponseGetFollowerListDto +import org.android.go.sopt.data.service.FollowerService +import javax.inject.Inject + +class FollowerDataSource @Inject constructor( + private val followerService: FollowerService, +) { + fun getFollowerList(page: Int): ResponseGetFollowerListDto = + followerService.getFollowerList(page) +} diff --git a/app/src/main/java/org/android/go/sopt/di/RetrofitModule.kt b/app/src/main/java/org/android/go/sopt/di/RetrofitModule.kt index 8707df3..5d486f0 100644 --- a/app/src/main/java/org/android/go/sopt/di/RetrofitModule.kt +++ b/app/src/main/java/org/android/go/sopt/di/RetrofitModule.kt @@ -10,7 +10,10 @@ import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import org.android.go.sopt.BuildConfig.BASE_URL +import org.android.go.sopt.BuildConfig.REQRES_URL +import org.android.go.sopt.util.type.BaseUrlType import retrofit2.Retrofit +import javax.inject.Qualifier import javax.inject.Singleton @Module @@ -31,7 +34,8 @@ object RetrofitModule { @Provides @Singleton - fun providesRetrofit( + @Retrofit2(BaseUrlType.SOPT) + fun providesSoptRetrofit( client: OkHttpClient, ): Retrofit = Retrofit.Builder() @@ -39,4 +43,19 @@ object RetrofitModule { .addConverterFactory(Json.asConverterFactory(APPLICATION_JSON.toMediaType())) .client(client) .build() + + @Provides + @Singleton + @Retrofit2(BaseUrlType.REQRES) + fun providesReqresRetrofit( + client: OkHttpClient, + ): Retrofit = + Retrofit.Builder() + .baseUrl(REQRES_URL) + .addConverterFactory(Json.asConverterFactory(APPLICATION_JSON.toMediaType())) + .client(client) + .build() + + @Qualifier + annotation class Retrofit2(val baseUrlType: BaseUrlType) } diff --git a/app/src/main/java/org/android/go/sopt/di/ServiceModule.kt b/app/src/main/java/org/android/go/sopt/di/ServiceModule.kt index 7153202..2d88b02 100644 --- a/app/src/main/java/org/android/go/sopt/di/ServiceModule.kt +++ b/app/src/main/java/org/android/go/sopt/di/ServiceModule.kt @@ -5,6 +5,7 @@ import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent import org.android.go.sopt.data.service.AuthService +import org.android.go.sopt.data.service.FollowerService import retrofit2.Retrofit import javax.inject.Singleton @@ -15,4 +16,9 @@ object ServiceModule { @Singleton fun providesAuthService(retrofit: Retrofit): AuthService = retrofit.create(AuthService::class.java) + + @Provides + @Singleton + fun providesFollowerService(retrofit: Retrofit): FollowerService = + retrofit.create(FollowerService::class.java) } diff --git a/app/src/main/java/org/android/go/sopt/domain/model/Follower.kt b/app/src/main/java/org/android/go/sopt/domain/model/Follower.kt new file mode 100644 index 0000000..b01a604 --- /dev/null +++ b/app/src/main/java/org/android/go/sopt/domain/model/Follower.kt @@ -0,0 +1,7 @@ +package org.android.go.sopt.domain.model + +data class Follower( + val name: String, + val profile: String, + val email: String, +) diff --git a/app/src/main/java/org/android/go/sopt/domain/repository/FollowerRepository.kt b/app/src/main/java/org/android/go/sopt/domain/repository/FollowerRepository.kt new file mode 100644 index 0000000..8e873d4 --- /dev/null +++ b/app/src/main/java/org/android/go/sopt/domain/repository/FollowerRepository.kt @@ -0,0 +1,7 @@ +package org.android.go.sopt.domain.repository + +import org.android.go.sopt.domain.model.Follower + +interface FollowerRepository { + suspend fun getFollowerList(page: Int): Result> +} diff --git a/app/src/main/java/org/android/go/sopt/util/type/BaseUrlType.kt b/app/src/main/java/org/android/go/sopt/util/type/BaseUrlType.kt new file mode 100644 index 0000000..a03a46c --- /dev/null +++ b/app/src/main/java/org/android/go/sopt/util/type/BaseUrlType.kt @@ -0,0 +1,5 @@ +package org.android.go.sopt.util.type + +enum class BaseUrlType { + SOPT, REQRES +}