Skip to content

Commit

Permalink
Issues #287 feat: 네트워크와 같이 쓰기위해 DbResult -> Result 로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
audxo112 committed Feb 8, 2023
1 parent 9e1f843 commit 287c09b
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import androidx.room.Query
import androidx.room.Transaction
import com.lighthouse.data.database.entity.DBGifticonCropEntity
import com.lighthouse.data.database.entity.DBGifticonEntity
import com.lighthouse.data.database.exception.NotFoundException
import com.lighthouse.data.database.exception.DBNotFoundException
import com.lighthouse.data.database.mapper.gifticon.edit.toGifticonCropEntity
import com.lighthouse.data.database.mapper.gifticon.edit.toGifticonEntity
import com.lighthouse.data.database.model.DBGifticonWithCrop
Expand Down Expand Up @@ -114,7 +114,7 @@ internal interface GifticonEditDao {
memo
)
if (updatedCount == 0) {
throw NotFoundException("업데이트할 기프티콘을 찾을 수 없습니다.")
throw DBNotFoundException("업데이트할 기프티콘을 찾을 수 없습니다.")
}
updateGifticonCrop(userId, id, croppedRect)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import com.lighthouse.data.database.entity.DBUsageHistoryEntity
import com.lighthouse.data.database.exception.NotFoundException
import com.lighthouse.data.database.exception.UpdateException
import com.lighthouse.data.database.exception.DBNotFoundException
import com.lighthouse.data.database.exception.DBUpdateException
import kotlinx.coroutines.flow.Flow

