From f9ef01555f2e6d72d4b3cb5f71080f7ec7c848bc Mon Sep 17 00:00:00 2001 From: Choe-Ji-Hwan Date: Wed, 23 Nov 2022 18:02:20 +0900 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20=EB=8B=A4=EB=8C=80=EB=8B=A4=20?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=20Relation=EC=9D=84=20=EC=9D=B4=EC=9A=A9?= =?UTF-8?q?=ED=95=B4=EC=84=9C=20=EA=B5=AC=ED=98=84=20/=20Fix:=20Foreign=20?= =?UTF-8?q?Key=20Constraint=20Failed=20=EC=97=90=EB=9F=AC=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0=20(https://github.com/boostcampwm-2022/android06-mogl?= =?UTF-8?q?e/issues/52#issue-1461314864)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/wakeup/data/database/dao/MomentDao.kt | 37 ++++++++------ .../data/database/entity/GlobeEntity.kt | 2 +- .../data/database/entity/MomentEntity.kt | 6 +-- .../data/database/entity/MomentGlobeEntity.kt | 21 ++++---- .../database/entity/MomentPictureEntity.kt | 21 ++++---- .../entity/MomentWithGlobesAndPictures.kt | 23 +++++++++ .../data/database/entity/PictureEntity.kt | 3 +- .../database/mapper/MomentEntityMapper.kt | 13 +++++ .../data/repository/MomentRepositoryImpl.kt | 42 +++++++++++----- .../local/moment/MomentLocalDataSource.kt | 20 +++++--- .../local/moment/MomentLocalDataSourceImpl.kt | 49 ++++++++++++++----- .../ui/addmoment/AddMomentViewModel.kt | 4 +- 12 files changed, 168 insertions(+), 73 deletions(-) create mode 100644 data/src/main/java/com/wakeup/data/database/entity/MomentWithGlobesAndPictures.kt diff --git a/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt b/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt index ec1fa9d..cf84dac 100644 --- a/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt +++ b/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt @@ -5,18 +5,22 @@ import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query +import androidx.room.Transaction import com.wakeup.data.database.entity.GlobeEntity import com.wakeup.data.database.entity.MomentEntity +import com.wakeup.data.database.entity.MomentGlobeEntity import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.MomentWithGlobesAndPictures import com.wakeup.data.database.entity.PictureEntity @Dao interface MomentDao { + @Transaction @Query( """ SELECT * FROM moment - WHERE mainAddress LIKE '%' || :query || '%' + WHERE mainAddress LIKE '%' || :query || '%' OR detailAddress LIKE '%' || :query || '%' OR content LIKE '%' || :query || '%' ORDER BY @@ -24,8 +28,9 @@ interface MomentDao { CASE WHEN :sortType = 1 THEN date END ASC """ ) - fun getMoments(sortType: Int = 0, query: String): PagingSource + fun getMoments(sortType: Int = 0, query: String): PagingSource + @Transaction @Query( """ SELECT *, @@ -38,26 +43,30 @@ interface MomentDao { ORDER BY distance """ ) - fun getMomentsByNearestDistance(query: String, lat: Double?, lng: Double?): PagingSource + fun getMomentsByNearestDistance( + query: String, + lat: Double?, + lng: Double?, + ): PagingSource - @Query( - "SELECT * FROM picture WHERE id IN" + - "(SELECT picture_id FROM moment_picture WHERE moment_id = :momentId)" - ) - suspend fun getPictures(momentId: Long): List + @Query("SELECT globe_entity_id FROM globe WHERE name = :globeName") + suspend fun getGlobeId(globeName: String): Long - @Query( - "SELECT * FROM globe WHERE id IN " + - "(SELECT globe_id FROM moment_globe WHERE moment_id = :momentId)" - ) - suspend fun getGlobes(momentId: Long): List + @Query("SELECT picture_entity_id FROM picture WHERE bitmap = :bitmap") + suspend fun getPictureByByteArray(bitmap: ByteArray): Long @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun saveMoment(moment: MomentEntity): Long - @Insert(onConflict = OnConflictStrategy.REPLACE) + @Insert(onConflict = OnConflictStrategy.IGNORE) suspend fun savePicture(picture: List): List @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun saveMomentPicture(momentPictures: List) + + @Insert(onConflict = OnConflictStrategy.IGNORE) + suspend fun saveGlobes(globes: List): List + + @Insert(onConflict = OnConflictStrategy.REPLACE) + suspend fun saveMomentGlobe(momentGlobe: MomentGlobeEntity) } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/entity/GlobeEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/GlobeEntity.kt index 700c52d..119a889 100644 --- a/data/src/main/java/com/wakeup/data/database/entity/GlobeEntity.kt +++ b/data/src/main/java/com/wakeup/data/database/entity/GlobeEntity.kt @@ -10,6 +10,6 @@ import androidx.room.PrimaryKey indices = [Index(value = ["name"], unique = true)] ) data class GlobeEntity( - @PrimaryKey(autoGenerate = true) val id: Long = 0L, + @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "globe_entity_id") val id: Long = 0L, @ColumnInfo(name = "name") val name: String, ) diff --git a/data/src/main/java/com/wakeup/data/database/entity/MomentEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/MomentEntity.kt index 187be65..853dd07 100644 --- a/data/src/main/java/com/wakeup/data/database/entity/MomentEntity.kt +++ b/data/src/main/java/com/wakeup/data/database/entity/MomentEntity.kt @@ -12,14 +12,14 @@ import androidx.room.PrimaryKey foreignKeys = [ ForeignKey( entity = PictureEntity::class, - parentColumns = ["id"], + parentColumns = ["picture_entity_id"], childColumns = ["thumbnail_id"], - onDelete = CASCADE + onUpdate = CASCADE ) ] ) data class MomentEntity( - @PrimaryKey(autoGenerate = true) val id: Long = 0L, + @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "moment_entity_id") val id: Long = 0L, @Embedded val place: PlaceEntity, @ColumnInfo(name = "thumbnail_id") val thumbnailId: Long?, @ColumnInfo(name = "content") val content: String, diff --git a/data/src/main/java/com/wakeup/data/database/entity/MomentGlobeEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/MomentGlobeEntity.kt index 4b96352..34e0788 100644 --- a/data/src/main/java/com/wakeup/data/database/entity/MomentGlobeEntity.kt +++ b/data/src/main/java/com/wakeup/data/database/entity/MomentGlobeEntity.kt @@ -3,27 +3,26 @@ package com.wakeup.data.database.entity import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.ForeignKey -import androidx.room.PrimaryKey @Entity( tableName = "moment_globe", foreignKeys = [ ForeignKey( entity = MomentEntity::class, - parentColumns = ["id"], - childColumns = ["moment_id"], - onDelete = ForeignKey.CASCADE + parentColumns = ["moment_entity_id"], + childColumns = ["moment_entity_id"], + onUpdate = ForeignKey.CASCADE ), ForeignKey( entity = GlobeEntity::class, - parentColumns = ["id"], - childColumns = ["globe_id"], - onDelete = ForeignKey.CASCADE + parentColumns = ["globe_entity_id"], + childColumns = ["globe_entity_id"], + onUpdate = ForeignKey.CASCADE ) - ] + ], + primaryKeys = ["moment_entity_id", "globe_entity_id"] ) data class MomentGlobeEntity( - @PrimaryKey(autoGenerate = true) val id: Long = 0L, - @ColumnInfo(name = "moment_id", index = true) val momentId: Int, - @ColumnInfo(name = "globe_id", index = true) val globeId: Int, + @ColumnInfo(name = "moment_entity_id", index = true) val momentId: Long, + @ColumnInfo(name = "globe_entity_id", index = true) val globeId: Long, ) diff --git a/data/src/main/java/com/wakeup/data/database/entity/MomentPictureEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/MomentPictureEntity.kt index f0ac243..0d0e3b2 100644 --- a/data/src/main/java/com/wakeup/data/database/entity/MomentPictureEntity.kt +++ b/data/src/main/java/com/wakeup/data/database/entity/MomentPictureEntity.kt @@ -3,27 +3,26 @@ package com.wakeup.data.database.entity import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.ForeignKey -import androidx.room.PrimaryKey @Entity( tableName = "moment_picture", foreignKeys = [ ForeignKey( entity = MomentEntity::class, - parentColumns = ["id"], - childColumns = ["moment_id"], - onDelete = ForeignKey.CASCADE + parentColumns = ["moment_entity_id"], + childColumns = ["moment_entity_id"], + onUpdate = ForeignKey.CASCADE ), ForeignKey( entity = PictureEntity::class, - parentColumns = ["id"], - childColumns = ["picture_id"], - onDelete = ForeignKey.CASCADE + parentColumns = ["picture_entity_id"], + childColumns = ["picture_entity_id"], + onUpdate = ForeignKey.CASCADE ) - ] + ], + primaryKeys = ["moment_entity_id", "picture_entity_id"] ) data class MomentPictureEntity( - @PrimaryKey(autoGenerate = true) val id: Long = 0L, - @ColumnInfo(name = "moment_id", index = true) val momentId: Long, - @ColumnInfo(name = "picture_id", index = true) val pictureId: Long, + @ColumnInfo(name = "moment_entity_id", index = true) val momentId: Long, + @ColumnInfo(name = "picture_entity_id", index = true) val pictureId: Long, ) \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/entity/MomentWithGlobesAndPictures.kt b/data/src/main/java/com/wakeup/data/database/entity/MomentWithGlobesAndPictures.kt new file mode 100644 index 0000000..9a11615 --- /dev/null +++ b/data/src/main/java/com/wakeup/data/database/entity/MomentWithGlobesAndPictures.kt @@ -0,0 +1,23 @@ +package com.wakeup.data.database.entity + +import androidx.room.Embedded +import androidx.room.Junction +import androidx.room.Relation + +data class MomentWithGlobesAndPictures( + @Embedded val moment: MomentEntity, + @Relation( + parentColumn = "moment_entity_id", + entity = GlobeEntity::class, + entityColumn = "globe_entity_id", + associateBy = Junction(MomentGlobeEntity::class) + ) + val globeList: List, + @Relation( + parentColumn = "moment_entity_id", + entity = PictureEntity::class, + entityColumn = "picture_entity_id", + associateBy = Junction(MomentPictureEntity::class) + ) + val pictureList: List, +) \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/entity/PictureEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/PictureEntity.kt index 90f28c8..05151e9 100644 --- a/data/src/main/java/com/wakeup/data/database/entity/PictureEntity.kt +++ b/data/src/main/java/com/wakeup/data/database/entity/PictureEntity.kt @@ -10,7 +10,7 @@ import androidx.room.PrimaryKey indices = [Index(value = ["bitmap"], unique = true)] ) data class PictureEntity( - @PrimaryKey(autoGenerate = true) val id: Long = 0L, + @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "picture_entity_id") val id: Long = 0L, @ColumnInfo(name = "bitmap") val bitmap: ByteArray, ) { override fun equals(other: Any?): Boolean { @@ -30,4 +30,5 @@ data class PictureEntity( result = 31 * result + bitmap.contentHashCode() return result } + } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/mapper/MomentEntityMapper.kt b/data/src/main/java/com/wakeup/data/database/mapper/MomentEntityMapper.kt index dd3f3a1..7dee849 100644 --- a/data/src/main/java/com/wakeup/data/database/mapper/MomentEntityMapper.kt +++ b/data/src/main/java/com/wakeup/data/database/mapper/MomentEntityMapper.kt @@ -2,6 +2,7 @@ package com.wakeup.data.database.mapper import com.wakeup.data.database.entity.GlobeEntity import com.wakeup.data.database.entity.MomentEntity +import com.wakeup.data.database.entity.MomentWithGlobesAndPictures import com.wakeup.data.database.entity.PictureEntity import com.wakeup.domain.model.Moment import com.wakeup.domain.model.Place @@ -17,6 +18,18 @@ fun MomentEntity.toDomain(pictures: List, globes: List, globes: List): Moment { + return Moment( + id = moment.id, + place = moment.place.toDomain(), + pictures = pictures.map { it.toDomain() }, + content = moment.content, + globes = globes.map { it.toDomain() }, + date = moment.date + ) +} + + fun Moment.toEntity(place: Place, thumbnailId: Long?): MomentEntity { return MomentEntity( place = place.toEntity(), diff --git a/data/src/main/java/com/wakeup/data/repository/MomentRepositoryImpl.kt b/data/src/main/java/com/wakeup/data/repository/MomentRepositoryImpl.kt index a85b32a..a422ae9 100644 --- a/data/src/main/java/com/wakeup/data/repository/MomentRepositoryImpl.kt +++ b/data/src/main/java/com/wakeup/data/repository/MomentRepositoryImpl.kt @@ -2,32 +2,47 @@ package com.wakeup.data.repository import androidx.paging.PagingData import androidx.paging.map +import com.wakeup.data.database.entity.GlobeEntity +import com.wakeup.data.database.entity.MomentGlobeEntity import com.wakeup.data.database.entity.MomentPictureEntity import com.wakeup.data.database.mapper.toDomain import com.wakeup.data.database.mapper.toEntity import com.wakeup.data.source.local.moment.MomentLocalDataSource import com.wakeup.domain.model.Location import com.wakeup.domain.model.Moment -import com.wakeup.domain.model.Place import com.wakeup.domain.model.SortType -import com.wakeup.domain.model.Picture import com.wakeup.domain.repository.MomentRepository +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map -import timber.log.Timber +import kotlinx.coroutines.launch import javax.inject.Inject class MomentRepositoryImpl @Inject constructor( private val localDataSource: MomentLocalDataSource, ) : MomentRepository { - override fun getMoments(sort: SortType, query: String, myLocation: Location?): Flow> = + init { + // todo: globe_test_code (must remove to release) + CoroutineScope(Dispatchers.IO).launch { + localDataSource.saveGlobes(listOf( + GlobeEntity(name = "default"), + GlobeEntity(name = "globe 1"), + GlobeEntity(name = "globe 2"), + GlobeEntity(name = "globe 3")) + ) + } + } + + override fun getMoments( + sort: SortType, + query: String, + myLocation: Location?, + ): Flow> = localDataSource.getMoments(sort, query, myLocation?.toEntity()).map { pagingData -> - pagingData.map { momentEntity -> - momentEntity.toDomain( - localDataSource.getPictures(momentEntity.id), - localDataSource.getGlobes(momentEntity.id) - ) + pagingData.map { momentInfo -> + momentInfo.toDomain(momentInfo.pictureList, momentInfo.globeList) } } @@ -37,15 +52,20 @@ class MomentRepositoryImpl @Inject constructor( return } else { val pictureIndexes = - localDataSource.savePicture(moment.pictures.map { it.toEntity() }) + localDataSource.savePictures(moment.pictures.map { it.toEntity() }) + // 정책: moment 추가할 때 항상 globe 하나 선택해서 추가(default 도 하나 선택해서 추가 임). + val globeIndex = localDataSource.getGlobeId(moment.globes[0].name) val momentIndex = localDataSource.saveMoment(moment.toEntity(moment.place, pictureIndexes[0])) - localDataSource.saveMomentPicture( + localDataSource.saveMomentPictures( pictureIndexes.map { pictureId -> MomentPictureEntity(momentId = momentIndex, pictureId = pictureId) } ) + localDataSource.saveMomentGlobe( + MomentGlobeEntity(momentId = momentIndex, globeId = globeIndex) + ) } } } diff --git a/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSource.kt b/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSource.kt index e0662de..1e05d50 100644 --- a/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSource.kt +++ b/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSource.kt @@ -2,24 +2,32 @@ package com.wakeup.data.source.local.moment import androidx.paging.PagingData import com.wakeup.data.database.entity.GlobeEntity +import com.wakeup.data.database.entity.LocationEntity import com.wakeup.data.database.entity.MomentEntity +import com.wakeup.data.database.entity.MomentGlobeEntity import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.MomentWithGlobesAndPictures import com.wakeup.data.database.entity.PictureEntity -import com.wakeup.data.database.entity.LocationEntity import com.wakeup.domain.model.SortType import kotlinx.coroutines.flow.Flow interface MomentLocalDataSource { - fun getMoments(sortType: SortType, query: String, myLocation: LocationEntity?): Flow> + fun getMoments(sortType: SortType, query: String, myLocation: LocationEntity?): Flow> + +/* suspend fun getPictures(momentId: Long): List - suspend fun getPictures(momentId: Long): List + suspend fun getGlobes(momentId: Long): List*/ - suspend fun getGlobes(momentId: Long): List + suspend fun getGlobeId(globeName: String): Long suspend fun saveMoment(moment: MomentEntity): Long - suspend fun savePicture(picture: List): List + suspend fun savePictures(pictures: List): List + + suspend fun saveMomentPictures(momentPictures :List) + + suspend fun saveGlobes(globes: List): List - suspend fun saveMomentPicture(MomentPictures :List) + suspend fun saveMomentGlobe(momentGlobe :MomentGlobeEntity) } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt b/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt index fd93f77..ecc40db 100644 --- a/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt +++ b/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt @@ -5,10 +5,12 @@ import androidx.paging.PagingConfig import androidx.paging.PagingData import com.wakeup.data.database.dao.MomentDao import com.wakeup.data.database.entity.GlobeEntity +import com.wakeup.data.database.entity.LocationEntity import com.wakeup.data.database.entity.MomentEntity +import com.wakeup.data.database.entity.MomentGlobeEntity import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.MomentWithGlobesAndPictures import com.wakeup.data.database.entity.PictureEntity -import com.wakeup.data.database.entity.LocationEntity import com.wakeup.domain.model.SortType import kotlinx.coroutines.flow.Flow import javax.inject.Inject @@ -16,7 +18,11 @@ import javax.inject.Inject class MomentLocalDataSourceImpl @Inject constructor( private val momentDao: MomentDao, ) : MomentLocalDataSource { - override fun getMoments(sortType: SortType, query: String, myLocation: LocationEntity?): Flow> = + override fun getMoments( + sortType: SortType, + query: String, + myLocation: LocationEntity?, + ): Flow> = Pager( config = PagingConfig( pageSize = ITEMS_PER_PAGE, @@ -26,30 +32,47 @@ class MomentLocalDataSourceImpl @Inject constructor( ), pagingSourceFactory = { when (sortType) { - SortType.NEAREST -> momentDao.getMomentsByNearestDistance(query, myLocation?.latitude, myLocation?.longitude) + SortType.NEAREST -> momentDao.getMomentsByNearestDistance(query, + myLocation?.latitude, + myLocation?.longitude) else -> momentDao.getMoments(sortType.ordinal, query) } } ).flow - override suspend fun getPictures(momentId: Long): List = - momentDao.getPictures(momentId) + override suspend fun getGlobeId(globeName: String): Long { + return momentDao.getGlobeId(globeName) + } - override suspend fun getGlobes(momentId: Long): List = - momentDao.getGlobes(momentId) + override suspend fun saveMoment(moment: MomentEntity): Long { + return momentDao.saveMoment(moment) + } - override suspend fun saveMoment(moment: MomentEntity): Long = - momentDao.saveMoment(moment) + override suspend fun savePictures(pictures: List): List { + val indexResult = momentDao.savePicture(pictures).toMutableList() + indexResult.forEachIndexed { pictureIndex, id -> + if (id == EXIST_INSERT_ERROR_CODE) { + indexResult[pictureIndex] = momentDao.getPictureByByteArray(pictures[pictureIndex].bitmap) + } + } + return indexResult.toList() + } + + override suspend fun saveMomentPictures(momentPictures: List) { + momentDao.saveMomentPicture(momentPictures) + } - override suspend fun savePicture(picture: List): List = - momentDao.savePicture(picture) + override suspend fun saveGlobes(globes: List): List { + return momentDao.saveGlobes(globes) + } - override suspend fun saveMomentPicture(MomentPictures: List) { - momentDao.saveMomentPicture(MomentPictures) + override suspend fun saveMomentGlobe(momentGlobe: MomentGlobeEntity) { + momentDao.saveMomentGlobe(momentGlobe) } companion object { const val PREFETCH_PAGE = 2 const val ITEMS_PER_PAGE = 10 + const val EXIST_INSERT_ERROR_CODE = -1L } } \ No newline at end of file diff --git a/presentation/src/main/java/com/wakeup/presentation/ui/addmoment/AddMomentViewModel.kt b/presentation/src/main/java/com/wakeup/presentation/ui/addmoment/AddMomentViewModel.kt index c5d4369..e3464e3 100644 --- a/presentation/src/main/java/com/wakeup/presentation/ui/addmoment/AddMomentViewModel.kt +++ b/presentation/src/main/java/com/wakeup/presentation/ui/addmoment/AddMomentViewModel.kt @@ -2,11 +2,11 @@ package com.wakeup.presentation.ui.addmoment import android.content.Intent import androidx.lifecycle.ViewModel -import com.wakeup.presentation.model.LocationModel import androidx.lifecycle.viewModelScope import com.wakeup.domain.usecase.SaveMomentUseCase import com.wakeup.presentation.mapper.toDomain import com.wakeup.presentation.model.GlobeModel +import com.wakeup.presentation.model.LocationModel import com.wakeup.presentation.model.MomentModel import com.wakeup.presentation.model.PictureModel import com.wakeup.presentation.model.PlaceModel @@ -114,7 +114,7 @@ class AddMomentViewModel @Inject constructor( date = selectedDate.value ).toDomain() ) - Timber.d("${pictures.value[0]}") + //Timber.d("${pictures.value[0]}") } } } \ No newline at end of file From 727c6ae5252476a154a8d238174a0c85daa91a83 Mon Sep 17 00:00:00 2001 From: Choe-Ji-Hwan Date: Wed, 23 Nov 2022 18:13:08 +0900 Subject: [PATCH 2/3] =?UTF-8?q?style:=20Dao=EC=9D=98=20=ED=95=A8=EC=88=98?= =?UTF-8?q?=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20=EB=B3=80=EA=B2=BD=20(https://?= =?UTF-8?q?github.com/boostcampwm-2022/android06-mogle/issues/52#issue-146?= =?UTF-8?q?1314864)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt | 4 ++-- .../data/source/local/moment/MomentLocalDataSourceImpl.kt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt b/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt index cf84dac..43092a8 100644 --- a/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt +++ b/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt @@ -50,10 +50,10 @@ interface MomentDao { ): PagingSource @Query("SELECT globe_entity_id FROM globe WHERE name = :globeName") - suspend fun getGlobeId(globeName: String): Long + suspend fun getGlobeIdByName(globeName: String): Long @Query("SELECT picture_entity_id FROM picture WHERE bitmap = :bitmap") - suspend fun getPictureByByteArray(bitmap: ByteArray): Long + suspend fun getPictureIdByByteArray(bitmap: ByteArray): Long @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun saveMoment(moment: MomentEntity): Long diff --git a/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt b/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt index ecc40db..07b852b 100644 --- a/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt +++ b/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt @@ -41,7 +41,7 @@ class MomentLocalDataSourceImpl @Inject constructor( ).flow override suspend fun getGlobeId(globeName: String): Long { - return momentDao.getGlobeId(globeName) + return momentDao.getGlobeIdByName(globeName) } override suspend fun saveMoment(moment: MomentEntity): Long { @@ -52,7 +52,7 @@ class MomentLocalDataSourceImpl @Inject constructor( val indexResult = momentDao.savePicture(pictures).toMutableList() indexResult.forEachIndexed { pictureIndex, id -> if (id == EXIST_INSERT_ERROR_CODE) { - indexResult[pictureIndex] = momentDao.getPictureByByteArray(pictures[pictureIndex].bitmap) + indexResult[pictureIndex] = momentDao.getPictureIdByByteArray(pictures[pictureIndex].bitmap) } } return indexResult.toList() From 3c23d6ffd726c42f41fc35b3524ac700b1459a33 Mon Sep 17 00:00:00 2001 From: Choe-Ji-Hwan Date: Wed, 23 Nov 2022 21:38:31 +0900 Subject: [PATCH 3/3] =?UTF-8?q?style:=20=EB=A6=AC=EB=B7=B0=20=EB=82=B4?= =?UTF-8?q?=EC=9A=A9=20=EB=B0=98=EC=98=81=20(=EB=84=A4=EC=9D=B4=EB=B0=8D?= =?UTF-8?q?=20=EC=BB=A8=EB=B2=A4=EC=85=98)=20(https://github.com/boostcamp?= =?UTF-8?q?wm-2022/android06-mogle/issues/52#issue-1461314864)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/wakeup/data/DataUnitTest.kt | 20 +++++------ .../com/wakeup/data/database/MogleDatabase.kt | 10 +++--- .../com/wakeup/data/database/dao/MomentDao.kt | 14 ++++---- .../data/database/entity/GlobeEntity.kt | 2 +- .../data/database/entity/MomentEntity.kt | 6 ++-- .../data/database/entity/MomentGlobeEntity.kt | 28 --------------- .../data/database/entity/MomentGlobeXRef.kt | 28 +++++++++++++++ .../database/entity/MomentPictureEntity.kt | 28 --------------- .../data/database/entity/MomentPictureXRef.kt | 28 +++++++++++++++ .../entity/MomentWithGlobesAndPictures.kt | 12 +++---- .../data/database/entity/PictureEntity.kt | 2 +- .../com/wakeup/data/di/DataSourceModule.kt | 4 +-- .../java/com/wakeup/data/di/DatabaseModule.kt | 7 ++-- .../java/com/wakeup/data/di/NetworkModule.kt | 3 +- .../com/wakeup/data/di/RepositoryModule.kt | 4 +-- .../data/repository/MomentRepositoryImpl.kt | 35 +++++++++---------- .../local/moment/MomentLocalDataSource.kt | 8 ++--- .../local/moment/MomentLocalDataSourceImpl.kt | 14 ++++---- 18 files changed, 127 insertions(+), 126 deletions(-) delete mode 100644 data/src/main/java/com/wakeup/data/database/entity/MomentGlobeEntity.kt create mode 100644 data/src/main/java/com/wakeup/data/database/entity/MomentGlobeXRef.kt delete mode 100644 data/src/main/java/com/wakeup/data/database/entity/MomentPictureEntity.kt create mode 100644 data/src/main/java/com/wakeup/data/database/entity/MomentPictureXRef.kt diff --git a/data/src/androidTest/java/com/wakeup/data/DataUnitTest.kt b/data/src/androidTest/java/com/wakeup/data/DataUnitTest.kt index 25e9226..98ef951 100644 --- a/data/src/androidTest/java/com/wakeup/data/DataUnitTest.kt +++ b/data/src/androidTest/java/com/wakeup/data/DataUnitTest.kt @@ -10,7 +10,7 @@ import androidx.test.filters.SmallTest import com.wakeup.data.database.MogleDatabase import com.wakeup.data.database.dao.MomentDao import com.wakeup.data.database.entity.MomentEntity -import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.MomentPictureXRef import com.wakeup.data.database.entity.PictureEntity import com.wakeup.data.database.entity.PlaceEntity import com.wakeup.data.source.local.moment.MomentLocalDataSourceImpl @@ -44,7 +44,7 @@ class DataUnitTest { @Test fun saveMomentAndGetMoments(): Unit = runBlocking { - val pictureId = momentDao.savePicture(listOf( + val pictureId = momentDao.savePictures(listOf( PictureEntity( bitmap = "picture1".toByteArray() ), @@ -79,31 +79,31 @@ class DataUnitTest { date = System.currentTimeMillis() )) - momentDao.saveMomentPicture(listOf( - MomentPictureEntity( + momentDao.saveMomentPictureXRefs(listOf( + MomentPictureXRef( momentId = momentId1, pictureId = pictureId[0] ), - MomentPictureEntity( + MomentPictureXRef( momentId = momentId1, pictureId = pictureId[1] ), - MomentPictureEntity( + MomentPictureXRef( momentId = momentId1, pictureId = pictureId[2] ) )) - momentDao.saveMomentPicture(listOf( - MomentPictureEntity( + momentDao.saveMomentPictureXRefs(listOf( + MomentPictureXRef( momentId = momentId2, pictureId = pictureId[0] ), - MomentPictureEntity( + MomentPictureXRef( momentId = momentId2, pictureId = pictureId[1] ), - MomentPictureEntity( + MomentPictureXRef( momentId = momentId2, pictureId = pictureId[2] ) diff --git a/data/src/main/java/com/wakeup/data/database/MogleDatabase.kt b/data/src/main/java/com/wakeup/data/database/MogleDatabase.kt index 2785117..8ad50c9 100644 --- a/data/src/main/java/com/wakeup/data/database/MogleDatabase.kt +++ b/data/src/main/java/com/wakeup/data/database/MogleDatabase.kt @@ -5,21 +5,21 @@ import androidx.room.RoomDatabase import com.wakeup.data.database.dao.MomentDao import com.wakeup.data.database.entity.GlobeEntity import com.wakeup.data.database.entity.MomentEntity -import com.wakeup.data.database.entity.MomentGlobeEntity -import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.MomentGlobeXRef +import com.wakeup.data.database.entity.MomentPictureXRef import com.wakeup.data.database.entity.PictureEntity @Database( entities = [ MomentEntity::class, GlobeEntity::class, - MomentGlobeEntity::class, - MomentPictureEntity::class, + MomentGlobeXRef::class, + MomentPictureXRef::class, PictureEntity::class ], version = 1, exportSchema = false ) -abstract class MogleDatabase: RoomDatabase() { +abstract class MogleDatabase : RoomDatabase() { abstract fun momentDao(): MomentDao } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt b/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt index 43092a8..4e67e46 100644 --- a/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt +++ b/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt @@ -8,8 +8,8 @@ import androidx.room.Query import androidx.room.Transaction import com.wakeup.data.database.entity.GlobeEntity import com.wakeup.data.database.entity.MomentEntity -import com.wakeup.data.database.entity.MomentGlobeEntity -import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.MomentGlobeXRef +import com.wakeup.data.database.entity.MomentPictureXRef import com.wakeup.data.database.entity.MomentWithGlobesAndPictures import com.wakeup.data.database.entity.PictureEntity @@ -49,24 +49,24 @@ interface MomentDao { lng: Double?, ): PagingSource - @Query("SELECT globe_entity_id FROM globe WHERE name = :globeName") + @Query("SELECT globe_id FROM globe WHERE name = :globeName") suspend fun getGlobeIdByName(globeName: String): Long - @Query("SELECT picture_entity_id FROM picture WHERE bitmap = :bitmap") + @Query("SELECT picture_id FROM picture WHERE bitmap = :bitmap") suspend fun getPictureIdByByteArray(bitmap: ByteArray): Long @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun saveMoment(moment: MomentEntity): Long @Insert(onConflict = OnConflictStrategy.IGNORE) - suspend fun savePicture(picture: List): List + suspend fun savePictures(pictures: List): List @Insert(onConflict = OnConflictStrategy.REPLACE) - suspend fun saveMomentPicture(momentPictures: List) + suspend fun saveMomentPictureXRefs(momentPictures: List) @Insert(onConflict = OnConflictStrategy.IGNORE) suspend fun saveGlobes(globes: List): List @Insert(onConflict = OnConflictStrategy.REPLACE) - suspend fun saveMomentGlobe(momentGlobe: MomentGlobeEntity) + suspend fun saveMomentGlobeXRef(momentGlobe: MomentGlobeXRef) } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/entity/GlobeEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/GlobeEntity.kt index 119a889..e965646 100644 --- a/data/src/main/java/com/wakeup/data/database/entity/GlobeEntity.kt +++ b/data/src/main/java/com/wakeup/data/database/entity/GlobeEntity.kt @@ -10,6 +10,6 @@ import androidx.room.PrimaryKey indices = [Index(value = ["name"], unique = true)] ) data class GlobeEntity( - @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "globe_entity_id") val id: Long = 0L, + @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "globe_id") val id: Long = 0L, @ColumnInfo(name = "name") val name: String, ) diff --git a/data/src/main/java/com/wakeup/data/database/entity/MomentEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/MomentEntity.kt index 853dd07..e0410b1 100644 --- a/data/src/main/java/com/wakeup/data/database/entity/MomentEntity.kt +++ b/data/src/main/java/com/wakeup/data/database/entity/MomentEntity.kt @@ -12,16 +12,16 @@ import androidx.room.PrimaryKey foreignKeys = [ ForeignKey( entity = PictureEntity::class, - parentColumns = ["picture_entity_id"], + parentColumns = ["picture_id"], childColumns = ["thumbnail_id"], onUpdate = CASCADE ) ] ) data class MomentEntity( - @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "moment_entity_id") val id: Long = 0L, + @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "moment_id") val id: Long = 0L, @Embedded val place: PlaceEntity, - @ColumnInfo(name = "thumbnail_id") val thumbnailId: Long?, + @ColumnInfo(name = "thumbnail_id", index = true) val thumbnailId: Long?, @ColumnInfo(name = "content") val content: String, @ColumnInfo(name = "date") val date: Long, ) \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/entity/MomentGlobeEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/MomentGlobeEntity.kt deleted file mode 100644 index 34e0788..0000000 --- a/data/src/main/java/com/wakeup/data/database/entity/MomentGlobeEntity.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.wakeup.data.database.entity - -import androidx.room.ColumnInfo -import androidx.room.Entity -import androidx.room.ForeignKey - -@Entity( - tableName = "moment_globe", - foreignKeys = [ - ForeignKey( - entity = MomentEntity::class, - parentColumns = ["moment_entity_id"], - childColumns = ["moment_entity_id"], - onUpdate = ForeignKey.CASCADE - ), - ForeignKey( - entity = GlobeEntity::class, - parentColumns = ["globe_entity_id"], - childColumns = ["globe_entity_id"], - onUpdate = ForeignKey.CASCADE - ) - ], - primaryKeys = ["moment_entity_id", "globe_entity_id"] -) -data class MomentGlobeEntity( - @ColumnInfo(name = "moment_entity_id", index = true) val momentId: Long, - @ColumnInfo(name = "globe_entity_id", index = true) val globeId: Long, -) diff --git a/data/src/main/java/com/wakeup/data/database/entity/MomentGlobeXRef.kt b/data/src/main/java/com/wakeup/data/database/entity/MomentGlobeXRef.kt new file mode 100644 index 0000000..96966c4 --- /dev/null +++ b/data/src/main/java/com/wakeup/data/database/entity/MomentGlobeXRef.kt @@ -0,0 +1,28 @@ +package com.wakeup.data.database.entity + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.ForeignKey + +@Entity( + tableName = "moment_globe", + foreignKeys = [ + ForeignKey( + entity = MomentEntity::class, + parentColumns = ["moment_id"], + childColumns = ["moment_id"], + onUpdate = ForeignKey.CASCADE + ), + ForeignKey( + entity = GlobeEntity::class, + parentColumns = ["globe_id"], + childColumns = ["globe_id"], + onUpdate = ForeignKey.CASCADE + ) + ], + primaryKeys = ["moment_id", "globe_id"] +) +data class MomentGlobeXRef( + @ColumnInfo(name = "moment_id", index = true) val momentId: Long, + @ColumnInfo(name = "globe_id", index = true) val globeId: Long, +) diff --git a/data/src/main/java/com/wakeup/data/database/entity/MomentPictureEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/MomentPictureEntity.kt deleted file mode 100644 index 0d0e3b2..0000000 --- a/data/src/main/java/com/wakeup/data/database/entity/MomentPictureEntity.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.wakeup.data.database.entity - -import androidx.room.ColumnInfo -import androidx.room.Entity -import androidx.room.ForeignKey - -@Entity( - tableName = "moment_picture", - foreignKeys = [ - ForeignKey( - entity = MomentEntity::class, - parentColumns = ["moment_entity_id"], - childColumns = ["moment_entity_id"], - onUpdate = ForeignKey.CASCADE - ), - ForeignKey( - entity = PictureEntity::class, - parentColumns = ["picture_entity_id"], - childColumns = ["picture_entity_id"], - onUpdate = ForeignKey.CASCADE - ) - ], - primaryKeys = ["moment_entity_id", "picture_entity_id"] -) -data class MomentPictureEntity( - @ColumnInfo(name = "moment_entity_id", index = true) val momentId: Long, - @ColumnInfo(name = "picture_entity_id", index = true) val pictureId: Long, -) \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/entity/MomentPictureXRef.kt b/data/src/main/java/com/wakeup/data/database/entity/MomentPictureXRef.kt new file mode 100644 index 0000000..313dafc --- /dev/null +++ b/data/src/main/java/com/wakeup/data/database/entity/MomentPictureXRef.kt @@ -0,0 +1,28 @@ +package com.wakeup.data.database.entity + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.ForeignKey + +@Entity( + tableName = "moment_picture", + foreignKeys = [ + ForeignKey( + entity = MomentEntity::class, + parentColumns = ["moment_id"], + childColumns = ["moment_id"], + onUpdate = ForeignKey.CASCADE + ), + ForeignKey( + entity = PictureEntity::class, + parentColumns = ["picture_id"], + childColumns = ["picture_id"], + onUpdate = ForeignKey.CASCADE + ) + ], + primaryKeys = ["moment_id", "picture_id"] +) +data class MomentPictureXRef( + @ColumnInfo(name = "moment_id", index = true) val momentId: Long, + @ColumnInfo(name = "picture_id", index = true) val pictureId: Long, +) \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/entity/MomentWithGlobesAndPictures.kt b/data/src/main/java/com/wakeup/data/database/entity/MomentWithGlobesAndPictures.kt index 9a11615..20b610b 100644 --- a/data/src/main/java/com/wakeup/data/database/entity/MomentWithGlobesAndPictures.kt +++ b/data/src/main/java/com/wakeup/data/database/entity/MomentWithGlobesAndPictures.kt @@ -7,17 +7,17 @@ import androidx.room.Relation data class MomentWithGlobesAndPictures( @Embedded val moment: MomentEntity, @Relation( - parentColumn = "moment_entity_id", + parentColumn = "moment_id", entity = GlobeEntity::class, - entityColumn = "globe_entity_id", - associateBy = Junction(MomentGlobeEntity::class) + entityColumn = "globe_id", + associateBy = Junction(MomentGlobeXRef::class) ) val globeList: List, @Relation( - parentColumn = "moment_entity_id", + parentColumn = "moment_id", entity = PictureEntity::class, - entityColumn = "picture_entity_id", - associateBy = Junction(MomentPictureEntity::class) + entityColumn = "picture_id", + associateBy = Junction(MomentPictureXRef::class) ) val pictureList: List, ) \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/entity/PictureEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/PictureEntity.kt index 05151e9..7cf1956 100644 --- a/data/src/main/java/com/wakeup/data/database/entity/PictureEntity.kt +++ b/data/src/main/java/com/wakeup/data/database/entity/PictureEntity.kt @@ -10,7 +10,7 @@ import androidx.room.PrimaryKey indices = [Index(value = ["bitmap"], unique = true)] ) data class PictureEntity( - @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "picture_entity_id") val id: Long = 0L, + @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "picture_id") val id: Long = 0L, @ColumnInfo(name = "bitmap") val bitmap: ByteArray, ) { override fun equals(other: Any?): Boolean { diff --git a/data/src/main/java/com/wakeup/data/di/DataSourceModule.kt b/data/src/main/java/com/wakeup/data/di/DataSourceModule.kt index 56eff53..96bd44c 100644 --- a/data/src/main/java/com/wakeup/data/di/DataSourceModule.kt +++ b/data/src/main/java/com/wakeup/data/di/DataSourceModule.kt @@ -15,11 +15,11 @@ interface DataSourceModule { @Binds fun bindMomentLocalDataSource( - momentLocalDataSourceImpl: MomentLocalDataSourceImpl + momentLocalDataSourceImpl: MomentLocalDataSourceImpl, ): MomentLocalDataSource @Binds fun bindPlaceSearchRemoteDataSource( - placeSearchRemoteDataSourceImpl: PlaceSearchRemoteDataSourceImpl + placeSearchRemoteDataSourceImpl: PlaceSearchRemoteDataSourceImpl, ): PlaceSearchRemoteDataSource } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/di/DatabaseModule.kt b/data/src/main/java/com/wakeup/data/di/DatabaseModule.kt index c26ead0..7543d88 100644 --- a/data/src/main/java/com/wakeup/data/di/DatabaseModule.kt +++ b/data/src/main/java/com/wakeup/data/di/DatabaseModule.kt @@ -3,6 +3,7 @@ package com.wakeup.data.di import android.content.Context import androidx.room.Room import com.wakeup.data.database.MogleDatabase +import com.wakeup.data.database.dao.MomentDao import dagger.Module import dagger.Provides import dagger.hilt.InstallIn @@ -17,12 +18,14 @@ object DatabaseModule { @Singleton @Provides fun provideAppDatabase( - @ApplicationContext context: Context + @ApplicationContext context: Context, ): MogleDatabase = Room .databaseBuilder(context, MogleDatabase::class.java, "mogle.db") .build() @Singleton @Provides - fun provideMomentDao(mogleDatabase: MogleDatabase) = mogleDatabase.momentDao() + fun provideMomentDao( + mogleDatabase: MogleDatabase, + ): MomentDao = mogleDatabase.momentDao() } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/di/NetworkModule.kt b/data/src/main/java/com/wakeup/data/di/NetworkModule.kt index e46243c..51901e6 100644 --- a/data/src/main/java/com/wakeup/data/di/NetworkModule.kt +++ b/data/src/main/java/com/wakeup/data/di/NetworkModule.kt @@ -62,7 +62,7 @@ object NetworkModule { @Singleton fun provideKakaoRetrofit( okHttpClient: OkHttpClient, - converterFactory: Converter.Factory + converterFactory: Converter.Factory, ): Retrofit { return Retrofit.Builder() .baseUrl(KAKAO_BASE_URL) @@ -76,5 +76,4 @@ object NetworkModule { fun providePlaceSearchService(retrofit: Retrofit): PlaceSearchService { return retrofit.create(PlaceSearchService::class.java) } - } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/di/RepositoryModule.kt b/data/src/main/java/com/wakeup/data/di/RepositoryModule.kt index 8c6486d..f66b11a 100644 --- a/data/src/main/java/com/wakeup/data/di/RepositoryModule.kt +++ b/data/src/main/java/com/wakeup/data/di/RepositoryModule.kt @@ -15,11 +15,11 @@ interface RepositoryModule { @Binds fun bindMomentRepository( - momentRepositoryImpl: MomentRepositoryImpl + momentRepositoryImpl: MomentRepositoryImpl, ): MomentRepository @Binds fun bindPlaceSearchRepository( - placeSearchRepositoryImpl: PlaceSearchRepositoryImpl + placeSearchRepositoryImpl: PlaceSearchRepositoryImpl, ): PlaceSearchRepository } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/repository/MomentRepositoryImpl.kt b/data/src/main/java/com/wakeup/data/repository/MomentRepositoryImpl.kt index a422ae9..903752a 100644 --- a/data/src/main/java/com/wakeup/data/repository/MomentRepositoryImpl.kt +++ b/data/src/main/java/com/wakeup/data/repository/MomentRepositoryImpl.kt @@ -3,8 +3,8 @@ package com.wakeup.data.repository import androidx.paging.PagingData import androidx.paging.map import com.wakeup.data.database.entity.GlobeEntity -import com.wakeup.data.database.entity.MomentGlobeEntity -import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.MomentGlobeXRef +import com.wakeup.data.database.entity.MomentPictureXRef import com.wakeup.data.database.mapper.toDomain import com.wakeup.data.database.mapper.toEntity import com.wakeup.data.source.local.moment.MomentLocalDataSource @@ -50,22 +50,21 @@ class MomentRepositoryImpl @Inject constructor( if (moment.pictures.isEmpty()) { localDataSource.saveMoment(moment.toEntity(moment.place, null)) return - } else { - val pictureIndexes = - localDataSource.savePictures(moment.pictures.map { it.toEntity() }) - // 정책: moment 추가할 때 항상 globe 하나 선택해서 추가(default 도 하나 선택해서 추가 임). - val globeIndex = localDataSource.getGlobeId(moment.globes[0].name) - val momentIndex = - localDataSource.saveMoment(moment.toEntity(moment.place, pictureIndexes[0])) - - localDataSource.saveMomentPictures( - pictureIndexes.map { pictureId -> - MomentPictureEntity(momentId = momentIndex, pictureId = pictureId) - } - ) - localDataSource.saveMomentGlobe( - MomentGlobeEntity(momentId = momentIndex, globeId = globeIndex) - ) } + val pictureIndexes = + localDataSource.savePictures(moment.pictures.map { it.toEntity() }) + // 정책: moment 추가할 때 항상 globe 하나 선택해서 추가(default 도 하나 선택해서 추가 임). + val globeIndex = localDataSource.getGlobeId(moment.globes[0].name) + val momentIndex = + localDataSource.saveMoment(moment.toEntity(moment.place, pictureIndexes[0])) + + localDataSource.saveMomentPictures( + pictureIndexes.map { pictureId -> + MomentPictureXRef(momentId = momentIndex, pictureId = pictureId) + } + ) + localDataSource.saveMomentGlobe( + MomentGlobeXRef(momentId = momentIndex, globeId = globeIndex) + ) } } diff --git a/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSource.kt b/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSource.kt index 1e05d50..17ffe41 100644 --- a/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSource.kt +++ b/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSource.kt @@ -4,8 +4,8 @@ import androidx.paging.PagingData import com.wakeup.data.database.entity.GlobeEntity import com.wakeup.data.database.entity.LocationEntity import com.wakeup.data.database.entity.MomentEntity -import com.wakeup.data.database.entity.MomentGlobeEntity -import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.MomentGlobeXRef +import com.wakeup.data.database.entity.MomentPictureXRef import com.wakeup.data.database.entity.MomentWithGlobesAndPictures import com.wakeup.data.database.entity.PictureEntity import com.wakeup.domain.model.SortType @@ -25,9 +25,9 @@ interface MomentLocalDataSource { suspend fun savePictures(pictures: List): List - suspend fun saveMomentPictures(momentPictures :List) + suspend fun saveMomentPictures(momentPictures :List) suspend fun saveGlobes(globes: List): List - suspend fun saveMomentGlobe(momentGlobe :MomentGlobeEntity) + suspend fun saveMomentGlobe(momentGlobe :MomentGlobeXRef) } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt b/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt index 07b852b..30f1032 100644 --- a/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt +++ b/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt @@ -7,8 +7,8 @@ import com.wakeup.data.database.dao.MomentDao import com.wakeup.data.database.entity.GlobeEntity import com.wakeup.data.database.entity.LocationEntity import com.wakeup.data.database.entity.MomentEntity -import com.wakeup.data.database.entity.MomentGlobeEntity -import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.MomentGlobeXRef +import com.wakeup.data.database.entity.MomentPictureXRef import com.wakeup.data.database.entity.MomentWithGlobesAndPictures import com.wakeup.data.database.entity.PictureEntity import com.wakeup.domain.model.SortType @@ -49,7 +49,7 @@ class MomentLocalDataSourceImpl @Inject constructor( } override suspend fun savePictures(pictures: List): List { - val indexResult = momentDao.savePicture(pictures).toMutableList() + val indexResult = momentDao.savePictures(pictures).toMutableList() indexResult.forEachIndexed { pictureIndex, id -> if (id == EXIST_INSERT_ERROR_CODE) { indexResult[pictureIndex] = momentDao.getPictureIdByByteArray(pictures[pictureIndex].bitmap) @@ -58,16 +58,16 @@ class MomentLocalDataSourceImpl @Inject constructor( return indexResult.toList() } - override suspend fun saveMomentPictures(momentPictures: List) { - momentDao.saveMomentPicture(momentPictures) + override suspend fun saveMomentPictures(momentPictures: List) { + momentDao.saveMomentPictureXRefs(momentPictures) } override suspend fun saveGlobes(globes: List): List { return momentDao.saveGlobes(globes) } - override suspend fun saveMomentGlobe(momentGlobe: MomentGlobeEntity) { - momentDao.saveMomentGlobe(momentGlobe) + override suspend fun saveMomentGlobe(momentGlobe: MomentGlobeXRef) { + momentDao.saveMomentGlobeXRef(momentGlobe) } companion object {