Skip to content

Commit f3622e1

Browse files
chore: add serializer for api
1 parent b76f118 commit f3622e1

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2023. Smart Operating Block
3+
*
4+
* Use of this source code is governed by an MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
package application.presenter.api.serializer
10+
11+
import application.presenter.api.model.EnvironmentalDataApiDto
12+
import application.presenter.api.model.RoomApiDto
13+
import application.presenter.api.model.RoomApiDtoType
14+
import application.presenter.api.model.ValueWithUnit
15+
import entity.zone.Room
16+
import entity.zone.RoomEnvironmentalData
17+
import entity.zone.RoomType
18+
19+
/**
20+
* Serializer for data to return in API.
21+
*/
22+
object ApiSerializer {
23+
/**
24+
* Extension method to convert [Room] API DTO to [application.presenter.api.model.RoomApiDto] class.
25+
*/
26+
fun Room.toRoomApiDto(): RoomApiDto = RoomApiDto(
27+
id = this.id.value,
28+
name = this.name.orEmpty(),
29+
zoneId = this.zoneId.value,
30+
type = this.type.toRoomApiDtoType(),
31+
environmentalData = this.environmentalData.toEnvironmentDataApiDto()
32+
)
33+
34+
private fun RoomEnvironmentalData.toEnvironmentDataApiDto() = EnvironmentalDataApiDto(
35+
temperature = this.temperature?.let { ValueWithUnit(it.value, it.unit.toString()) },
36+
humidity = this.humidity?.percentage,
37+
luminosity = this.luminosity?.let { ValueWithUnit(it.value, it.unit.toString()) },
38+
presence = this.presence?.presenceDetected
39+
)
40+
41+
private fun RoomType.toRoomApiDtoType() = when (this) {
42+
RoomType.OPERATING_ROOM -> RoomApiDtoType.OPERATING_ROOM
43+
RoomType.PRE_OPERATING_ROOM -> RoomApiDtoType.PRE_OPERATING_ROOM
44+
}
45+
}

0 commit comments

Comments
 (0)