generated from AND-SOPT-ANDROID/and-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.
[feature] #9 - repository, usecase 구현
- Loading branch information
Showing
25 changed files
with
313 additions
and
167 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,47 @@ | ||
package org.sopt.and.data.mapper | ||
|
||
import org.sopt.and.data.model.request.SignInRequestDto | ||
import org.sopt.and.data.model.request.SignUpRequestDto | ||
import org.sopt.and.data.model.response.GetMyHobbyResponseResultDto | ||
import org.sopt.and.data.model.response.SignInResponseDto | ||
import org.sopt.and.data.model.response.SignUpResponseDto | ||
import org.sopt.and.domain.model.MyHobbyEntity | ||
import org.sopt.and.domain.model.SignInInformationEntity | ||
import org.sopt.and.domain.model.SignInResponseEntity | ||
import org.sopt.and.domain.model.SignUpInformationEntity | ||
import org.sopt.and.domain.model.SignUpResponseEntity | ||
import retrofit2.Response | ||
|
||
object Mapper { | ||
fun toMyHobbyEntity(getHobbyResponseResultDto: GetMyHobbyResponseResultDto) = | ||
MyHobbyEntity(myHobby = getHobbyResponseResultDto.myHobby) | ||
|
||
fun toSignUpResponseEntity(signUpResponseDto: Response<SignUpResponseDto>) = | ||
signUpResponseDto.body()?.result?.let { | ||
SignUpResponseEntity( | ||
no = it.no, | ||
status = signUpResponseDto.code(), | ||
code = signUpResponseDto.body()!!.code | ||
) | ||
} | ||
|
||
fun toSignInResponseEntity(signInResponseDto: Response<SignInResponseDto>) = | ||
signInResponseDto.body()?.result?.let { | ||
SignInResponseEntity( | ||
token = it.token, | ||
status = signInResponseDto.code(), | ||
code = signInResponseDto.body()!!.code | ||
) | ||
} | ||
|
||
fun toSignInRequestDto(signInInformationEntity: SignInInformationEntity) = SignInRequestDto( | ||
username = signInInformationEntity.username, | ||
password = signInInformationEntity.password | ||
) | ||
|
||
fun toSignUpRequestDto(signUpInformationEntity: SignUpInformationEntity) = SignUpRequestDto( | ||
username = signUpInformationEntity.username, | ||
password = signUpInformationEntity.password, | ||
hobby = signUpInformationEntity.hobby | ||
) | ||
} |
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
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
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
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,36 @@ | ||
package org.sopt.and.data.service | ||
|
||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import org.sopt.and.BuildConfig | ||
import retrofit2.Retrofit | ||
|
||
object ApiFactory { | ||
private const val BASE_URL: String = BuildConfig.BASE_URL | ||
|
||
private val okHttpClient: OkHttpClient by lazy { | ||
OkHttpClient.Builder() | ||
.addInterceptor(AuthInterceptor(AppContext.get())) | ||
.addInterceptor(HttpLoggingInterceptor().apply { | ||
level = HttpLoggingInterceptor.Level.BODY | ||
}) | ||
.build() | ||
} | ||
|
||
val retrofit: Retrofit by lazy { | ||
Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.client(okHttpClient) | ||
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) | ||
.build() | ||
} | ||
|
||
inline fun <reified T> create(): T = retrofit.create(T::class.java) | ||
} | ||
|
||
object ServicePool { | ||
val userService: UserService by lazy { ApiFactory.create<UserService>() } | ||
} |
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,13 @@ | ||
package org.sopt.and.data.service | ||
|
||
import android.content.Context | ||
|
||
object AppContext { | ||
private lateinit var applicationContext: Context | ||
|
||
fun init(context: Context) { | ||
applicationContext = context.applicationContext | ||
} | ||
|
||
fun get(): Context = applicationContext | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/org/sopt/and/data/service/AuthInterceptor.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,32 @@ | ||
package org.sopt.and.data.service | ||
|
||
import android.content.Context | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
|
||
class AuthInterceptor(context: Context) : Interceptor { | ||
private val tokenManager = TokenManager(context) | ||
|
||
override fun intercept(chain: Interceptor.Chain): Response { | ||
var token: String? | ||
|
||
runBlocking { | ||
token = tokenManager.getToken().first() | ||
} | ||
|
||
val request = chain.request().newBuilder() | ||
.apply { | ||
if (token != null) { | ||
addHeader(HEADER_NAME, token!!) | ||
} | ||
} | ||
.build() | ||
return chain.proceed(request) | ||
} | ||
|
||
companion object { | ||
const val HEADER_NAME = "token" | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/org/sopt/and/data/service/TokenManager.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,32 @@ | ||
package org.sopt.and.data.service | ||
|
||
import android.content.Context | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import org.sopt.and.data.service.TokenManager.Companion.DATASTORE_NAME | ||
|
||
val Context.dataStore by preferencesDataStore(DATASTORE_NAME) | ||
|
||
class TokenManager(private val context: Context) { | ||
private val TOKEN_KEY = stringPreferencesKey(TOKEN_NAME) | ||
|
||
fun getToken(): Flow<String?> { | ||
return context.dataStore.data.map { preferences -> | ||
preferences[TOKEN_KEY] | ||
} | ||
} | ||
|
||
suspend fun saveToken(token: String) { | ||
context.dataStore.edit { preferences -> | ||
preferences[TOKEN_KEY] = token | ||
} | ||
} | ||
|
||
companion object { | ||
const val DATASTORE_NAME = "token" | ||
const val TOKEN_NAME = "auth_token" | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/org/sopt/and/data/service/UserService.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,21 @@ | ||
package org.sopt.and.data.service | ||
|
||
import org.sopt.and.data.model.request.SignInRequestDto | ||
import org.sopt.and.data.model.request.SignUpRequestDto | ||
import org.sopt.and.data.model.response.MyHobbyResponseDto | ||
import org.sopt.and.data.model.response.SignInResponseDto | ||
import org.sopt.and.data.model.response.SignUpResponseDto | ||
import retrofit2.Response | ||
import retrofit2.http.Body | ||
import retrofit2.http.GET | ||
import retrofit2.http.POST | ||
|
||
interface UserService { | ||
suspend fun signUp(@Body request: SignUpRequestDto): Response<SignUpResponseDto> | ||
|
||
@POST("/login") | ||
suspend fun signIn(@Body request: SignInRequestDto): Response<SignInResponseDto> | ||
|
||
@GET("/user/my-hobby") | ||
suspend fun getMyHobby(): GetMyHobbyResponseDto | ||
} |
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,5 @@ | ||
package org.sopt.and.domain.model | ||
|
||
data class MyHobbyEntity( | ||
val myHobby: String | ||
) |
Oops, something went wrong.