Skip to content

Commit

Permalink
[feat] #7 repo impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Sangwook123 committed May 3, 2024
1 parent 41320e4 commit 3a099ca
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 3 deletions.
13 changes: 10 additions & 3 deletions core/data/src/main/java/org/sopt/data/di/DataModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.sopt.data.repository.AuthRepositoryImpl
import org.sopt.data.repository.FriendRepository
import org.sopt.data.repository.UserDataRepositoryImpl
import org.sopt.data.repository.UserRepositoryImpl
import org.sopt.domain.repo.AuthRepository
import org.sopt.domain.repo.SoptRepository
import org.sopt.domain.repo.UserDataRepository
import javax.inject.Singleton
import org.sopt.domain.repo.UserRepository

@Module
@InstallIn(SingletonComponent::class)
abstract class DataModule {
@Singleton
@Binds
abstract fun bindsFriendRepo(soptRepository: FriendRepository): SoptRepository

@Singleton
@Binds
abstract fun bindsUserRepo(userDataRepository: UserDataRepositoryImpl): UserDataRepository

@Binds
abstract fun bindsMemberRepo(userRepository: UserRepositoryImpl): UserRepository

@Binds
abstract fun authRepo(authRepository: AuthRepositoryImpl): AuthRepository
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.sopt.data.repository

import kotlinx.serialization.json.Json
import org.sopt.domain.repo.AuthRepository
import org.sopt.model.Base
import org.sopt.model.Member
import org.sopt.network.api.AuthApi
import org.sopt.network.model.request.RequestPatchPasswordDto
import org.sopt.network.model.request.RequestPostSigninDto
import org.sopt.network.model.request.RequestPostSignupDto
import org.sopt.network.model.response.base.ApiError
import org.sopt.network.model.response.base.BaseResponse
import org.sopt.network.model.response.base.toCoreModel
import org.sopt.network.model.response.base.toCoreModelNothingType
import org.sopt.network.model.response.toCoreModel
import retrofit2.HttpException
import javax.inject.Inject

class AuthRepositoryImpl @Inject constructor(
private val authApi: AuthApi,
) : AuthRepository {
override suspend fun postSignup(member: Member, pw: String): Result<Base<Nothing>> {
val result = runCatching {
authApi.postSignup(
RequestPostSignupDto(
authenticationId = member.id,
nickname = member.nickname,
password = pw,
phone = member.phone
)
)
}
return handleResult(result)
}

override suspend fun getUserinfo(): Result<Member> = runCatching {
authApi.getUserinfo().data!!.toCoreModel()
}

override suspend fun postSignin(id: String, pw: String): Result<Base<Nothing>> {
val result = runCatching {
authApi.postSignin(
RequestPostSigninDto(
id,
pw
)
)
}
return handleResult(result)
}

override suspend fun patchPassword(
previousPassword: String,
newPassword: String,
newPasswordVerification: String,
): Result<Base<Nothing>> {
val result = runCatching {
authApi.patchPassword(
RequestPatchPasswordDto(
previousPassword = previousPassword,
newPassword = newPassword,
newPasswordVerification = newPasswordVerification
)
)
}
return handleResult(result)
}

private fun handleResult(result: Result<BaseResponse<Unit>>) =
when (val exception = result.exceptionOrNull()) {
null -> result.map {
it.toCoreModelNothingType()
}

is HttpException -> {
val json = exception.response()?.errorBody()?.string()
val api = Json.decodeFromString<ApiError>(json!!)
Result.success(
api.toCoreModel()
)
}

else -> {
Result.failure(exception)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.sopt.data.repository

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import kotlinx.coroutines.flow.Flow
import org.sopt.data.paging.UserPagingSource
import org.sopt.domain.repo.UserRepository
import org.sopt.model.ReqresUser
import javax.inject.Inject

class UserRepositoryImpl @Inject constructor(
private val userPagingSource: UserPagingSource,
) : UserRepository {
override fun getUser(): Flow<PagingData<ReqresUser>> {
return Pager(PagingConfig(6, 2)) {
userPagingSource
}.flow
}
}

0 comments on commit 3a099ca

Please sign in to comment.