@Dao
Expand Down Expand Up @@ -114,7 +114,7 @@ internal interface GifticonUsageHistoryDao {
val gifticonId = usageHistory.gifticonId

if (updateGifticonUsed(userId, gifticonId, true) == 0) {
throw NotFoundException("업데이트할 기프티콘을 찾을 수 없습니다.")
throw DBNotFoundException("업데이트할 기프티콘을 찾을 수 없습니다.")
}

insertUsageHistory(usageHistory)
Expand All @@ -136,18 +136,18 @@ internal interface GifticonUsageHistoryDao {
val balance = getCurrentGifticonBalance(userId, gifticonId)

if (balance < amount) {
throw UpdateException("사용할 금액이 잔액보다 많습니다.")
throw DBUpdateException("사용할 금액이 잔액보다 많습니다.")
}

if (updateGifticonBalance(userId, gifticonId, balance - amount) == 0) {
throw NotFoundException("업데이트할 기프티콘을 찾을 수 없습니다.")
throw DBNotFoundException("업데이트할 기프티콘을 찾을 수 없습니다.")
}

insertUsageHistory(usageHistory)

if (balance == amount) {
if (updateGifticonUsed(userId, gifticonId, true) == 0) {
throw NotFoundException("업데이트할 기프티콘을 찾을 수 없습니다.")
throw DBNotFoundException("업데이트할 기프티콘을 찾을 수 없습니다.")
}
}
}
Expand All @@ -166,11 +166,11 @@ internal interface GifticonUsageHistoryDao {
if (isCashCard) {
val totalBalance = getTotalGifticonBalance(userId, gifticonId)
if (updateGifticonBalance(userId, gifticonId, totalBalance) == 0) {
throw NotFoundException("기프티콘의 정보를 찾을 수 없습니다.")
throw DBNotFoundException("기프티콘의 정보를 찾을 수 없습니다.")
}
}
if (updateGifticonUsed(userId, gifticonId, false) == 0) {
throw NotFoundException("기프티콘의 정보를 찾을 수 없습니다.")
throw DBNotFoundException("기프티콘의 정보를 찾을 수 없습니다.")
}
deleteUsageHistory(userId, gifticonId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,26 @@ import com.lighthouse.beep.model.exception.db.InsertException
import com.lighthouse.beep.model.exception.db.NotFoundException
import com.lighthouse.beep.model.exception.db.SelectException
import com.lighthouse.beep.model.exception.db.UpdateException
import com.lighthouse.beep.model.result.DbResult
import com.lighthouse.data.database.exception.DBDeleteException
import com.lighthouse.data.database.exception.DBInsertException
import com.lighthouse.data.database.exception.DBNotFoundException
import com.lighthouse.data.database.exception.DBSelectException
import com.lighthouse.data.database.exception.DBUpdateException

internal inline fun <T, R> T.runCatchingDB(block: T.() -> R): DbResult<R> {
internal inline fun <T, R> T.runCatchingDB(block: T.() -> R): Result<R> {
return try {
DbResult.Success(block())
Result.success(block())
} catch (e: DBNotFoundException) {
DbResult.Failure(NotFoundException(e.message))
Result.failure(NotFoundException(e.message))
} catch (e: DBSelectException) {
DbResult.Failure(SelectException(e.message))
Result.failure(SelectException(e.message))
} catch (e: DBInsertException) {
DbResult.Failure(InsertException(e.message))
Result.failure(InsertException(e.message))
} catch (e: DBDeleteException) {
DbResult.Failure(DeleteException(e.message))
Result.failure(DeleteException(e.message))
} catch (e: DBUpdateException) {
DbResult.Failure(UpdateException(e.message))
Result.failure(UpdateException(e.message))
} catch (e: Throwable) {
DbResult.Failure(e)
Result.failure(e)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.lighthouse.data.database.repository.brand

import com.lighthouse.beep.model.brand.BrandPlaceInfo
import com.lighthouse.beep.model.location.Dms
import com.lighthouse.beep.model.result.DbResult
import com.lighthouse.data.database.dao.BrandLocationDao
import com.lighthouse.data.database.exception.DBNotFoundException
import com.lighthouse.data.database.ext.runCatchingDB
import com.lighthouse.data.database.mapper.brand.combineSectionId
import com.lighthouse.data.database.mapper.brand.toDomain
Expand All @@ -21,14 +21,10 @@ internal class BrandDatabaseRepositoryImpl @Inject constructor(
x: Dms,
y: Dms,
brandName: String
): DbResult<List<BrandPlaceInfo>> {
): Result<List<BrandPlaceInfo>> {
val sections = brandLocationDao.getBrands(combineSectionId(x, y, brandName))
return if (sections != null) {
runCatchingDB {
sections.brands.toDomain()
}
} else {
DbResult.Failure(Exception())
return runCatchingDB {
sections?.brands?.toDomain() ?: throw DBNotFoundException("Section을 찾을 수 없습니다.")
}
}

Expand All @@ -37,13 +33,13 @@ internal class BrandDatabaseRepositoryImpl @Inject constructor(
x: Dms,
y: Dms,
brandName: String
): DbResult<Unit> {
): Result<Unit> {
return runCatchingDB {
brandLocationDao.insertBrand(brandPlaceInfoList.toEntity(), x, y, brandName)
}
}

override suspend fun removeExpirationBrands(): DbResult<Unit> {
override suspend fun removeExpirationBrands(): Result<Unit> {
val date: Date = Calendar.getInstance().apply {
add(Calendar.MONTH, -1)
}.time
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.lighthouse.data.database.repository.gifticon

import com.lighthouse.beep.model.gifticon.GifticonWithCrop
import com.lighthouse.beep.model.result.DbResult
import com.lighthouse.data.database.dao.GifticonEditDao
import com.lighthouse.data.database.ext.runCatchingDB
import com.lighthouse.data.database.mapper.gifticon.edit.toEntity
Expand All @@ -14,15 +13,15 @@ internal class GifticonEditDatabaseRepositoryImpl @Inject constructor(

override suspend fun insertGifticons(
gifticonWithCropList: List<GifticonWithCrop>
): DbResult<Unit> {
): Result<Unit> {
return runCatchingDB {
gifticonEditDao.insertGifticonWithCrop(gifticonWithCropList.toEntity())
}
}

override suspend fun updateGifticon(
gifticonWithCrop: GifticonWithCrop
): DbResult<Unit> {
): Result<Unit> {
return runCatchingDB {
gifticonEditDao.updateGifticonWithCrop(gifticonWithCrop.toEntity())
}
Expand All @@ -31,7 +30,7 @@ internal class GifticonEditDatabaseRepositoryImpl @Inject constructor(
override suspend fun deleteGifticon(
userId: String,
gifticonId: String
): DbResult<Unit> {
): Result<Unit> {
return runCatchingDB {
gifticonEditDao.deleteGifticon(userId, gifticonId)
}
Expand All @@ -40,7 +39,7 @@ internal class GifticonEditDatabaseRepositoryImpl @Inject constructor(
override suspend fun moveUserIdGifticon(
oldUserId: String,
newUserId: String
): DbResult<Unit> {
): Result<Unit> {
return runCatchingDB {
gifticonEditDao.moveUserIdGifticon(oldUserId, newUserId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import com.lighthouse.beep.model.brand.BrandWithGifticonCount
import com.lighthouse.beep.model.etc.SortBy
import com.lighthouse.beep.model.gifticon.Gifticon
import com.lighthouse.beep.model.gifticon.GifticonWithCrop
import com.lighthouse.beep.model.result.DbResult
import com.lighthouse.data.database.dao.GifticonSearchDao
import com.lighthouse.data.database.exception.NotFoundException
import com.lighthouse.data.database.exception.DBNotFoundException
import com.lighthouse.data.database.ext.runCatchingDB
import com.lighthouse.data.database.mapper.gifticon.edit.toDomain
import com.lighthouse.data.database.mapper.gifticon.search.toDomain
Expand All @@ -23,7 +22,7 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor(
override fun getGifticon(
userId: String,
gifticonId: String
): DbResult<Flow<Gifticon>> {
): Result<Flow<Gifticon>> {
return runCatchingDB {
gifticonSearchDao.getGifticon(userId, gifticonId).map {
it.toDomain()
Expand All @@ -36,7 +35,7 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor(
isUsed: Boolean,
filterExpired: Boolean,
sortBy: SortBy
): DbResult<Flow<List<Gifticon>>> {
): Result<Flow<List<Gifticon>>> {
val gifticons = when (filterExpired) {
true -> when (sortBy) {
SortBy.RECENT -> gifticonSearchDao.getAllGifticonsSortByRecent(
Expand Down Expand Up @@ -77,7 +76,7 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor(
filterBrand: Set<String>,
filterExpired: Boolean,
sortBy: SortBy
): DbResult<Flow<List<Gifticon>>> {
): Result<Flow<List<Gifticon>>> {
val upperFilterBrand = filterBrand.map {
it.uppercase()
}.toSet()
Expand Down Expand Up @@ -125,7 +124,7 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor(
userId: String,
isUsed: Boolean,
filterExpired: Boolean
): DbResult<Flow<List<BrandWithGifticonCount>>> {
): Result<Flow<List<BrandWithGifticonCount>>> {
val brandWithGifticonCount = when (filterExpired) {
true -> gifticonSearchDao.getAllBrands(userId, isUsed, Date())
false -> gifticonSearchDao.getAllBrands(userId, isUsed)
Expand All @@ -143,7 +142,7 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor(
isUsed: Boolean,
brand: String,
filterExpired: Boolean
): DbResult<Flow<List<Gifticon>>> {
): Result<Flow<List<Gifticon>>> {
val gifticons = when (filterExpired) {
true -> gifticonSearchDao.getGifticonByBrand(userId, isUsed, brand)
false -> gifticonSearchDao.getGifticonByBrand(userId, isUsed, brand, Date())
Expand All @@ -158,18 +157,18 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor(
override suspend fun getGifticonCrop(
userId: String,
gifticonId: String
): DbResult<GifticonWithCrop> {
): Result<GifticonWithCrop> {
return runCatchingDB {
gifticonSearchDao.getGifticonWithCrop(userId, gifticonId)?.toDomain()
?: throw NotFoundException("해당 기프티콘을 찾을 수 없습니다.")
?: throw DBNotFoundException("해당 기프티콘을 찾을 수 없습니다.")
}
}

override fun hasGifticon(
userId: String,
isUsed: Boolean,
filterExpired: Boolean
): DbResult<Flow<Boolean>> {
): Result<Flow<Boolean>> {
return runCatchingDB {
when (filterExpired) {
true -> gifticonSearchDao.hasGifticon(userId, isUsed)
Expand All @@ -178,7 +177,7 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor(
}
}

override suspend fun hasGifticonBrand(userId: String, brand: String): DbResult<Boolean> {
override suspend fun hasGifticonBrand(userId: String, brand: String): Result<Boolean> {
return runCatchingDB {
gifticonSearchDao.hasGifticonBrand(userId, brand)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.lighthouse.data.database.repository.gifticon

import com.lighthouse.beep.model.result.DbResult
import com.lighthouse.beep.model.user.UsageHistory
import com.lighthouse.data.database.dao.GifticonUsageHistoryDao
import com.lighthouse.data.database.ext.runCatchingDB
Expand All @@ -19,7 +18,7 @@ internal class GifticonUsageHistoryDatabaseRepositoryImpl @Inject constructor(
userId: String,
gifticonId: String,
usageHistory: UsageHistory
): DbResult<Unit> {
): Result<Unit> {
return runCatchingDB {
gifticonUsageHistoryDao.useGifticonAndInsertHistory(
userId,
Expand All @@ -33,7 +32,7 @@ internal class GifticonUsageHistoryDatabaseRepositoryImpl @Inject constructor(
gifticonId: String,
amount: Int,
usageHistory: UsageHistory
): DbResult<Unit> {
): Result<Unit> {
return runCatchingDB {
gifticonUsageHistoryDao.useCashCardGifticonAndInsertHistory(
userId,
Expand All @@ -46,7 +45,7 @@ internal class GifticonUsageHistoryDatabaseRepositoryImpl @Inject constructor(
override suspend fun revertUsedGifticon(
userId: String,
gifticonId: String
): DbResult<Unit> {
): Result<Unit> {
return runCatchingDB {
gifticonUsageHistoryDao.revertUsedGifticonAndDeleteHistory(
userId,
Expand All @@ -58,7 +57,7 @@ internal class GifticonUsageHistoryDatabaseRepositoryImpl @Inject constructor(
override fun getUsageHistory(
userId: String,
gifticonId: String
): DbResult<Flow<List<UsageHistory>>> {
): Result<Flow<List<UsageHistory>>> {
return runCatchingDB {
gifticonUsageHistoryDao.getUsageHistory(
userId,
Expand All @@ -72,7 +71,7 @@ internal class GifticonUsageHistoryDatabaseRepositoryImpl @Inject constructor(
override suspend fun insertUsageHistory(
gifticonId: String,
usageHistory: UsageHistory
): DbResult<Unit> {
): Result<Unit> {
return runCatchingDB {
gifticonUsageHistoryDao.insertUsageHistory(usageHistory.toEntity(gifticonId))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ package com.lighthouse.repository.brand

import com.lighthouse.beep.model.brand.BrandPlaceInfo
import com.lighthouse.beep.model.location.Dms
import com.lighthouse.beep.model.result.DbResult

interface BrandDatabaseRepository {
suspend fun getBrands(
x: Dms,
y: Dms,
brandName: String
): DbResult<List<BrandPlaceInfo>>
): Result<List<BrandPlaceInfo>>

suspend fun insertBrands(
brandPlaceInfoList: List<BrandPlaceInfo>,
x: Dms,
y: Dms,
brandName: String
): DbResult<Unit>
): Result<Unit>

suspend fun removeExpirationBrands(): DbResult<Unit>
suspend fun removeExpirationBrands(): Result<Unit>
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package com.lighthouse.repository.gifticon

import com.lighthouse.beep.model.gifticon.GifticonWithCrop
import com.lighthouse.beep.model.result.DbResult

interface GifticonEditDatabaseRepository {
suspend fun insertGifticons(
gifticonWithCropList: List<GifticonWithCrop>
): DbResult<Unit>
): Result<Unit>

suspend fun updateGifticon(
gifticonWithCrop: GifticonWithCrop
): DbResult<Unit>
): Result<Unit>

suspend fun deleteGifticon(
userId: String,
gifticonId: String
): DbResult<Unit>
): Result<Unit>

suspend fun moveUserIdGifticon(
oldUserId: String,
newUserId: String
): DbResult<Unit>
): Result<Unit>
}
Loading

0 comments on commit 287c09b

Please sign in to comment.