Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.withpeace.withpeace.core.data.di

import com.withpeace.withpeace.core.data.repository.DefaultAppUpdateRepository
import com.withpeace.withpeace.core.data.repository.DefaultBalanceGameRepository
import com.withpeace.withpeace.core.data.repository.DefaultCommentRepository
import com.withpeace.withpeace.core.data.repository.DefaultImageRepository
import com.withpeace.withpeace.core.data.repository.DefaultPostRepository
import com.withpeace.withpeace.core.data.repository.DefaultRecentSearchKeywordRepository
Expand All @@ -10,6 +11,7 @@ import com.withpeace.withpeace.core.data.repository.DefaultUserRepository
import com.withpeace.withpeace.core.data.repository.DefaultYouthPolicyRepository
import com.withpeace.withpeace.core.domain.repository.AppUpdateRepository
import com.withpeace.withpeace.core.domain.repository.BalanceGameRepository
import com.withpeace.withpeace.core.domain.repository.CommentRepository
import com.withpeace.withpeace.core.domain.repository.ImageRepository
import com.withpeace.withpeace.core.domain.repository.PostRepository
import com.withpeace.withpeace.core.domain.repository.RecentSearchKeywordRepository
Expand Down Expand Up @@ -69,4 +71,9 @@ interface RepositoryModule {
defaultBalanceGameRepository: DefaultBalanceGameRepository
): BalanceGameRepository

@Binds
@ViewModelScoped
fun bindsCommentRepository(
defaultCommentRepository: DefaultCommentRepository
): CommentRepository
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.withpeace.withpeace.core.data.mapper

import com.withpeace.withpeace.core.domain.model.balancegame.BalanceGame
import com.withpeace.withpeace.core.domain.model.balancegame.BalanceGameComment
import com.withpeace.withpeace.core.domain.model.date.Date
import com.withpeace.withpeace.core.network.di.response.BalanceGameCommentEntity
import com.withpeace.withpeace.core.network.di.response.GameEntity
import java.time.LocalDateTime

fun GameEntity.toDomain(): BalanceGame {
return BalanceGame(
id = this.gameId,
date = date,
title = title,
optionA = optionA,
optionB = optionB,
userChoice = userChoice,
isActive = isActive,
optionACount = optionACount,
optionBCount = optionBCount,
hasPrevious = hasPrevious,
hasNext = hasNext,
comments = comments.map { it.toDomain() },
)
}

