-
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.
re #9
- Loading branch information
Tobias Scholze
committed
Apr 3, 2023
1 parent
5010df6
commit a309e02
Showing
8 changed files
with
92 additions
and
34 deletions.
There are no files selected for viewing
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
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
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
62 changes: 54 additions & 8 deletions
62
app/shared/src/commonMain/kotlin/io/github/tscholze/kennzeichner/data/Region.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,14 +1,60 @@ | ||
package io.github.tscholze.kennzeichner.data | ||
|
||
import kotlinx.serialization.Serializable | ||
import io.github.tscholze.kennzeichner.data.dto.RegionDTO | ||
|
||
@Serializable | ||
data class Region ( | ||
/** | ||
* Business model of a region. | ||
*/ | ||
class Region( | ||
val id: String, | ||
val name: String, | ||
val lat: String, | ||
val long: String, | ||
val inhabitants: String, | ||
val coordinate: Coordinate, | ||
val inhabitants: Int, | ||
val leader: String, | ||
val area: String | ||
) | ||
val area: Int | ||
) { | ||
companion object { | ||
/** | ||
* Creates a new Region object from DTO. | ||
* | ||
* @param dto: DTO to transfer from. | ||
* @return Created instance or null if transferring failed. | ||
*/ | ||
fun fromDto(dto: RegionDTO): Region? { | ||
return try { | ||
Region( | ||
dto.id, | ||
dto.name, | ||
Coordinate.fromDto(dto), | ||
Int.fromDtoString(dto.inhabitants), | ||
dto.leader, | ||
Int.fromDtoString(dto.area) | ||
) | ||
} catch (error: Exception) { | ||
null | ||
} | ||
} | ||
} | ||
} | ||
|
||
data class Coordinate( | ||
val latitude: Double, | ||
val longitude: Double | ||
) { | ||
companion object { | ||
@Throws(NumberFormatException::class) | ||
fun fromDto(dto: RegionDTO): Coordinate { | ||
return Coordinate( | ||
dto.lat.replace(",", ".").toDouble(), | ||
dto.long.replace(",", ".").toDouble() | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Throws(NumberFormatException::class) | ||
private fun Int.Companion.fromDtoString(value: String): Int { | ||
return value.replace(",", "") | ||
.replace(".", "") | ||
.toInt() | ||
} |
18 changes: 18 additions & 0 deletions
18
app/shared/src/commonMain/kotlin/io/github/tscholze/kennzeichner/data/dto/RegionDTO.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,18 @@ | ||
package io.github.tscholze.kennzeichner.data.dto | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
/** | ||
* Data transfer model of a remote | ||
* region. | ||
*/ | ||
@Serializable | ||
data class RegionDTO ( | ||
val id: String, | ||
val name: String, | ||
val lat: String, | ||
val long: String, | ||
val inhabitants: String, | ||
val leader: String, | ||
val area: String | ||
) |