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.
80 lines
3.0 KiB
80 lines
3.0 KiB
package net.aiterp.git.ykonsole2.infrastructure.repositories
|
|
|
|
import net.aiterp.git.ykonsole2.domain.models.WorkoutState
|
|
import net.aiterp.git.ykonsole2.domain.models.WorkoutStateRepository
|
|
import net.aiterp.git.ykonsole2.domain.runtime.*
|
|
import net.aiterp.git.ykonsole2.infrastructure.getIntOrNull
|
|
import net.aiterp.git.ykonsole2.infrastructure.prepare
|
|
import net.aiterp.git.ykonsole2.infrastructure.runQuery
|
|
import net.aiterp.git.ykonsole2.infrastructure.withConnection
|
|
import java.sql.Types
|
|
import javax.sql.DataSource
|
|
|
|
val DataSource.workoutStateRepo get() = object : WorkoutStateRepository {
|
|
override fun fetchByWorkoutId(workoutId: String) = withConnection {
|
|
prepare("SELECT * FROM workout_state WHERE workout_id = ?") {
|
|
setString(1, workoutId)
|
|
|
|
runQuery {
|
|
buildList {
|
|
while (next()) add(
|
|
WorkoutState(
|
|
workoutId = getString("workout_id"),
|
|
time = Time(getInt("ws_seconds")),
|
|
calories = getIntOrNull("ws_kcal")?.let { Calories(it) },
|
|
level = getIntOrNull("ws_level")?.let { Level(it) },
|
|
distance = getIntOrNull("ws_meters")?.let { Distance(it) },
|
|
rpmSpeed = getIntOrNull("ws_rpm")?.let { RpmSpeed(it) },
|
|
pulse = getIntOrNull("ws_pulse")?.let { Pulse(it) },
|
|
),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun save(state: WorkoutState) {
|
|
withConnection {
|
|
prepare(
|
|
"""
|
|
INSERT INTO workout_state (workout_id, ws_seconds, ws_kcal, ws_level, ws_meters, ws_rpm, ws_pulse)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
ON DUPLICATE KEY UPDATE ws_kcal = VALUES(ws_kcal),
|
|
ws_level = VALUES(ws_level),
|
|
ws_meters = VALUES(ws_meters),
|
|
ws_rpm = VALUES(ws_rpm),
|
|
ws_pulse = VALUES(ws_pulse)
|
|
""".trimIndent()
|
|
) {
|
|
setString(1, state.workoutId)
|
|
setInt(2, state.time.seconds)
|
|
if (state.calories != null) setInt(3, state.calories!!.kcal) else setNull(3, Types.INTEGER)
|
|
if (state.level != null) setInt(4, state.level!!.raw) else setNull(4, Types.INTEGER)
|
|
if (state.distance != null) setInt(5, state.distance!!.meters) else setNull(5, Types.INTEGER)
|
|
if (state.rpmSpeed != null) setInt(6, state.rpmSpeed!!.rpm) else setNull(6, Types.INTEGER)
|
|
if (state.pulse != null) setInt(7, state.pulse!!.bpm) else setNull(7, Types.INTEGER)
|
|
execute()
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun deleteAll(states: Collection<WorkoutState>) {
|
|
withConnection {
|
|
prepare("DELETE FROM workout_state WHERE workout_id = ? AND ws_seconds = ?") {
|
|
states.forEachIndexed { index, state ->
|
|
setString(1, state.workoutId)
|
|
setInt(2, state.time.seconds)
|
|
addBatch()
|
|
|
|
if ((index + 1) % 100 == 0) {
|
|
executeBatch()
|
|
}
|
|
}
|
|
|
|
if (states.size % 100 != 0) {
|
|
executeBatch()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|