Skip to content

Commit

Permalink
[refactor] #7 auth repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Sangwook123 committed May 10, 2024
1 parent 9293258 commit c9cc50a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import kotlinx.serialization.json.Json
import org.sopt.domain.repo.AuthRepository
import org.sopt.model.Base
import org.sopt.model.Member
import org.sopt.model.exception.ApiError
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
Expand All @@ -19,8 +18,8 @@ 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 {
override suspend fun postSignup(member: Member, pw: String): Result<Base<Nothing>> =
runCatching {
authApi.postSignup(
RequestPostSignupDto(
authenticationId = member.id,
Expand All @@ -29,59 +28,65 @@ class AuthRepositoryImpl @Inject constructor(
phone = member.phone
)
)
}.handleResult {
it.toCoreModelNothingType()
}
return handleResult(result)
}

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

override suspend fun getUserinfo(): Result<Base<Member>> = runCatching {
authApi.getUserinfo()
}.handleResult {
Base(data = it.data?.toCoreModel(), message = it.message)
}

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


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
)
): Result<Base<Nothing>> = runCatching {
authApi.patchPassword(
RequestPatchPasswordDto(
previousPassword = previousPassword,
newPassword = newPassword,
newPasswordVerification = newPasswordVerification
)
}
return handleResult(result)
)
}.handleResult {
it.toCoreModelNothingType()
}

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()
)
}
private inline fun <reified T, V> Result<BaseResponse<T>>.handleResult(action: (BaseResponse<T>) -> Base<V>) =
this.mapCatching {
action(it)
}.recoverCatching { exception ->
when (exception) {
is HttpException -> {
val json = exception.response()?.errorBody()?.string()
if (json != null) {
val api = Json.decodeFromString<BaseResponse<Nothing>>(json)
val apiError = ApiError(message = api.message)
throw apiError
} else {
throw exception
}
}

else -> {
Result.failure(exception)
else -> {
throw exception
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.sopt.model.Member
interface AuthRepository {
suspend fun postSignup(member: Member, pw: String): Result<Base<Nothing>>

suspend fun getUserinfo(): Result<Member?>
suspend fun getUserinfo(): Result<Base<Member>>

suspend fun postSignin(id: String, pw: String): Result<Base<Nothing>>

Expand Down
1 change: 0 additions & 1 deletion core/model/src/main/java/org/sopt/model/Base.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.sopt.model

data class Base<T>(
val code: Int,
val data: T? = null,
val message: String,
)
Expand Down
5 changes: 5 additions & 0 deletions core/model/src/main/java/org/sopt/model/exception/ApiError.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.sopt.model.exception

data class ApiError(
override val message: String,
): Exception()

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ data class BaseResponse<T>(
)

fun <T> BaseResponse<T>.toCoreModel(): Base<T> = Base(
code = this.code,
data = this.data,
message = this.message
)

fun <T> BaseResponse<T>.toCoreModelNothingType(): Base<Nothing> = Base(
code = this.code,
message = this.message
)

0 comments on commit c9cc50a

Please sign in to comment.