generated from NOW-SOPT-ANDROID/now-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41320e4
commit 3a099ca
Showing
3 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
core/data/src/main/java/org/sopt/data/repository/AuthRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
core/data/src/main/java/org/sopt/data/repository/UserRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |