Skip to content

Commit

Permalink
[feature] #9 - repository, usecase 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
sayyyho committed Dec 8, 2024
1 parent a346adb commit 7a7e46b
Show file tree
Hide file tree
Showing 25 changed files with 313 additions and 167 deletions.
60 changes: 0 additions & 60 deletions app/src/main/java/org/sopt/and/data/api/Api.kt

This file was deleted.

25 changes: 0 additions & 25 deletions app/src/main/java/org/sopt/and/data/api/Auth.kt

This file was deleted.

13 changes: 0 additions & 13 deletions app/src/main/java/org/sopt/and/data/api/Hobby.kt

This file was deleted.

48 changes: 0 additions & 48 deletions app/src/main/java/org/sopt/and/data/dto/Auth.kt

This file was deleted.

18 changes: 0 additions & 18 deletions app/src/main/java/org/sopt/and/data/dto/My.kt

This file was deleted.

47 changes: 47 additions & 0 deletions app/src/main/java/org/sopt/and/data/mapper/Mapper.kt
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
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.sopt.and.data.repositoryimpl
import org.sopt.and.data.datasource.GetMyHobbyDataSource
import org.sopt.and.data.mapper.Mapper
import org.sopt.and.domain.model.MyHobbyEntity
import org.sopt.and.domain.repository.GetMyHobbyRepository
import org.sopt.and.domain.usecase.GetMyHobbyRepository

class MyHobbyRepositoryImpl(
private val getMyHobbyDataSource: GetMyHobbyDataSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.sopt.and.data.datasource.SignInDataSource
import org.sopt.and.data.mapper.Mapper
import org.sopt.and.domain.model.SignInInformationEntity
import org.sopt.and.domain.model.SignInResponseEntity
import org.sopt.and.domain.repository.SignInRepository
import org.sopt.and.domain.usecase.SignInRepository

class SignInRepositoryImpl(
private val signInDataSource: SignInDataSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.sopt.and.data.datasource.SignUpDataSource
import org.sopt.and.data.mapper.Mapper
import org.sopt.and.domain.model.SignUpInformationEntity
import org.sopt.and.domain.model.SignUpResponseEntity
import org.sopt.and.domain.repository.SignUpRepository
import org.sopt.and.domain.usecase.SignUpRepository

class SignUpRepositoryImpl(
private val signUpDataSource: SignUpDataSource
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/java/org/sopt/and/data/service/ApiFactory.kt
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>() }
}
13 changes: 13 additions & 0 deletions app/src/main/java/org/sopt/and/data/service/AppContext.kt
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 app/src/main/java/org/sopt/and/data/service/AuthInterceptor.kt
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 app/src/main/java/org/sopt/and/data/service/TokenManager.kt
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 app/src/main/java/org/sopt/and/data/service/UserService.kt
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
}
5 changes: 5 additions & 0 deletions app/src/main/java/org/sopt/and/domain/model/MyHobbyEntity.kt
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
)
Loading

0 comments on commit 7a7e46b

Please sign in to comment.