You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.7 KiB
104 lines
2.7 KiB
package net.aiterp.git.ykonsole2.application.routes
|
|
|
|
import com.fasterxml.jackson.annotation.JsonInclude
|
|
import net.aiterp.git.ykonsole2.domain.models.*
|
|
import net.aiterp.git.ykonsole2.domain.runtime.*
|
|
import java.time.Instant
|
|
|
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
|
data class ValueDTO(
|
|
val time: Int? = null,
|
|
val distance: Int? = null,
|
|
val calories: Int? = null,
|
|
val level: Int? = null,
|
|
val rpmSpeed: Int? = null,
|
|
val pulse: Int? = null,
|
|
) {
|
|
private val values by lazy {
|
|
buildList {
|
|
time?.let { add(Time(it)) }
|
|
distance?.let { add(Distance(it)) }
|
|
calories?.let { add(Calories(it)) }
|
|
level?.let { add(Level(it)) }
|
|
rpmSpeed?.let { add(RpmSpeed(it)) }
|
|
pulse?.let { add(Pulse(it)) }
|
|
}
|
|
}
|
|
|
|
fun toValues(): List<Value> = values
|
|
fun toValueOrNull() = values.firstOrNull()
|
|
|
|
companion object {
|
|
fun from(value: Value?) = from(listOfNotNull(value))
|
|
|
|
fun from(state: WorkoutState) = state.run { from(asValueList()) }
|
|
|
|
fun from(values: List<Value>) = ValueDTO(
|
|
time = values.find<Time>()?.seconds,
|
|
distance = values.find<Distance>()?.meters,
|
|
calories = values.find<Calories>()?.kcal,
|
|
level = values.find<Level>()?.raw,
|
|
rpmSpeed = values.find<RpmSpeed>()?.rpm,
|
|
pulse = values.find<Pulse>()?.bpm,
|
|
)
|
|
}
|
|
}
|
|
|
|
data class ProgramDTO(
|
|
val id: String,
|
|
val name: String,
|
|
val steps: List<ProgramStepDTO>,
|
|
) {
|
|
companion object {
|
|
fun from(program: Program) = ProgramDTO(
|
|
id = program.id,
|
|
name = program.name,
|
|
steps = program.steps.map { ProgramStepDTO(null, ValueDTO.from(it.values), ValueDTO.from(it.duration)) }
|
|
)
|
|
}
|
|
}
|
|
|
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
|
data class ProgramStepDTO(
|
|
val index: Int? = null,
|
|
val values: ValueDTO,
|
|
val duration: ValueDTO = ValueDTO(),
|
|
) {
|
|
fun toProgramStep() = ProgramStep(values.toValues(), duration.toValueOrNull())
|
|
}
|
|
|
|
data class WorkoutDTO(
|
|
val id: String,
|
|
val createdAt: Instant,
|
|
val device: Device?,
|
|
val program: ProgramDTO?,
|
|
val status: WorkoutStatus,
|
|
val message: String,
|
|
val test: Boolean,
|
|
) {
|
|
constructor(workout: Workout, device: Device?, program: Program?) : this(
|
|
id = workout.id,
|
|
createdAt = workout.createdAt,
|
|
device = device,
|
|
program = program?.let { ProgramDTO.from(it) },
|
|
status = workout.status,
|
|
message = workout.message,
|
|
test = workout.test,
|
|
)
|
|
}
|
|
|
|
data class MilestoneDTO(
|
|
val primary: ValueDTO,
|
|
val primaryKey: String,
|
|
val current: ValueDTO,
|
|
val diff: ValueDTO,
|
|
) {
|
|
companion object {
|
|
fun from(mr: MilestoneReached) = MilestoneDTO(
|
|
primary = ValueDTO.from(mr.keyValue),
|
|
primaryKey = mr.keyValue.name.lowercase(),
|
|
current = ValueDTO.from(mr.current),
|
|
diff = ValueDTO.from(mr.diff),
|
|
)
|
|
}
|
|
}
|