diff --git a/data/data-database/src/main/java/com/lighthouse/data/database/dao/GifticonEditDao.kt b/data/data-database/src/main/java/com/lighthouse/data/database/dao/GifticonEditDao.kt index 1705efcc9..f5d486c95 100644 --- a/data/data-database/src/main/java/com/lighthouse/data/database/dao/GifticonEditDao.kt +++ b/data/data-database/src/main/java/com/lighthouse/data/database/dao/GifticonEditDao.kt @@ -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 @@ -114,7 +114,7 @@ internal interface GifticonEditDao { memo ) if (updatedCount == 0) { - throw NotFoundException("업데이트할 기프티콘을 찾을 수 없습니다.") + throw DBNotFoundException("업데이트할 기프티콘을 찾을 수 없습니다.") } updateGifticonCrop(userId, id, croppedRect) } diff --git a/data/data-database/src/main/java/com/lighthouse/data/database/dao/GifticonUsageHistoryDao.kt b/data/data-database/src/main/java/com/lighthouse/data/database/dao/GifticonUsageHistoryDao.kt index 125affa8d..761223ad8 100644 --- a/data/data-database/src/main/java/com/lighthouse/data/database/dao/GifticonUsageHistoryDao.kt +++ b/data/data-database/src/main/java/com/lighthouse/data/database/dao/GifticonUsageHistoryDao.kt @@ -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 @@ -114,7 +114,7 @@ internal interface GifticonUsageHistoryDao { val gifticonId = usageHistory.gifticonId if (updateGifticonUsed(userId, gifticonId, true) == 0) { - throw NotFoundException("업데이트할 기프티콘을 찾을 수 없습니다.") + throw DBNotFoundException("업데이트할 기프티콘을 찾을 수 없습니다.") } insertUsageHistory(usageHistory) @@ -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("업데이트할 기프티콘을 찾을 수 없습니다.") } } } @@ -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) } diff --git a/data/data-database/src/main/java/com/lighthouse/data/database/ext/KotlinExt.kt b/data/data-database/src/main/java/com/lighthouse/data/database/ext/KotlinExt.kt index 44c869290..0700ef793 100644 --- a/data/data-database/src/main/java/com/lighthouse/data/database/ext/KotlinExt.kt +++ b/data/data-database/src/main/java/com/lighthouse/data/database/ext/KotlinExt.kt @@ -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.runCatchingDB(block: T.() -> R): DbResult { +internal inline fun T.runCatchingDB(block: T.() -> R): Result { 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) } } diff --git a/data/data-database/src/main/java/com/lighthouse/data/database/repository/brand/BrandDatabaseRepositoryImpl.kt b/data/data-database/src/main/java/com/lighthouse/data/database/repository/brand/BrandDatabaseRepositoryImpl.kt index 75bc232cd..40192cd0f 100644 --- a/data/data-database/src/main/java/com/lighthouse/data/database/repository/brand/BrandDatabaseRepositoryImpl.kt +++ b/data/data-database/src/main/java/com/lighthouse/data/database/repository/brand/BrandDatabaseRepositoryImpl.kt @@ -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 @@ -21,14 +21,10 @@ internal class BrandDatabaseRepositoryImpl @Inject constructor( x: Dms, y: Dms, brandName: String - ): DbResult> { + ): Result> { 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을 찾을 수 없습니다.") } } @@ -37,13 +33,13 @@ internal class BrandDatabaseRepositoryImpl @Inject constructor( x: Dms, y: Dms, brandName: String - ): DbResult { + ): Result { return runCatchingDB { brandLocationDao.insertBrand(brandPlaceInfoList.toEntity(), x, y, brandName) } } - override suspend fun removeExpirationBrands(): DbResult { + override suspend fun removeExpirationBrands(): Result { val date: Date = Calendar.getInstance().apply { add(Calendar.MONTH, -1) }.time diff --git a/data/data-database/src/main/java/com/lighthouse/data/database/repository/gifticon/GifticonEditDatabaseRepositoryImpl.kt b/data/data-database/src/main/java/com/lighthouse/data/database/repository/gifticon/GifticonEditDatabaseRepositoryImpl.kt index 3ebd552f9..c0897a974 100644 --- a/data/data-database/src/main/java/com/lighthouse/data/database/repository/gifticon/GifticonEditDatabaseRepositoryImpl.kt +++ b/data/data-database/src/main/java/com/lighthouse/data/database/repository/gifticon/GifticonEditDatabaseRepositoryImpl.kt @@ -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 @@ -14,7 +13,7 @@ internal class GifticonEditDatabaseRepositoryImpl @Inject constructor( override suspend fun insertGifticons( gifticonWithCropList: List - ): DbResult { + ): Result { return runCatchingDB { gifticonEditDao.insertGifticonWithCrop(gifticonWithCropList.toEntity()) } @@ -22,7 +21,7 @@ internal class GifticonEditDatabaseRepositoryImpl @Inject constructor( override suspend fun updateGifticon( gifticonWithCrop: GifticonWithCrop - ): DbResult { + ): Result { return runCatchingDB { gifticonEditDao.updateGifticonWithCrop(gifticonWithCrop.toEntity()) } @@ -31,7 +30,7 @@ internal class GifticonEditDatabaseRepositoryImpl @Inject constructor( override suspend fun deleteGifticon( userId: String, gifticonId: String - ): DbResult { + ): Result { return runCatchingDB { gifticonEditDao.deleteGifticon(userId, gifticonId) } @@ -40,7 +39,7 @@ internal class GifticonEditDatabaseRepositoryImpl @Inject constructor( override suspend fun moveUserIdGifticon( oldUserId: String, newUserId: String - ): DbResult { + ): Result { return runCatchingDB { gifticonEditDao.moveUserIdGifticon(oldUserId, newUserId) } diff --git a/data/data-database/src/main/java/com/lighthouse/data/database/repository/gifticon/GifticonSearchDatabaseRepositoryImpl.kt b/data/data-database/src/main/java/com/lighthouse/data/database/repository/gifticon/GifticonSearchDatabaseRepositoryImpl.kt index d222076fb..00fb61b47 100644 --- a/data/data-database/src/main/java/com/lighthouse/data/database/repository/gifticon/GifticonSearchDatabaseRepositoryImpl.kt +++ b/data/data-database/src/main/java/com/lighthouse/data/database/repository/gifticon/GifticonSearchDatabaseRepositoryImpl.kt @@ -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 @@ -23,7 +22,7 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor( override fun getGifticon( userId: String, gifticonId: String - ): DbResult> { + ): Result> { return runCatchingDB { gifticonSearchDao.getGifticon(userId, gifticonId).map { it.toDomain() @@ -36,7 +35,7 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor( isUsed: Boolean, filterExpired: Boolean, sortBy: SortBy - ): DbResult>> { + ): Result>> { val gifticons = when (filterExpired) { true -> when (sortBy) { SortBy.RECENT -> gifticonSearchDao.getAllGifticonsSortByRecent( @@ -77,7 +76,7 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor( filterBrand: Set, filterExpired: Boolean, sortBy: SortBy - ): DbResult>> { + ): Result>> { val upperFilterBrand = filterBrand.map { it.uppercase() }.toSet() @@ -125,7 +124,7 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor( userId: String, isUsed: Boolean, filterExpired: Boolean - ): DbResult>> { + ): Result>> { val brandWithGifticonCount = when (filterExpired) { true -> gifticonSearchDao.getAllBrands(userId, isUsed, Date()) false -> gifticonSearchDao.getAllBrands(userId, isUsed) @@ -143,7 +142,7 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor( isUsed: Boolean, brand: String, filterExpired: Boolean - ): DbResult>> { + ): Result>> { val gifticons = when (filterExpired) { true -> gifticonSearchDao.getGifticonByBrand(userId, isUsed, brand) false -> gifticonSearchDao.getGifticonByBrand(userId, isUsed, brand, Date()) @@ -158,10 +157,10 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor( override suspend fun getGifticonCrop( userId: String, gifticonId: String - ): DbResult { + ): Result { return runCatchingDB { gifticonSearchDao.getGifticonWithCrop(userId, gifticonId)?.toDomain() - ?: throw NotFoundException("해당 기프티콘을 찾을 수 없습니다.") + ?: throw DBNotFoundException("해당 기프티콘을 찾을 수 없습니다.") } } @@ -169,7 +168,7 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor( userId: String, isUsed: Boolean, filterExpired: Boolean - ): DbResult> { + ): Result> { return runCatchingDB { when (filterExpired) { true -> gifticonSearchDao.hasGifticon(userId, isUsed) @@ -178,7 +177,7 @@ internal class GifticonSearchDatabaseRepositoryImpl @Inject constructor( } } - override suspend fun hasGifticonBrand(userId: String, brand: String): DbResult { + override suspend fun hasGifticonBrand(userId: String, brand: String): Result { return runCatchingDB { gifticonSearchDao.hasGifticonBrand(userId, brand) } diff --git a/data/data-database/src/main/java/com/lighthouse/data/database/repository/gifticon/GifticonUsageHistoryDatabaseRepositoryImpl.kt b/data/data-database/src/main/java/com/lighthouse/data/database/repository/gifticon/GifticonUsageHistoryDatabaseRepositoryImpl.kt index df5e29765..56fda84b1 100644 --- a/data/data-database/src/main/java/com/lighthouse/data/database/repository/gifticon/GifticonUsageHistoryDatabaseRepositoryImpl.kt +++ b/data/data-database/src/main/java/com/lighthouse/data/database/repository/gifticon/GifticonUsageHistoryDatabaseRepositoryImpl.kt @@ -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 @@ -19,7 +18,7 @@ internal class GifticonUsageHistoryDatabaseRepositoryImpl @Inject constructor( userId: String, gifticonId: String, usageHistory: UsageHistory - ): DbResult { + ): Result { return runCatchingDB { gifticonUsageHistoryDao.useGifticonAndInsertHistory( userId, @@ -33,7 +32,7 @@ internal class GifticonUsageHistoryDatabaseRepositoryImpl @Inject constructor( gifticonId: String, amount: Int, usageHistory: UsageHistory - ): DbResult { + ): Result { return runCatchingDB { gifticonUsageHistoryDao.useCashCardGifticonAndInsertHistory( userId, @@ -46,7 +45,7 @@ internal class GifticonUsageHistoryDatabaseRepositoryImpl @Inject constructor( override suspend fun revertUsedGifticon( userId: String, gifticonId: String - ): DbResult { + ): Result { return runCatchingDB { gifticonUsageHistoryDao.revertUsedGifticonAndDeleteHistory( userId, @@ -58,7 +57,7 @@ internal class GifticonUsageHistoryDatabaseRepositoryImpl @Inject constructor( override fun getUsageHistory( userId: String, gifticonId: String - ): DbResult>> { + ): Result>> { return runCatchingDB { gifticonUsageHistoryDao.getUsageHistory( userId, @@ -72,7 +71,7 @@ internal class GifticonUsageHistoryDatabaseRepositoryImpl @Inject constructor( override suspend fun insertUsageHistory( gifticonId: String, usageHistory: UsageHistory - ): DbResult { + ): Result { return runCatchingDB { gifticonUsageHistoryDao.insertUsageHistory(usageHistory.toEntity(gifticonId)) } diff --git a/data/data/src/main/java/com/lighthouse/repository/brand/BrandDatabaseRepository.kt b/data/data/src/main/java/com/lighthouse/repository/brand/BrandDatabaseRepository.kt index 0721df8e7..928d9207f 100644 --- a/data/data/src/main/java/com/lighthouse/repository/brand/BrandDatabaseRepository.kt +++ b/data/data/src/main/java/com/lighthouse/repository/brand/BrandDatabaseRepository.kt @@ -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> + ): Result> suspend fun insertBrands( brandPlaceInfoList: List, x: Dms, y: Dms, brandName: String - ): DbResult + ): Result - suspend fun removeExpirationBrands(): DbResult + suspend fun removeExpirationBrands(): Result } diff --git a/data/data/src/main/java/com/lighthouse/repository/gifticon/GifticonEditDatabaseRepository.kt b/data/data/src/main/java/com/lighthouse/repository/gifticon/GifticonEditDatabaseRepository.kt index ae8947f30..6eee973e6 100644 --- a/data/data/src/main/java/com/lighthouse/repository/gifticon/GifticonEditDatabaseRepository.kt +++ b/data/data/src/main/java/com/lighthouse/repository/gifticon/GifticonEditDatabaseRepository.kt @@ -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 - ): DbResult + ): Result suspend fun updateGifticon( gifticonWithCrop: GifticonWithCrop - ): DbResult + ): Result suspend fun deleteGifticon( userId: String, gifticonId: String - ): DbResult + ): Result suspend fun moveUserIdGifticon( oldUserId: String, newUserId: String - ): DbResult + ): Result } diff --git a/data/data/src/main/java/com/lighthouse/repository/gifticon/GifticonSearchDatabaseRepository.kt b/data/data/src/main/java/com/lighthouse/repository/gifticon/GifticonSearchDatabaseRepository.kt index 879bdd808..1d4c82709 100644 --- a/data/data/src/main/java/com/lighthouse/repository/gifticon/GifticonSearchDatabaseRepository.kt +++ b/data/data/src/main/java/com/lighthouse/repository/gifticon/GifticonSearchDatabaseRepository.kt @@ -4,21 +4,20 @@ 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 kotlinx.coroutines.flow.Flow interface GifticonSearchDatabaseRepository { fun getGifticon( userId: String, gifticonId: String - ): DbResult> + ): Result> fun getAllGifticons( userId: String, isUsed: Boolean, filterExpired: Boolean, sortBy: SortBy = SortBy.DEADLINE - ): DbResult>> + ): Result>> fun getFilteredGifticons( userId: String, @@ -26,34 +25,34 @@ interface GifticonSearchDatabaseRepository { filterBrand: Set, filterExpired: Boolean, sortBy: SortBy = SortBy.DEADLINE - ): DbResult>> + ): Result>> fun getGifticonByBrand( userId: String, isUsed: Boolean, brand: String, filterExpired: Boolean - ): DbResult>> + ): Result>> fun getAllBrands( userId: String, isUsed: Boolean, filterExpired: Boolean - ): DbResult>> + ): Result>> suspend fun getGifticonCrop( userId: String, gifticonId: String - ): DbResult + ): Result fun hasGifticon( userId: String, isUsed: Boolean, filterExpired: Boolean - ): DbResult> + ): Result> suspend fun hasGifticonBrand( userId: String, brand: String - ): DbResult + ): Result } diff --git a/data/data/src/main/java/com/lighthouse/repository/gifticon/GifticonUsageHistoryDatabaseRepository.kt b/data/data/src/main/java/com/lighthouse/repository/gifticon/GifticonUsageHistoryDatabaseRepository.kt index d1bbb4e0e..5d087e6a4 100644 --- a/data/data/src/main/java/com/lighthouse/repository/gifticon/GifticonUsageHistoryDatabaseRepository.kt +++ b/data/data/src/main/java/com/lighthouse/repository/gifticon/GifticonUsageHistoryDatabaseRepository.kt @@ -1,6 +1,5 @@ package com.lighthouse.repository.gifticon -import com.lighthouse.beep.model.result.DbResult import com.lighthouse.beep.model.user.UsageHistory import kotlinx.coroutines.flow.Flow @@ -9,27 +8,27 @@ interface GifticonUsageHistoryDatabaseRepository { userId: String, gifticonId: String, usageHistory: UsageHistory - ): DbResult + ): Result suspend fun useCashCardGifticon( userId: String, gifticonId: String, amount: Int, usageHistory: UsageHistory - ): DbResult + ): Result suspend fun revertUsedGifticon( userId: String, gifticonId: String - ): DbResult + ): Result fun getUsageHistory( userId: String, gifticonId: String - ): DbResult>> + ): Result>> suspend fun insertUsageHistory( gifticonId: String, usageHistory: UsageHistory - ): DbResult + ): Result } diff --git a/model/src/main/java/com/lighthouse/beep/model/exception/db/DeleteException.kt b/model/src/main/java/com/lighthouse/beep/model/exception/db/DeleteException.kt new file mode 100644 index 000000000..ced794159 --- /dev/null +++ b/model/src/main/java/com/lighthouse/beep/model/exception/db/DeleteException.kt @@ -0,0 +1,3 @@ +package com.lighthouse.beep.model.exception.db + +class DeleteException(message: String? = "데이터 Delete 에 실패 했습니다.") : Exception(message)