Skip to content

Commit

Permalink
[Mod] #18 코드리뷰반영
Browse files Browse the repository at this point in the history
  • Loading branch information
cacaocoffee committed Jun 14, 2024
1 parent f639c9a commit 06dbdef
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 84 deletions.
21 changes: 0 additions & 21 deletions app/src/main/java/com/sopt/now/data/AuthRepositoryImpl.kt

This file was deleted.

12 changes: 10 additions & 2 deletions app/src/main/java/com/sopt/now/data/dto/Mapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -16,4 +18,10 @@ internal fun AuthRequestModel.toRequestSignUpDto(): RequestSignUpDto =
password = password,
nickname = nickname,
phone = phone
)
)

internal fun BaseResponse<Unit>.toBaseResponseEntity(): BaseResponseEntity =
BaseResponseEntity(
code = this.code,
message = this.message
)
Original file line number Diff line number Diff line change
@@ -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<Int?> =
runCatching {
val response: Response<BaseResponse<Unit>> = authService.login(authData.toRequestLoginDto())
if (response.isSuccessful) {
response.headers()["location"]?.toInt()
}
else{
throw ApiError(response.getResponseErrorMessage())
}
}

override suspend fun signUp(authData: AuthRequestModel): Result<BaseResponseEntity> {
return runCatching {
authService.signUp(authData.toRequestSignUpDto()).toBaseResponseEntity()
}.onFailure { throwable ->
return throwable.handleThrowable()
}
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sopt.now.domain.entity.request
package com.sopt.now.domain.entity


data class AuthRequestModel(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Response<BaseResponse<Unit>>>
suspend fun signUp(authData: AuthRequestModel): Result<Response<BaseResponse<Unit>>>
suspend fun login(authData: AuthRequestModel): Result<Int?>
suspend fun signUp(authData: AuthRequestModel): Result<BaseResponseEntity>
}
8 changes: 3 additions & 5 deletions app/src/main/java/com/sopt/now/domain/usecase/LogInUseCase.kt
Original file line number Diff line number Diff line change
@@ -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<Response<BaseResponse<Unit>>> =
authRepository.logIn(request)
suspend operator fun invoke(request: AuthRequestModel):Result<Int?> =
authRepository.login(request)
}
Original file line number Diff line number Diff line change
@@ -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<Response<BaseResponse<Unit>>> =
suspend operator fun invoke(request: AuthRequestModel):Result<BaseResponseEntity> =
authRepository.signUp(request)
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/sopt/now/ui/ViewModelFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/sopt/now/ui/login/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 11 additions & 22 deletions app/src/main/java/com/sopt/now/ui/login/LoginViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/sopt/now/ui/signup/SignUpActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 9 additions & 21 deletions app/src/main/java/com/sopt/now/ui/signup/SignUpViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -44,9 +37,4 @@ class SignUpViewModel(private val signUpUseCase: SignUpUseCase) : ViewModel() {
}
}
}

companion object {
private const val LOCATION = "location"
private const val MESSAGE = "message"
}
}

0 comments on commit 06dbdef

Please sign in to comment.