Skip to content

Commit

Permalink
Merge pull request #23 from KanuKim97/refactor/domain-model
Browse files Browse the repository at this point in the history
[REFACTOR] Refactoring Domain Module
  • Loading branch information
KanuKim97 authored Nov 30, 2024
2 parents 67d81ba + c5cc79d commit c6c7848
Show file tree
Hide file tree
Showing 36 changed files with 250 additions and 355 deletions.
28 changes: 28 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions core/data/src/main/java/com/example/data/mapper/Mapper.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.example.data.mapper

import com.example.data.model.CollectionDataModel
import com.example.database.model.CollectionEntity
import com.example.model.domain.CollectionModel

fun entityToModelMapper(
fun entityToDataModelMapper(
entity: CollectionEntity
): CollectionModel = CollectionModel(
): CollectionDataModel = CollectionDataModel(
id = entity.placeID,
name = entity.placeName,
latLng = entity.placeLatLng,
imgUrl = entity.placeImgUrl
)

fun modelToEntityMapper(
model: CollectionModel
fun dataModelToEntityMapper(
model: CollectionDataModel
): CollectionEntity = CollectionEntity(
placeID = model.id,
placeName = model.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.model.domain
package com.example.data.model

data class CollectionModel(
data class CollectionDataModel(
val id: String,
val name: String,
val latLng: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.example.data.repository

import com.example.model.domain.CollectionModel
import com.example.data.model.CollectionDataModel
import kotlinx.coroutines.flow.Flow

interface DatabaseRepository {
fun readAllCollectionEntities(): Flow<List<CollectionModel>>
fun readAllCollectionEntities(): Flow<List<CollectionDataModel>>

fun readCollectionEntity(placeID: String): Flow<CollectionModel>
fun readCollectionEntity(placeId: String): Flow<CollectionDataModel>

fun saveUserCollection(content: CollectionModel): Flow<Result<Unit>>
fun saveUserCollection(
placeId: String,
placeName: String,
placeImgUrl: String,
placeLatLng: String
): Flow<Result<Unit>>

fun deleteUserCollection(content: CollectionModel): Flow<Result<Unit>>
fun deleteUserCollection(content: CollectionDataModel): Flow<Result<Unit>>
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.example.data.repository

import com.example.common.IODispatcher
import com.example.data.mapper.entityToModelMapper
import com.example.data.mapper.modelToEntityMapper
import com.example.data.mapper.dataModelToEntityMapper
import com.example.data.mapper.entityToDataModelMapper
import com.example.data.model.CollectionDataModel
import com.example.database.dao.EatDao
import com.example.model.domain.CollectionModel
import com.example.database.model.CollectionEntity
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
Expand All @@ -19,31 +20,45 @@ class DatabaseRepositoryImpl @Inject constructor(
private val eatDao: EatDao,
@IODispatcher private val ioDispatcher: CoroutineDispatcher
): DatabaseRepository {
override fun readAllCollectionEntities(): Flow<List<CollectionModel>> = eatDao
override fun readAllCollectionEntities(): Flow<List<CollectionDataModel>> = eatDao
.readAllCollectionEntities()
.map { entities ->
entities.map { entity -> entityToModelMapper(entity) }
}
.map { it.map { entity -> entityToDataModelMapper(entity) } }
.catch { exception ->
when (exception) {
is IOException -> emit(listOf())
is SQLDataException -> emit(listOf())
is ClassNotFoundException -> emit(listOf())
else -> emit(listOf())
is IOException -> throw IOException()
is SQLDataException -> throw SQLDataException()
is ClassNotFoundException -> throw ClassNotFoundException()
else -> throw exception
}
}.flowOn(ioDispatcher)

override fun readCollectionEntity(
placeID: String
): Flow<CollectionModel> = eatDao
.readCollectionEntity(placeID)
.map { entity -> entityToModelMapper(entity) }
.flowOn(ioDispatcher)
override fun readCollectionEntity(placeId: String): Flow<CollectionDataModel> = eatDao
.readCollectionEntity(placeId)
.map { entityToDataModelMapper(it) }
.catch { exception ->
when (exception) {
is IOException -> throw IOException()
is SQLDataException -> throw SQLDataException()
is ClassNotFoundException -> throw ClassNotFoundException()
else -> throw exception
}
}.flowOn(ioDispatcher)

override fun saveUserCollection(
content: CollectionModel
placeId: String,
placeName: String,
placeImgUrl: String,
placeLatLng: String
): Flow<Result<Unit>> = flow {
eatDao.saveUserCollection(modelToEntityMapper(content))
val entity = CollectionEntity(
placeID = placeId,
placeName = placeName,
placeImgUrl = placeImgUrl,
placeLatLng = placeLatLng
)


eatDao.saveUserCollection(entity)
emit(Result.success(Unit))
}.catch { exception ->
when (exception) {
Expand All @@ -54,10 +69,10 @@ class DatabaseRepositoryImpl @Inject constructor(
}
}.flowOn(ioDispatcher)

override fun deleteUserCollection(
content: CollectionModel
): Flow<Result<Unit>> = flow {
eatDao.deleteUserCollection(modelToEntityMapper(content))
override fun deleteUserCollection(content: CollectionDataModel): Flow<Result<Unit>> = flow {
val entity = dataModelToEntityMapper(content)

eatDao.deleteUserCollection(entity)
emit(Result.success(Unit))
}.catch { exception ->
when (exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.data

import com.example.data.model.CollectionDataModel
import com.example.data.repository.DatabaseRepository
import com.example.model.domain.CollectionModel
import io.mockk.clearMocks
import io.mockk.every
import io.mockk.mockk
Expand Down Expand Up @@ -31,7 +31,7 @@ class DataBaseRepoUnitTest {

@Test
fun `execute should return readAllCollectionEntities`() = runTest {
var result = listOf<CollectionModel>()
var result = listOf<CollectionDataModel>()

databaseRepositoryImpl
.readAllCollectionEntities()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package com.example.data

import com.example.model.domain.CollectionModel
import com.example.data.model.CollectionDataModel
import com.example.model.network.CurrentOpeningHours
import com.example.model.network.Geometry
import com.example.model.network.OpeningHours
import com.example.model.network.detailPlace.DetailedResult

internal object DataLayerDummyData {
val DUMMY_COLLECTION_DATA = listOf(
CollectionModel(
CollectionDataModel(
id = "1",
name = "default",
latLng = "default",
imgUrl = "default"
),
CollectionModel(
CollectionDataModel(
id = "2",
name = "default",
latLng = "default",
imgUrl = "default"
),
CollectionModel(
CollectionDataModel(
id = "3",
name = "default",
latLng = "default",
Expand Down
14 changes: 7 additions & 7 deletions core/data/src/test/java/com/example/data/MapperUnitTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.data

import com.example.data.mapper.entityToModelMapper
import com.example.data.mapper.modelToEntityMapper
import com.example.data.mapper.entityToDataModelMapper
import com.example.data.mapper.dataModelToEntityMapper
import com.example.data.model.CollectionDataModel
import com.example.database.model.CollectionEntity
import com.example.model.domain.CollectionModel
import org.junit.Test
import org.junit.Assert.assertEquals

Expand All @@ -17,8 +17,8 @@ class MapperUnitTest {
placeLatLng = "dummy",
placeImgUrl = "dummy"
)
private val collectionModel: CollectionModel =
CollectionModel(
private val collectionModel: CollectionDataModel =
CollectionDataModel(
id = "dummy",
name = "dummy",
latLng = "dummy",
Expand All @@ -28,13 +28,13 @@ class MapperUnitTest {

@Test
fun mapping_ENTITY_TO_MODEL() {
val mappingResult = entityToModelMapper(collectionEntity)
val mappingResult = entityToDataModelMapper(collectionEntity)
assertEquals(collectionModel, mappingResult)
}

@Test
fun mapping_MODEL_TO_ENTITY() {
val mappingResult = modelToEntityMapper(collectionModel)
val mappingResult = dataModelToEntityMapper(collectionModel)
assertEquals(collectionEntity, mappingResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ interface EatDao {
@Query("SELECT * FROM collection_entity")
fun readAllCollectionEntities(): Flow<List<CollectionEntity>>

@Query("SELECT * FROM Collection_Entity WHERE placeID = (:placeID)")
fun readCollectionEntity(placeID: String): Flow<CollectionEntity>
@Query("SELECT * FROM Collection_Entity WHERE placeID = (:placeId)")
fun readCollectionEntity(placeId: String): Flow<CollectionEntity>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveUserCollection(content: CollectionEntity)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.domain.model

data class CollectionDomainModel(
val id: String,
val name: String,
val latLng: String,
val imgUrl: String = ""
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.domain.model

data class NearByPlaceItemModel(
val placeId: String,
val placeName: String,
val placePhotoReference: String = ""
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.model.domain
package com.example.domain.model

data class DetailedModel(
data class PlaceDetailItemModel(
val placeId: String,
val placeName: String,
val placeRating: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ package com.example.domain.network

import com.example.data.repository.PlaceApiRepository
import com.example.domain.BuildConfig
import com.example.model.domain.GridItemsModel
import com.example.domain.model.NearByPlaceItemModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class GetGridItemUseCase @Inject constructor(
private val network: PlaceApiRepository
) {
operator fun invoke(latLng: String): Flow<List<GridItemsModel>> = network
class GetGridItemUseCase @Inject constructor(private val network: PlaceApiRepository) {
operator fun invoke(latLng: String): Flow<List<NearByPlaceItemModel>> = network
.nearByPlace(latLng)
.map { list -> list.slice(list.lastIndex/3 .. list.lastIndex) }
.map { result ->
result.map {
GridItemsModel(
placeID = it.placeId ?: "",
name = it.name ?: "",
photoRef = if (it.photos.isNotEmpty()) {
NearByPlaceItemModel(
placeId = it.placeId ?: "",
placeName = it.name ?: "",
placePhotoReference = if (it.photos.isNotEmpty()) {
it.photos[0].getFullPhotoReference(BuildConfig.PLACE_API_KEY)
} else {
""
Expand Down
Loading

0 comments on commit c6c7848

Please sign in to comment.