-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
106 additions
and
27 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/example/kocelainterview/data/local_data_source/ShipDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.example.kocelainterview.data.local_data_source | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
|
||
@Dao | ||
interface ShipDao { | ||
@Query("SELECT * FROM ships") | ||
fun getShips(): List<ShipEntity> | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
fun saveShips(ships: List<ShipEntity>) | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/example/kocelainterview/data/local_data_source/ShipDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.example.kocelainterview.data.local_data_source | ||
|
||
import androidx.room.Database | ||
import androidx.room.RoomDatabase | ||
|
||
@Database(entities = [ShipEntity::class], version = 1) | ||
abstract class ShipDatabase:RoomDatabase (){ | ||
abstract fun shipDao():ShipDao | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/example/kocelainterview/data/local_data_source/ShipEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.example.kocelainterview.data.local_data_source | ||
|
||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import com.example.kocelainterview.data.remote.dto.ShipDto | ||
|
||
@Entity(tableName = "ships") | ||
data class ShipEntity( | ||
@PrimaryKey val ship_id: String, | ||
val active: Boolean, | ||
val image: String?, | ||
val ship_name: String, | ||
val weight_kg: Int, | ||
val year_built: Int | ||
) | ||
|
||
|
||
fun ShipEntity.toShipDto(): ShipDto { | ||
return ShipDto( | ||
ship_id = ship_id, | ||
active = active, | ||
image = image, | ||
ship_name = ship_name, | ||
weight_kg = weight_kg, | ||
year_built = year_built | ||
|
||
) | ||
} | ||
|
||
|
||
|
||
|
44 changes: 29 additions & 15 deletions
44
app/src/main/java/com/example/kocelainterview/data/remote/dto/ShipDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 21 additions & 4 deletions
25
app/src/main/java/com/example/kocelainterview/data/repository/ShipRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,36 @@ | ||
package com.example.kocelainterview.data.repository | ||
|
||
import com.example.kocelainterview.data.local_data_source.ShipDao | ||
import com.example.kocelainterview.data.local_data_source.toShipDto | ||
import com.example.kocelainterview.data.remote.api_service.ShipsApi | ||
import com.example.kocelainterview.data.remote.dto.ShipDetailDto | ||
import com.example.kocelainterview.data.remote.dto.ShipDto | ||
import com.example.kocelainterview.data.remote.dto.toEntity | ||
import com.example.kocelainterview.domain.repository_interface.ShipRepository | ||
import javax.inject.Inject | ||
|
||
class ShipRepositoryImpl @Inject constructor( | ||
private val api:ShipsApi | ||
) :ShipRepository{ | ||
private val api: ShipsApi, | ||
private val shipDao: ShipDao | ||
) : ShipRepository { | ||
|
||
override suspend fun getShips(): List<ShipDto> { | ||
return api.getShips() | ||
// Check Room database first | ||
val cachedShips = shipDao.getShips() | ||
if (cachedShips.isNotEmpty()) { | ||
return cachedShips.map { it.toShipDto() } | ||
} | ||
|
||
// Fetch from API if not found in cache | ||
val ships = api.getShips() | ||
|
||
// Save to Room database | ||
shipDao.saveShips(ships.map { it.toEntity() }) | ||
|
||
return ships | ||
} | ||
|
||
override suspend fun getShipById(shipId: String): ShipDetailDto { | ||
return api.getShipById(shipId) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters