Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT/#10] 6주차 필수 과제 #11

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions app/src/main/java/org/sopt/and/core/UserInfo.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.sopt.and.core.data.datasource

import org.sopt.and.core.data.dto.BaseResponse
import org.sopt.and.core.data.dto.response.GetHobbyResponse

interface HobbyDataSource {
suspend fun getHobby(
token: String
): BaseResponse<GetHobbyResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.sopt.and.core.data.datasource

import org.sopt.and.core.data.dto.BaseResponse
import org.sopt.and.core.data.dto.reqeust.LoginRequest
import org.sopt.and.core.data.dto.response.LoginResponse

interface SignInDataSource {
suspend fun postSignIn(
request: LoginRequest
): BaseResponse<LoginResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.sopt.and.core.data.datasource

import org.sopt.and.core.data.dto.BaseResponse
import org.sopt.and.core.data.dto.reqeust.CreateUserRequest
import org.sopt.and.core.data.dto.response.CreateUserResponse

interface SignUpDataSource {
suspend fun postSignUp(
request: CreateUserRequest
): BaseResponse<CreateUserResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.sopt.and.core.data.datasourceimpl

import org.sopt.and.core.data.datasource.HobbyDataSource
import org.sopt.and.core.data.dto.BaseResponse
import org.sopt.and.core.data.dto.response.GetHobbyResponse
import org.sopt.and.core.data.service.HobbyService

class HobbyDataSourceImpl(
private val hobbyService: HobbyService
) : HobbyDataSource {
override suspend fun getHobby(token: String): BaseResponse<GetHobbyResponse> = hobbyService.getMyHobby(token)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.sopt.and.core.data.datasourceimpl

import org.sopt.and.core.data.datasource.SignInDataSource
import org.sopt.and.core.data.dto.BaseResponse
import org.sopt.and.core.data.dto.reqeust.LoginRequest
import org.sopt.and.core.data.dto.response.LoginResponse
import org.sopt.and.core.data.service.SignInService


class SignInDataSourceImpl(
private val signInService: SignInService
) : SignInDataSource {
override suspend fun postSignIn(request: LoginRequest): BaseResponse<LoginResponse> = signInService.login(request)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.sopt.and.core.data.datasourceimpl

import org.sopt.and.core.data.datasource.SignUpDataSource
import org.sopt.and.core.data.dto.BaseResponse
import org.sopt.and.core.data.dto.reqeust.CreateUserRequest
import org.sopt.and.core.data.dto.response.CreateUserResponse
import org.sopt.and.core.data.service.SignUpService

class SignUpDataSourceImpl(
private val signUpService: SignUpService
) : SignUpDataSource {
override suspend fun postSignUp(request: CreateUserRequest): BaseResponse<CreateUserResponse> = signUpService.signUp(request)
}
6 changes: 3 additions & 3 deletions app/src/main/java/org/sopt/and/core/data/dto/BaseResponse.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.sopt.and.core.data.dto

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class BaseResponse <T> (
val status: Int,
val message: String,
val data: T? = null
@SerialName("result")
val result: T
Comment on lines 6 to +9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BaseResponse 수정 굿 ~

)
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ import kotlinx.serialization.Serializable

@Serializable
data class CreateUserResponse(
@SerialName("result")
val result: Result
) {
@Serializable
data class Result(
@SerialName("no")
val no: Int
)
}
@SerialName("no")
val userId: Int
Comment on lines +8 to +9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요런식으로 네이밍을 다르게 할 수 있구나 처음 알았네요!

)
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import kotlinx.serialization.Serializable

@Serializable
data class GetHobbyResponse(
@SerialName("result")
val result: Result
) {
@Serializable
data class Result(
@SerialName("hobby")
val hobby: String
)
}
@SerialName("hobby")
val hobby: String
)

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ import kotlinx.serialization.Serializable

@Serializable
data class LoginResponse(
@SerialName("result")
val result: Result
) {
@Serializable
data class Result(
@SerialName("token")
val token: String
)
}
@SerialName("token")
val token: String
)


Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.sopt.and.core.data.repositoryimpl

import org.sopt.and.core.data.datasource.HobbyDataSource
import org.sopt.and.core.data.dto.response.GetHobbyResponse
import org.sopt.and.domain.entity.HobbyData
import org.sopt.and.domain.entity.SignInData
import org.sopt.and.domain.repository.HobbyRepository

class HobbyRepositoryImpl(private val hobbyDataSource: HobbyDataSource) : HobbyRepository {
override suspend fun getHobby(token: String): Result<HobbyData> = runCatching {
val response = hobbyDataSource.getHobby(token)
HobbyData(response.result.hobby)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.sopt.and.core.data.repositoryimpl

import org.sopt.and.core.data.datasource.SignInDataSource
import org.sopt.and.core.data.dto.reqeust.LoginRequest
import org.sopt.and.domain.entity.SignInData
import org.sopt.and.domain.repository.SignInRepository

class SignInRepositoryImpl(private val signInDataSource: SignInDataSource) : SignInRepository {
override suspend fun signIn(username: String, password: String): Result<SignInData> = runCatching {
val response = signInDataSource.postSignIn(LoginRequest(username, password))
SignInData(response.result.token)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.sopt.and.core.data.repositoryimpl

import org.sopt.and.core.data.datasource.SignUpDataSource
import org.sopt.and.core.data.dto.reqeust.CreateUserRequest
import org.sopt.and.core.data.dto.response.CreateUserResponse
import org.sopt.and.domain.entity.SignUpData
import org.sopt.and.domain.repository.SignUpRepository

class SignUpRepositoryImpl(private val signUpDataSource: SignUpDataSource) : SignUpRepository {
override suspend fun signUp(username: String, password: String, hobby: String): Result<SignUpData> = runCatching {
val response = signUpDataSource.postSignUp(CreateUserRequest(username, password, hobby))
SignUpData(response.result.userId)
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/org/sopt/and/core/data/service/HobbyService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.sopt.and.core.data.service

import org.sopt.and.core.data.dto.BaseResponse
import org.sopt.and.core.data.dto.response.GetHobbyResponse
import retrofit2.http.GET
import retrofit2.http.Header

interface HobbyService {
@GET("user/my-hobby")
suspend fun getMyHobby(
@Header("token") token: String
): BaseResponse<GetHobbyResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sopt.and.core.data.service

import org.sopt.and.network.RetrofitInstance

object ServicePool {
val signInService: SignInService = RetrofitInstance.create<SignInService>()
val signUpService: SignUpService = RetrofitInstance.create<SignUpService>()
val hobbyService: HobbyService = RetrofitInstance.create<HobbyService>()
}
14 changes: 14 additions & 0 deletions app/src/main/java/org/sopt/and/core/data/service/SignInService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.sopt.and.core.data.service

import org.sopt.and.core.data.dto.BaseResponse
import org.sopt.and.core.data.dto.reqeust.LoginRequest
import org.sopt.and.core.data.dto.response.LoginResponse
import retrofit2.http.Body
import retrofit2.http.POST

interface SignInService {
@POST("login")
suspend fun login(
@Body request: LoginRequest
): BaseResponse<LoginResponse>
}
14 changes: 14 additions & 0 deletions app/src/main/java/org/sopt/and/core/data/service/SignUpService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.sopt.and.core.data.service

import org.sopt.and.core.data.dto.BaseResponse
import org.sopt.and.core.data.dto.reqeust.CreateUserRequest
import org.sopt.and.core.data.dto.response.CreateUserResponse
import retrofit2.http.Body
import retrofit2.http.POST

interface SignUpService {
@POST("user")
suspend fun signUp(
@Body request: CreateUserRequest
): BaseResponse<CreateUserResponse>
}
23 changes: 23 additions & 0 deletions app/src/main/java/org/sopt/and/core/util/DefaultErrorHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.sopt.and.core.util

import android.content.Context
import retrofit2.HttpException
import org.sopt.and.R

interface ErrorHandler {
fun handleNetworkError(exception: Throwable?): String
}

class DefaultErrorHandler(private val context: Context) : ErrorHandler {
override fun handleNetworkError(exception: Throwable?): String {
return when (exception) {
is HttpException -> when (exception.code()) {
400 -> context.getString(R.string.network_error_400)
403 -> context.getString(R.string.network_error_403)
409 -> context.getString(R.string.network_error_409)
else -> context.getString(R.string.network_error)
}
else -> context.getString(R.string.network_error)
}
}
}
Comment on lines +11 to +23

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DefaultErrorHandler 좋네요
모든 API의 에러 메시지가 각 상태코드별로 서로 동일한지도 확인해보면 좋을 것 같아요 ~

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

에러 핸들링 함수 좋네요!!

5 changes: 5 additions & 0 deletions app/src/main/java/org/sopt/and/domain/entity/HobbyData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.sopt.and.domain.entity

data class HobbyData(
val hobby: String
)
6 changes: 6 additions & 0 deletions app/src/main/java/org/sopt/and/domain/entity/SignInData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.sopt.and.domain.entity

data class SignInData(
val token: String
)

5 changes: 5 additions & 0 deletions app/src/main/java/org/sopt/and/domain/entity/SignUpData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.sopt.and.domain.entity

data class SignUpData(
val userId: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.sopt.and.domain.repository

import org.sopt.and.core.data.dto.response.GetHobbyResponse

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분도 안쓰는 import인 것 같아용

import org.sopt.and.domain.entity.HobbyData

interface HobbyRepository {
suspend fun getHobby(token: String): Result<HobbyData>
}
23 changes: 23 additions & 0 deletions app/src/main/java/org/sopt/and/domain/repository/RepositoryPool.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.sopt.and.domain.repository

import org.sopt.and.core.data.datasourceimpl.HobbyDataSourceImpl
import org.sopt.and.core.data.datasourceimpl.SignInDataSourceImpl
import org.sopt.and.core.data.datasourceimpl.SignUpDataSourceImpl
import org.sopt.and.core.data.repositoryimpl.HobbyRepositoryImpl
import org.sopt.and.core.data.repositoryimpl.SignInRepositoryImpl
import org.sopt.and.core.data.repositoryimpl.SignUpRepositoryImpl
import org.sopt.and.core.data.service.ServicePool

object RepositoryPool {
val hobbyRepository: HobbyRepository by lazy {
HobbyRepositoryImpl(HobbyDataSourceImpl(ServicePool.hobbyService))
}

val signInRepository: SignInRepository by lazy {
SignInRepositoryImpl(SignInDataSourceImpl(ServicePool.signInService))
}

val signUpRepository: SignUpRepository by lazy {
SignUpRepositoryImpl(SignUpDataSourceImpl(ServicePool.signUpService))
}
}
Comment on lines +11 to +23

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 식으로 의존성 주입 로직에 관련된 코드는, viewmodelfactory라는 이름으로 생성해줘도 될 것 같아요 ~

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

domain 레이어에는 다른 계층, 또는 서드파티 관련 import는 없어야한다고 알고있어용 RepositoryPool을 다른 곳으로 옮기는게 어떨까요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.sopt.and.domain.repository

import org.sopt.and.domain.entity.SignInData

interface SignInRepository {
suspend fun signIn(username: String, password: String): Result<SignInData>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.sopt.and.domain.repository

import org.sopt.and.core.data.dto.response.CreateUserResponse

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data 레이어 관련 import가 있어서 봤더니, 이 코드에서는 안 쓰이는 import인 것 같네요! 지워줍시다 ~

import org.sopt.and.domain.entity.SignUpData

interface SignUpRepository {
suspend fun signUp(username: String, password: String, hobby: String): Result<SignUpData>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request에 들어가는 부분도, model을 하나 생성해서 써줘도 좋을 것 같아요 !

}
3 changes: 1 addition & 2 deletions app/src/main/java/org/sopt/and/navigation/NavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import org.sopt.and.core.UserInfo
import org.sopt.and.presentation.home.HomeScreen
import org.sopt.and.presentation.mypage.MyPageScreen
import org.sopt.and.presentation.search.SearchScreen
Expand All @@ -22,7 +21,7 @@ sealed class Screen(val route: String) {
}

@Composable
fun NavGraph(navController: NavHostController, userInfo: UserInfo) {
fun NavGraph(navController: NavHostController) {
NavHost(
navController = navController,
startDestination = Screen.SignIn.route
Expand Down
Loading