From 06dbdef988d4dd5225ede3699a2d871aa79bbac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AA=85=EC=84=9D?= Date: Sat, 15 Jun 2024 05:34:29 +0900 Subject: [PATCH] =?UTF-8?q?[Mod]=20#18=20=EC=BD=94=EB=93=9C=EB=A6=AC?= =?UTF-8?q?=EB=B7=B0=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/sopt/now/data/AuthRepositoryImpl.kt | 21 ----------- .../main/java/com/sopt/now/data/dto/Mapper.kt | 12 +++++- .../data/repositoryimpl/AuthRepositoryImpl.kt | 37 +++++++++++++++++++ .../entity/{request => }/AuthRequestModel.kt | 2 +- .../now/domain/repository/AuthRepository.kt | 9 ++--- .../sopt/now/domain/usecase/LogInUseCase.kt | 8 ++-- .../sopt/now/domain/usecase/SignUpUseCase.kt | 7 ++-- .../java/com/sopt/now/ui/ViewModelFactory.kt | 2 +- .../com/sopt/now/ui/login/LoginActivity.kt | 2 +- .../com/sopt/now/ui/login/LoginViewModel.kt | 33 ++++++----------- .../com/sopt/now/ui/signup/SignUpActivity.kt | 2 +- .../com/sopt/now/ui/signup/SignUpViewModel.kt | 30 +++++---------- 12 files changed, 81 insertions(+), 84 deletions(-) delete mode 100644 app/src/main/java/com/sopt/now/data/AuthRepositoryImpl.kt create mode 100644 app/src/main/java/com/sopt/now/data/repositoryimpl/AuthRepositoryImpl.kt rename app/src/main/java/com/sopt/now/domain/entity/{request => }/AuthRequestModel.kt (76%) diff --git a/app/src/main/java/com/sopt/now/data/AuthRepositoryImpl.kt b/app/src/main/java/com/sopt/now/data/AuthRepositoryImpl.kt deleted file mode 100644 index 7a6248b..0000000 --- a/app/src/main/java/com/sopt/now/data/AuthRepositoryImpl.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.sopt.now.data - -import com.sopt.now.data.api.AuthService -import com.sopt.now.data.dto.response.BaseResponse -import com.sopt.now.data.dto.toRequestLoginDto -import com.sopt.now.data.dto.toRequestSignUpDto -import com.sopt.now.domain.entity.request.AuthRequestModel -import com.sopt.now.domain.repository.AuthRepository -import retrofit2.Response - -class AuthRepositoryImpl(private val authService: AuthService) : AuthRepository { - override suspend fun logIn(authData: AuthRequestModel): Result>> = - runCatching { - authService.login(authData.toRequestLoginDto()) - } - - override suspend fun signUp(authData: AuthRequestModel): Result>> = - runCatching { - authService.signUp(authData.toRequestSignUpDto()) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/data/dto/Mapper.kt b/app/src/main/java/com/sopt/now/data/dto/Mapper.kt index a084a7d..b3d1a87 100644 --- a/app/src/main/java/com/sopt/now/data/dto/Mapper.kt +++ b/app/src/main/java/com/sopt/now/data/dto/Mapper.kt @@ -2,7 +2,9 @@ package com.sopt.now.data.dto import com.sopt.now.data.dto.request.RequestLoginDto import com.sopt.now.data.dto.request.RequestSignUpDto -import com.sopt.now.domain.entity.request.AuthRequestModel +import com.sopt.now.data.dto.response.BaseResponse +import com.sopt.now.domain.entity.AuthRequestModel +import com.sopt.now.domain.entity.BaseResponseEntity internal fun AuthRequestModel.toRequestLoginDto(): RequestLoginDto = RequestLoginDto( @@ -16,4 +18,10 @@ internal fun AuthRequestModel.toRequestSignUpDto(): RequestSignUpDto = password = password, nickname = nickname, phone = phone - ) \ No newline at end of file + ) + +internal fun BaseResponse.toBaseResponseEntity(): BaseResponseEntity = + BaseResponseEntity( + code = this.code, + message = this.message + ) diff --git a/app/src/main/java/com/sopt/now/data/repositoryimpl/AuthRepositoryImpl.kt b/app/src/main/java/com/sopt/now/data/repositoryimpl/AuthRepositoryImpl.kt new file mode 100644 index 0000000..6ff71fe --- /dev/null +++ b/app/src/main/java/com/sopt/now/data/repositoryimpl/AuthRepositoryImpl.kt @@ -0,0 +1,37 @@ +package com.sopt.now.data.repositoryimpl + +import com.sopt.now.data.api.AuthService +import com.sopt.now.data.dto.response.BaseResponse +import com.sopt.now.data.dto.toBaseResponseEntity +import com.sopt.now.data.dto.toRequestLoginDto +import com.sopt.now.data.dto.toRequestSignUpDto +import com.sopt.now.data.repositoryimpl.extension.getResponseErrorMessage +import com.sopt.now.data.repositoryimpl.extension.handleThrowable +import com.sopt.now.domain.entity.ApiError +import com.sopt.now.domain.entity.AuthRequestModel +import com.sopt.now.domain.entity.BaseResponseEntity +import com.sopt.now.domain.repository.AuthRepository +import retrofit2.Response + +class AuthRepositoryImpl(private val authService: AuthService) : AuthRepository { + override suspend fun login(authData: AuthRequestModel): Result = + runCatching { + val response: Response> = authService.login(authData.toRequestLoginDto()) + if (response.isSuccessful) { + response.headers()["location"]?.toInt() + } + else{ + throw ApiError(response.getResponseErrorMessage()) + } + } + + override suspend fun signUp(authData: AuthRequestModel): Result { + return runCatching { + authService.signUp(authData.toRequestSignUpDto()).toBaseResponseEntity() + }.onFailure { throwable -> + return throwable.handleThrowable() + } + } + + +} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/domain/entity/request/AuthRequestModel.kt b/app/src/main/java/com/sopt/now/domain/entity/AuthRequestModel.kt similarity index 76% rename from app/src/main/java/com/sopt/now/domain/entity/request/AuthRequestModel.kt rename to app/src/main/java/com/sopt/now/domain/entity/AuthRequestModel.kt index d01a796..17e06e1 100644 --- a/app/src/main/java/com/sopt/now/domain/entity/request/AuthRequestModel.kt +++ b/app/src/main/java/com/sopt/now/domain/entity/AuthRequestModel.kt @@ -1,4 +1,4 @@ -package com.sopt.now.domain.entity.request +package com.sopt.now.domain.entity data class AuthRequestModel( diff --git a/app/src/main/java/com/sopt/now/domain/repository/AuthRepository.kt b/app/src/main/java/com/sopt/now/domain/repository/AuthRepository.kt index ee06166..89ac3d5 100644 --- a/app/src/main/java/com/sopt/now/domain/repository/AuthRepository.kt +++ b/app/src/main/java/com/sopt/now/domain/repository/AuthRepository.kt @@ -1,10 +1,9 @@ package com.sopt.now.domain.repository -import com.sopt.now.data.dto.response.BaseResponse -import com.sopt.now.domain.entity.request.AuthRequestModel -import retrofit2.Response +import com.sopt.now.domain.entity.AuthRequestModel +import com.sopt.now.domain.entity.BaseResponseEntity interface AuthRepository { - suspend fun logIn(authData: AuthRequestModel): Result>> - suspend fun signUp(authData: AuthRequestModel): Result>> + suspend fun login(authData: AuthRequestModel): Result + suspend fun signUp(authData: AuthRequestModel): Result } \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/domain/usecase/LogInUseCase.kt b/app/src/main/java/com/sopt/now/domain/usecase/LogInUseCase.kt index 44e6d7e..9e4c006 100644 --- a/app/src/main/java/com/sopt/now/domain/usecase/LogInUseCase.kt +++ b/app/src/main/java/com/sopt/now/domain/usecase/LogInUseCase.kt @@ -1,12 +1,10 @@ package com.sopt.now.domain.usecase -import com.sopt.now.data.dto.response.BaseResponse -import com.sopt.now.domain.entity.request.AuthRequestModel +import com.sopt.now.domain.entity.AuthRequestModel import com.sopt.now.domain.repository.AuthRepository -import retrofit2.Response class LogInUseCase(private val authRepository: AuthRepository) { - suspend operator fun invoke(request: AuthRequestModel):Result>> = - authRepository.logIn(request) + suspend operator fun invoke(request: AuthRequestModel):Result = + authRepository.login(request) } \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/domain/usecase/SignUpUseCase.kt b/app/src/main/java/com/sopt/now/domain/usecase/SignUpUseCase.kt index 7fcccc2..9dc86d5 100644 --- a/app/src/main/java/com/sopt/now/domain/usecase/SignUpUseCase.kt +++ b/app/src/main/java/com/sopt/now/domain/usecase/SignUpUseCase.kt @@ -1,12 +1,11 @@ package com.sopt.now.domain.usecase -import com.sopt.now.data.dto.response.BaseResponse -import com.sopt.now.domain.entity.request.AuthRequestModel +import com.sopt.now.domain.entity.AuthRequestModel +import com.sopt.now.domain.entity.BaseResponseEntity import com.sopt.now.domain.repository.AuthRepository -import retrofit2.Response class SignUpUseCase(private val authRepository: AuthRepository) { - suspend operator fun invoke(request: AuthRequestModel):Result>> = + suspend operator fun invoke(request: AuthRequestModel):Result = authRepository.signUp(request) } \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/ui/ViewModelFactory.kt b/app/src/main/java/com/sopt/now/ui/ViewModelFactory.kt index 502e916..7f45e47 100644 --- a/app/src/main/java/com/sopt/now/ui/ViewModelFactory.kt +++ b/app/src/main/java/com/sopt/now/ui/ViewModelFactory.kt @@ -3,7 +3,7 @@ package com.sopt.now.ui import SignUpViewModel import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider -import com.sopt.now.data.AuthRepositoryImpl +import com.sopt.now.data.repositoryimpl.AuthRepositoryImpl import com.sopt.now.data.api.ServicePool import com.sopt.now.domain.usecase.LogInUseCase import com.sopt.now.domain.usecase.SignUpUseCase diff --git a/app/src/main/java/com/sopt/now/ui/login/LoginActivity.kt b/app/src/main/java/com/sopt/now/ui/login/LoginActivity.kt index 7fdd29e..c07a910 100644 --- a/app/src/main/java/com/sopt/now/ui/login/LoginActivity.kt +++ b/app/src/main/java/com/sopt/now/ui/login/LoginActivity.kt @@ -8,7 +8,7 @@ import androidx.activity.viewModels import com.sopt.now.R import com.sopt.now.ui.model.User import com.sopt.now.databinding.ActivityLoginBinding -import com.sopt.now.domain.entity.request.AuthRequestModel +import com.sopt.now.domain.entity.AuthRequestModel import com.sopt.now.ui.main.MainActivity import com.sopt.now.ui.signup.SignUpActivity import com.sopt.now.ui.ViewModelFactory diff --git a/app/src/main/java/com/sopt/now/ui/login/LoginViewModel.kt b/app/src/main/java/com/sopt/now/ui/login/LoginViewModel.kt index 055cdc9..c4d49f7 100644 --- a/app/src/main/java/com/sopt/now/ui/login/LoginViewModel.kt +++ b/app/src/main/java/com/sopt/now/ui/login/LoginViewModel.kt @@ -4,7 +4,7 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.sopt.now.ui.model.User -import com.sopt.now.domain.entity.request.AuthRequestModel +import com.sopt.now.domain.entity.AuthRequestModel import com.sopt.now.domain.usecase.LogInUseCase import com.sopt.now.util.StringNetworkError.FAIL_ERROR import com.sopt.now.util.StringNetworkError.LOGIN @@ -22,30 +22,19 @@ class LoginViewModel(private val logInUseCase: LogInUseCase) : ViewModel() { viewModelScope.launch { logInUseCase(request) .onSuccess { response -> - val userId = response.headers()[LOCATION].toString() - if (response.isSuccessful) - _loginState.value = - UiState.Success( - User( - request.authenticationId, - request.password, - "", - "", - userId = userId - ) + _loginState.value = + UiState.Success( + User( + request.authenticationId, + request.password, + "", + "", + userId = response.toString() ) - else { - val errorMessage = - JSONObject(response.errorBody()?.string()).getString(MESSAGE) - _loginState.value = UiState.Error(errorMessage.toString()) - } + ) } .onFailure { e -> - if (e is HttpException) { - _loginState.value = UiState.Error(e.message()) - } else { - _loginState.value = UiState.Error(FAIL_ERROR.format(LOGIN)) - } + _loginState.value = UiState.Error(e.message.toString()) } } } diff --git a/app/src/main/java/com/sopt/now/ui/signup/SignUpActivity.kt b/app/src/main/java/com/sopt/now/ui/signup/SignUpActivity.kt index 5955186..fbbdbc5 100644 --- a/app/src/main/java/com/sopt/now/ui/signup/SignUpActivity.kt +++ b/app/src/main/java/com/sopt/now/ui/signup/SignUpActivity.kt @@ -7,7 +7,7 @@ import androidx.activity.viewModels import com.sopt.now.R import com.sopt.now.ui.model.User import com.sopt.now.databinding.ActivitySignUpBinding -import com.sopt.now.domain.entity.request.AuthRequestModel +import com.sopt.now.domain.entity.AuthRequestModel import com.sopt.now.ui.ViewModelFactory import com.sopt.now.ui.login.LoginActivity import com.sopt.now.ui.login.LoginActivity.Companion.TAG_USER diff --git a/app/src/main/java/com/sopt/now/ui/signup/SignUpViewModel.kt b/app/src/main/java/com/sopt/now/ui/signup/SignUpViewModel.kt index b0e3893..0f4b123 100644 --- a/app/src/main/java/com/sopt/now/ui/signup/SignUpViewModel.kt +++ b/app/src/main/java/com/sopt/now/ui/signup/SignUpViewModel.kt @@ -2,7 +2,7 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.sopt.now.ui.model.User -import com.sopt.now.domain.entity.request.AuthRequestModel +import com.sopt.now.domain.entity.AuthRequestModel import com.sopt.now.domain.usecase.SignUpUseCase import com.sopt.now.util.UiState import kotlinx.coroutines.launch @@ -18,22 +18,15 @@ class SignUpViewModel(private val signUpUseCase: SignUpUseCase) : ViewModel() { viewModelScope.launch { signUpUseCase(request) .onSuccess { response -> - if (response.isSuccessful) { - _signUpState.value = - UiState.Success( - User( - request.authenticationId, - request.password, - request.nickname, - request.phone, - response.headers()[LOCATION].toString() - ) + _signUpState.value = + UiState.Success( + User( + request.authenticationId, + request.password, + request.nickname, + request.phone, ) - } else { - val errorMessage = - JSONObject(response.errorBody()?.string()).getString(MESSAGE) - _signUpState.value = UiState.Error(errorMessage.toString()) - } + ) } .onFailure { e -> if (e is HttpException) { @@ -44,9 +37,4 @@ class SignUpViewModel(private val signUpUseCase: SignUpUseCase) : ViewModel() { } } } - - companion object { - private const val LOCATION = "location" - private const val MESSAGE = "message" - } }