Skip to content

Commit

Permalink
#14 #4 #5 데이터베이스 싱크 처리 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
kimny927 committed Dec 9, 2024
1 parent 43c92b5 commit 8dd5e51
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ interface PhotoDataSource {

suspend fun getLatestFetchTime(): Result<Long>

suspend fun saveAllPhotoLocation(list: List<PhotoLocationEntity>) : Result<Unit>

suspend fun deleteAllPhotoLocation() : Result<Unit>

suspend fun getPhotoLocation(
latitude: Double,
longitude: Double,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ class PhotoDataSourceImpl @Inject constructor(
}
}

override suspend fun saveAllPhotoLocation(list: List<PhotoLocationEntity>): Result<Unit> {
return runCatching {
photoLocationDao.insertAll(list)
}
}

override suspend fun deleteAllPhotoLocation(): Result<Unit> {
return runCatching {
photoLocationDao.deleteAll()
}
}

override suspend fun getPhotoLocation(
latitude: Double,
longitude: Double,
Expand Down
4 changes: 2 additions & 2 deletions data/src/main/java/ny/photomap/data/db/PhotoLocationDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interface PhotoLocationDao {
@Delete
suspend fun delete(entity: PhotoLocationEntity)

@Delete
suspend fun deleteAll(entityList: List<PhotoLocationEntity>)
@Query("DELETE FROM photo_location_table")
suspend fun deleteAll()

}
16 changes: 13 additions & 3 deletions data/src/main/java/ny/photomap/data/db/PhotoLocationEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,28 @@ data class PhotoLocationEntity(
val generatedTime: Long,
val addedTime: Long,
@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
val thumbNail: ByteArray?
val thumbNail: ByteArray?,
) : ModelMapper<PhotoLocationModel> {
@PrimaryKey(autoGenerate = true)
var id: Long = 0

override fun toModel(): PhotoLocationModel = PhotoLocationModel(
uriString = uri.toString(),
uri = uri,
name = name,
latitude = latitude,
longitude = longitude,
generatedTimeMillis = generatedTime,
addedTimeMillis = addedTime,
thumbNail = thumbNail
)
}
}

fun PhotoLocationModel.toEntity(): PhotoLocationEntity = PhotoLocationEntity(
uri = uri,
name = name,
latitude = latitude,
longitude = longitude,
generatedTime = generatedTimeMillis,
addedTime = addedTimeMillis,
thumbNail = thumbNail
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ data class PhotoLocationData(
val thumbNail: ByteArray?,
) : ModelMapper<PhotoLocationModel> {
override fun toModel(): PhotoLocationModel = PhotoLocationModel(
uriString = uri.toString(),
uri = this@PhotoLocationData.uri.toString(),
name = name,
latitude = latitude,
longitude = longitude,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ny.photomap.data.repository

import ny.photomap.data.datasource.PhotoDataSource
import ny.photomap.data.db.PhotoLocationEntity
import ny.photomap.data.db.toEntity
import ny.photomap.data.model.PhotoLocationData
import ny.photomap.domain.PhotoRepository
import ny.photomap.domain.model.PhotoLocationModel
Expand Down Expand Up @@ -30,6 +31,16 @@ class PhotoRepositoryImpl @Inject constructor(private val dataSource: PhotoDataS
override suspend fun getLatestFetchTime(): Result<Long> =
dataSource.getLatestFetchTime()

override suspend fun saveAllPhotoLocation(list: List<PhotoLocationModel>): Result<Unit> {
return runCatching {
dataSource.saveAllPhotoLocation(
list.map(PhotoLocationModel::toEntity)
)
}
}

override suspend fun deleteAllPhotoLocation(): Result<Unit> =
dataSource.deleteAllPhotoLocation()

override suspend fun getPhotoLocation(
latitude: Double,
Expand Down
4 changes: 4 additions & 0 deletions domain/src/main/java/ny/photomap/domain/PhotoRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ interface PhotoRepository {

suspend fun getLatestFetchTime(): Result<Long>

suspend fun saveAllPhotoLocation(list: List<PhotoLocationModel>) : Result<Unit>

suspend fun deleteAllPhotoLocation() : Result<Unit>

suspend fun getPhotoLocation(
latitude: Double,
longitude: Double,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ny.photomap.domain.model

data class PhotoLocationModel(
val uriString: String,
val uri: String,
val name: String?,
val latitude: Double,
val longitude: Double,
Expand Down

0 comments on commit 8dd5e51

Please sign in to comment.