Skip to content

Commit

Permalink
Merge pull request #22 from Team-B1ND/feature/search
Browse files Browse the repository at this point in the history
검색 페이지 구현하기
  • Loading branch information
wsi1212 authored Apr 30, 2024
2 parents 0f76c6c + 1b29fab commit d595681
Show file tree
Hide file tree
Showing 128 changed files with 2,920 additions and 1,272 deletions.
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kr.hs.dgsw.mentomenv2.data.datasource

import kotlinx.coroutines.flow.Flow
import kr.hs.dgsw.mentomenv2.data.response.GetCodeResponse

interface DAuthDataSource {
fun getCode(
id: String,
pw: String,
clientId: String,
redirectURL: String,
): Flow<GetCodeResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ interface DataStoreDataSource {
value: String,
): Flow<Unit>

fun saveToken(token: Token): Flow<Unit>

fun getData(
key: String,
defaultValue: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kr.hs.dgsw.mentomenv2.data.datasource

import kotlinx.coroutines.flow.Flow
import kr.hs.dgsw.mentomenv2.domain.model.Post
import kr.hs.dgsw.mentomenv2.domain.params.PostEditParam
import kr.hs.dgsw.mentomenv2.domain.params.PostSubmitParam

interface PostDataSource {
Expand All @@ -10,4 +11,10 @@ interface PostDataSource {
fun getPostByTag(tag: String): Flow<List<Post>>

fun submitPost(postSubmitParam: PostSubmitParam): Flow<Unit>

fun getPostById(id: Int): Flow<Post>

fun deletePostById(id: Int): Flow<Unit>

fun editPost(postEditParam: PostEditParam): Flow<Unit>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package kr.hs.dgsw.mentomenv2.data.datasourceimpl.cache

import android.util.Log
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
Expand All @@ -10,8 +9,8 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kr.hs.dgsw.mentomenv2.data.datasource.DataStoreDataSource
import kr.hs.dgsw.mentomenv2.domain.exception.MenToMenException
import kr.hs.dgsw.mentomenv2.domain.model.Token
import kr.hs.dgsw.mentomenv2.domain.util.Log
import javax.inject.Inject

class DataStoreDataSourceImpl
Expand All @@ -30,6 +29,14 @@ class DataStoreDataSourceImpl
}
}

override fun saveToken(token: Token): Flow<Unit> =
flow {
dataStore.edit { preferences ->
preferences[stringPreferencesKey("access_token")] = token.accessToken
preferences[stringPreferencesKey("refresh_token")] = token.refreshToken
}
}

override fun getToken(): Flow<Token> =
flow {
val accessToken =
Expand All @@ -44,11 +51,7 @@ class DataStoreDataSourceImpl
"DataStoreDataSourceImpl",
"Token: ${accessToken.first()}, ${refreshToken.first()} 토큰 호출 성공",
)
if (accessToken.first() == "" || refreshToken.first() == "") {
throw MenToMenException("토큰이 없습니다.")
} else {
emit(Token(accessToken.first(), refreshToken.first()))
}
emit(Token(accessToken.first(), refreshToken.first()))
}

override fun getData(
Expand All @@ -64,6 +67,7 @@ class DataStoreDataSourceImpl

override fun removeData(key: String): Flow<Unit> =
flow {
Log.d("removeData: ", "key: $key")
val preferencesKey = stringPreferencesKey(key)
dataStore.edit { preferences ->
preferences.remove(preferencesKey)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package kr.hs.dgsw.mentomenv2.data.datasourceimpl.remote

import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kr.hs.dgsw.mentomenv2.data.datasource.AuthDataSource
import kr.hs.dgsw.mentomenv2.data.datasourceimpl.remote.base.RetrofitDataSourceImpl
import kr.hs.dgsw.mentomenv2.data.request.DAuthClientRequest
import kr.hs.dgsw.mentomenv2.data.request.SingInRequest
import kr.hs.dgsw.mentomenv2.data.response.TokenResponse
import kr.hs.dgsw.mentomenv2.data.service.AuthService
import kr.hs.dgsw.mentomenv2.domain.exception.MenToMenException
Expand All @@ -24,9 +23,8 @@ class AuthDataSourceImpl
get() = createApi(AuthService::class.java)

override fun signIn(code: String): Flow<Token> {
Log.d("AuthDataSourceImpl", "signIn out return flow: $code")
return flow {
emit(api.signIn(DAuthClientRequest(code)).data)
emit(api.signIn(SingInRequest(code)).data)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package kr.hs.dgsw.mentomenv2.data.datasourceimpl.remote

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kr.hs.dgsw.mentomenv2.data.datasource.DAuthDataSource
import kr.hs.dgsw.mentomenv2.data.request.GetCodeRequest
import kr.hs.dgsw.mentomenv2.data.response.GetCodeResponse
import kr.hs.dgsw.mentomenv2.data.service.DAuthService
import javax.inject.Inject

class DAuthDataSourceImpl
@Inject
constructor(
private val api: DAuthService,
) : DAuthDataSource {
override fun getCode(
id: String,
pw: String,
clientId: String,
redirectURL: String,
): Flow<GetCodeResponse> {
return flow {
api.getCode(GetCodeRequest(id, pw, clientId, redirectURL))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kr.hs.dgsw.mentomenv2.data.datasource.MyDataSource
import kr.hs.dgsw.mentomenv2.data.service.MyService
import kr.hs.dgsw.mentomenv2.domain.exception.MenToMenException
import kr.hs.dgsw.mentomenv2.domain.model.Post
import kr.hs.dgsw.mentomenv2.domain.model.StdInfo
import kr.hs.dgsw.mentomenv2.domain.model.User
import retrofit2.HttpException
import javax.inject.Inject

class MyDataSourceImpl
Expand All @@ -36,7 +36,7 @@ class MyDataSourceImpl
emit(response.body()?.data ?: User())
}
} else {
throw HttpException(response)
throw MenToMenException(response.message())
}
}.flowOn(Dispatchers.IO)

Expand All @@ -47,7 +47,7 @@ class MyDataSourceImpl
if (response.isSuccessful) {
emit(response.body()?.data ?: emptyList())
} else {
throw HttpException(response)
throw MenToMenException(response.message())
}
}.flowOn(Dispatchers.IO)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package kr.hs.dgsw.mentomenv2.data.datasourceimpl.remote

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kr.hs.dgsw.mentomenv2.data.datasource.PostDataSource
import kr.hs.dgsw.mentomenv2.data.service.PostService
import kr.hs.dgsw.mentomenv2.domain.model.Post
import kr.hs.dgsw.mentomenv2.domain.params.PostEditParam
import kr.hs.dgsw.mentomenv2.domain.params.PostSubmitParam
import javax.inject.Inject

Expand All @@ -30,4 +33,21 @@ class PostDataSourceImpl
emit(api.submitPost(postSubmitParam).data)
}
}

override fun getPostById(id: Int): Flow<Post> {
return flow {
emit(api.getPostById(id).data)
}
}

override fun deletePostById(id: Int): Flow<Unit> {
return flow {
emit(api.deletePostById(id).data)
}
}

override fun editPost(postEditParam: PostEditParam): Flow<Unit> =
flow {
emit(api.editPost(postEditParam).data)
}.flowOn(Dispatchers.IO)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract class RetrofitDataSourceImpl<SV>(
private val retrofit: Retrofit =
Retrofit.Builder()
.client(client)
.baseUrl("https://mentomen.team-alt.com/")
.baseUrl("http://43.201.193.60/")
.addConverterFactory(
GsonConverterFactory.create(
GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(),
Expand All @@ -34,7 +34,6 @@ abstract class RetrofitDataSourceImpl<SV>(
val interceptor = HttpLoggingInterceptor()
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)

// 토큰을 가져오는 메서드가 있다고 가정하고, "YOUR_TOKEN_METHOD"를 실제 메서드로 대체하세요.
val token =
getTokenUseCase.invoke().let {
it.map {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package kr.hs.dgsw.mentomenv2.data.interceptor

import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kr.hs.dgsw.mentomenv2.data.repository.DataStoreRepositoryImpl
import kr.hs.dgsw.mentomenv2.domain.model.Token
import kr.hs.dgsw.mentomenv2.domain.usecase.auth.GetAccessTokenUseCase
import kr.hs.dgsw.mentomenv2.domain.util.Log
import kr.hs.dgsw.mentomenv2.domain.util.Result
import okhttp3.Interceptor
import okhttp3.Protocol
Expand All @@ -19,7 +18,6 @@ class Intercept
@Inject
constructor(
private val dataStoreRepositoryImpl: DataStoreRepositoryImpl,
private val getAccessTokenUseCase: GetAccessTokenUseCase,
) : Interceptor {
private val tokenHeader = "Authorization"

Expand All @@ -28,7 +26,7 @@ class Intercept

@Synchronized
override fun intercept(chain: Interceptor.Chain): Response {
setToken()
getToken()
response = chain.proceedWithToken(chain.request())

if (response.code == 401 || response.code == 403 || response.code == 400 || response.code == 500) {
Expand Down Expand Up @@ -76,10 +74,14 @@ class Intercept
}
}

private fun setToken() =
private fun getToken() =
runBlocking(Dispatchers.IO) {
dataStoreRepositoryImpl.getToken().let {
it.collect {
Log.d(
"getToken: ",
"token: ${it.data?.accessToken ?: ""}, ${it.data?.refreshToken ?: ""}",
)
token =
when (it) {
is Result.Success ->
Expand All @@ -97,25 +99,42 @@ class Intercept

private fun clearDataStore() =
runBlocking(Dispatchers.IO) {
dataStoreRepositoryImpl.clearData().let { }
dataStoreRepositoryImpl.clearData().let {
it.collect { result ->
when (result) {
is Result.Success -> {
Log.d("clearDataStore: ", "clearDataStore: 성공")
}

is Result.Error -> {
Log.d("clearDataStore: ", "clearDataStore: 실패")
}

is Result.Loading -> {
Log.d("clearDataStore: ", "clearDataStore: 로딩중")
}
}
}
}
}

private fun fetchToken() =
runBlocking(Dispatchers.IO) {
getAccessTokenUseCase.invoke().let {
it.collect {
dataStoreRepositoryImpl.getToken().let { token ->
token.collect {
when (it) {
is Result.Success -> {
dataStoreRepositoryImpl.saveData("refresh_token", it.data ?: "")
token = Token(it.data ?: "", token.refreshToken)
dataStoreRepositoryImpl.saveData("refresh_token", it.data?.refreshToken ?: "")
this@Intercept.token =
Token(it.data?.refreshToken ?: "", this@Intercept.token.refreshToken)
}

is Result.Error -> {
token = Token("", "")
this@Intercept.token = Token("", "")
}

is Result.Loading -> {
token = Token("", "")
this@Intercept.token = Token("", "")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kr.hs.dgsw.mentomenv2.data.mapper

import kr.hs.dgsw.mentomenv2.data.response.GetCodeResponse
import kr.hs.dgsw.mentomenv2.domain.model.Code

fun GetCodeResponse.toModel() =
Code(
code = this.location,
)
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package kr.hs.dgsw.mentomenv2.data.repository

import android.util.Log
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kr.hs.dgsw.mentomenv2.data.datasource.AuthDataSource
import kr.hs.dgsw.mentomenv2.data.repository.base.BaseRepositoryImpl
import kr.hs.dgsw.mentomenv2.domain.model.Code
import kr.hs.dgsw.mentomenv2.domain.model.Token
import kr.hs.dgsw.mentomenv2.domain.repository.AuthRepository
import kr.hs.dgsw.mentomenv2.domain.util.Log
import kr.hs.dgsw.mentomenv2.domain.util.Result
import javax.inject.Inject

Expand All @@ -15,9 +16,9 @@ class AuthRepositoryImpl
constructor(
private val authDataSource: AuthDataSource,
) : BaseRepositoryImpl(), AuthRepository {
override fun signIn(code: String): Flow<Result<Token>> {
override fun signIn(code: Code): Flow<Result<Token>> {
Log.d("AuthRepositoryImpl", "signIn: $code")
return execute { authDataSource.signIn(code) }
return execute { authDataSource.signIn(code.code) }
}

override fun getAccessToken(): Flow<Result<String>> {
Expand Down
Loading

0 comments on commit d595681

Please sign in to comment.