fun BalanceGameCommentEntity.toDomain(): BalanceGameComment {
return BalanceGameComment(
id = this.commentId,
userId = this.userId,
nickname = this.nickname,
profileImageUrl = this.profileImageUrl,
content = this.content,
userChoice = this.userChoice,
createDate = Date(this.createDate.toLocalDateTime()),
)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package com.withpeace.withpeace.core.data.repository

import com.skydoves.sandwich.suspendMapSuccess
import com.skydoves.sandwich.suspendOnSuccess
import com.withpeace.withpeace.core.data.mapper.toDomain
import com.withpeace.withpeace.core.data.util.handleApiFailure
import com.withpeace.withpeace.core.datastore.dataStore.balancegame.BalanceGameDataStore
import com.withpeace.withpeace.core.domain.model.balancegame.BalanceGame
import com.withpeace.withpeace.core.domain.model.error.CheonghaError
import com.withpeace.withpeace.core.domain.model.error.ClientError
import com.withpeace.withpeace.core.domain.model.error.ResponseError
import com.withpeace.withpeace.core.domain.repository.BalanceGameRepository
import com.withpeace.withpeace.core.domain.repository.UserRepository
import com.withpeace.withpeace.core.network.di.request.SelectBalanceGameRequest
import com.withpeace.withpeace.core.network.di.service.BalanceGameService
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class DefaultBalanceGameRepository @Inject constructor(
private val balanceGameDataStore: BalanceGameDataStore,
private val balanceGameService: BalanceGameService,
private val userRepository: UserRepository,
): BalanceGameRepository {
override fun isVisited(): Flow<Boolean> {
return balanceGameDataStore.isVisited
Expand All @@ -15,4 +29,44 @@ class DefaultBalanceGameRepository @Inject constructor(
override suspend fun updateVisitedStatus(visited: Boolean) {
balanceGameDataStore.updateVisitedStatus(visited)
}

override fun fetchBalanceGame(
pageIndex: Int,
pageSize: Int,
onError: (CheonghaError) -> Unit,
): Flow<List<BalanceGame>> = flow {
balanceGameService.fetchBalanceGame(pageIndex = pageIndex, pageSize = pageSize).suspendMapSuccess {
emit(this.data.map { it.toDomain() })
}.handleApiFailure {
onErrorWithAuthExpired(it, onError)
if(it.serverErrorCode == 40010) {
onError(ClientError.BalanceGameExpired)
}
}
}

override fun selectBalanceGame(
gameId: Long,
selection: String,
onError: (CheonghaError) -> Unit,
): Flow<Unit> = flow {
balanceGameService.postBalanceGame(
gameId,
SelectBalanceGameRequest(selection)).suspendOnSuccess {
emit(Unit)
}
}

private suspend fun onErrorWithAuthExpired(
it: ResponseError,
onError: suspend (CheonghaError) -> Unit,
) {
if (it == ResponseError.INVALID_TOKEN_ERROR) {
userRepository.logout(onError).collect {
onError(ClientError.AuthExpired)
}
} else {
onError(it)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.withpeace.withpeace.core.data.repository

import com.skydoves.sandwich.suspendOnSuccess
import com.withpeace.withpeace.core.data.util.handleApiFailure
import com.withpeace.withpeace.core.domain.model.error.CheonghaError
import com.withpeace.withpeace.core.domain.model.error.ClientError
import com.withpeace.withpeace.core.domain.model.error.ResponseError
import com.withpeace.withpeace.core.domain.model.post.ReportType
import com.withpeace.withpeace.core.domain.repository.CommentRepository
import com.withpeace.withpeace.core.network.di.request.CommonCommentRequest
import com.withpeace.withpeace.core.network.di.request.ReportTypeRequest
import com.withpeace.withpeace.core.network.di.service.CommentService
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class DefaultCommentRepository @Inject constructor(
private val commentService: CommentService,
private val userRepository: DefaultUserRepository,
) : CommentRepository {
override fun registerComment(
targetType: String, targetId: Long, content: String,
onError: suspend (CheonghaError) -> Unit,
): Flow<Unit> =
flow {
commentService.registerComment(CommonCommentRequest(targetType, targetId, content))
.suspendOnSuccess {
emit(Unit)
}.handleApiFailure {
onErrorWithAuthExpired(it, onError)
}
}

override fun reportComment(
commentId: Long,
reportType: ReportType,
onError: suspend (CheonghaError) -> Unit,
): Flow<Unit> {
return flow {
commentService.reportComment(commentId, ReportTypeRequest(reportType.name))
.suspendOnSuccess {
emit(Unit)
}.handleApiFailure {
onErrorWithAuthExpired(it, onError)
}
}
}

private suspend fun onErrorWithAuthExpired(
it: ResponseError,
onError: suspend (CheonghaError) -> Unit,
) {
if (it == ResponseError.INVALID_TOKEN_ERROR) {
userRepository.logout(onError).collect {
onError(ClientError.AuthExpired)
}
} else {
onError(it)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.withpeace.withpeace.core.domain.model.balancegame

import com.withpeace.withpeace.core.domain.model.date.Date

data class BalanceGame(
val id: Long,
val date: String,
val title: String,
val optionA: String,
val optionB: String,
val userChoice: String?,
val isActive: Boolean,
val optionACount: Long,
val optionBCount: Long,
val hasPrevious: Boolean,
val hasNext: Boolean,
val comments: List<BalanceGameComment>,
) {
fun getAPercentage(): Int {
if(optionACount + optionBCount == 0L) {
return 0
}
return (optionACount / (optionACount + optionBCount)).toInt()
}

fun getBPercentage(): Int {
if(optionACount + optionBCount == 0L) {
return 0
}
return 100 - getAPercentage()
}
}

data class BalanceGameComment(
val id: Long,
val userId: Long,
val nickname: String,
val profileImageUrl: String,
val content: String,
val userChoice: String?,
val createDate: Date,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.withpeace.withpeace.core.domain.model.balancegame

/*
* 미사용 클래스
* 밸런스 게임 선택시 결과 값을 동기화 하기 위한 용도
* 자신이 선택한 카운트만 동기화 하는 것으로 진행
*/
data class BalanceGameSelectResult(
val optionACount: Long,
val optionBCount: Long,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ sealed interface ClientError : CheonghaError {
data object NoSearchResult : SearchError
data object SingleCharacterSearch : SearchError
}

data object BalanceGameExpired : ClientError
}

class NoSearchResultException: IllegalStateException()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
package com.withpeace.withpeace.core.domain.repository

import com.withpeace.withpeace.core.domain.model.balancegame.BalanceGame
import com.withpeace.withpeace.core.domain.model.error.CheonghaError
import kotlinx.coroutines.flow.Flow

interface BalanceGameRepository {
fun isVisited(): Flow<Boolean>

suspend fun updateVisitedStatus(visited: Boolean)

fun fetchBalanceGame(
pageIndex: Int,
pageSize: Int,
onError: (CheonghaError) -> Unit,
): Flow<List<BalanceGame>>

fun selectBalanceGame(
gameId: Long,
selection: String,
onError: (CheonghaError) -> Unit,
): Flow<Unit>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.withpeace.withpeace.core.domain.repository

import com.withpeace.withpeace.core.domain.model.error.CheonghaError
import com.withpeace.withpeace.core.domain.model.post.ReportType
import kotlinx.coroutines.flow.Flow

interface CommentRepository {
fun registerComment(
targetType: String,
targetId: Long,
content: String,
onError: suspend (CheonghaError) -> Unit,
): Flow<Unit>

fun reportComment(
commentId: Long,
reportType: ReportType,
onError: suspend (CheonghaError) -> Unit,
): Flow<Unit>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.withpeace.withpeace.core.domain.usecase

import com.withpeace.withpeace.core.domain.model.balancegame.BalanceGame
import com.withpeace.withpeace.core.domain.model.error.CheonghaError
import com.withpeace.withpeace.core.domain.repository.BalanceGameRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class GetBalanceGameUseCase @Inject constructor(
private val balanceGameRepository: BalanceGameRepository,
) {
operator fun invoke(
pageIndex: Int,
pageSize: Int,
onError: (CheonghaError) -> Unit,
): Flow<List<BalanceGame>> {
return balanceGameRepository.fetchBalanceGame(
pageIndex = pageIndex, pageSize = pageSize, onError = onError,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package com.withpeace.withpeace.core.domain.usecase

import com.withpeace.withpeace.core.domain.model.error.CheonghaError
import com.withpeace.withpeace.core.domain.repository.PostRepository
import com.withpeace.withpeace.core.domain.repository.CommentRepository
import javax.inject.Inject

class RegisterCommentUseCase @Inject constructor(
private val postRepository: PostRepository,
private val commentRepository: CommentRepository,
) {
operator fun invoke(
postId: Long,
targetType: String,
targetId: Long,
content: String,
onError: suspend (CheonghaError) -> Unit,
) = postRepository.registerComment(
postId = postId,
) = commentRepository.registerComment(
targetType = targetType,
targetId = targetId,
content = content,
onError = onError,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.withpeace.withpeace.core.domain.usecase

import com.withpeace.withpeace.core.domain.model.error.CheonghaError
import com.withpeace.withpeace.core.domain.repository.PostRepository
import javax.inject.Inject

class RegisterPostCommentUseCase @Inject constructor(
private val postRepository: PostRepository,
) {
operator fun invoke(
postId: Long,
content: String,
onError: suspend (CheonghaError) -> Unit,
) = postRepository.registerComment(
postId = postId,
content = content,
onError = onError,
)
}
Loading