change: Use ZonedDateTime for storage in DB

This commit is contained in:
2025-08-13 08:16:02 +02:00
parent c587289ab3
commit 076411258d
6 changed files with 84 additions and 11 deletions
@@ -7,15 +7,21 @@ import java.time.format.DateTimeParseException
import java.util.* import java.util.*
class LocalDateTimeAdapter : XmlAdapter<String?, LocalDateTime>() { class LocalDateTimeAdapter : XmlAdapter<String?, LocalDateTime?>() {
@Throws(DateTimeParseException::class) @Throws(DateTimeParseException::class)
override fun unmarshal(xmlValue: String?): LocalDateTime { override fun unmarshal(xmlValue: String?): LocalDateTime? {
if (xmlValue == null) {
return null
}
return LocalDateTime.parse(xmlValue, dateFormatter.get()) return LocalDateTime.parse(xmlValue, dateFormatter.get())
} }
@Throws(DateTimeParseException::class) @Throws(DateTimeParseException::class)
override fun marshal(kotlinValue: LocalDateTime): String? { override fun marshal(kotlinValue: LocalDateTime?): String? {
if (kotlinValue == null) {
return null
}
return dateFormatter.get().format(kotlinValue) return dateFormatter.get().format(kotlinValue)
} }
@@ -1,11 +1,15 @@
package de.twomartens.timetable.bahnApi.mapper package de.twomartens.timetable.bahnApi.mapper
import de.twomartens.timetable.bahnApi.model.db.BahnStationStop
import de.twomartens.timetable.bahnApi.model.db.BahnStopEvent
import de.twomartens.timetable.bahnApi.model.db.BahnTimetable import de.twomartens.timetable.bahnApi.model.db.BahnTimetable
import de.twomartens.timetable.model.common.RouteId import de.twomartens.timetable.model.common.RouteId
import de.twomartens.timetable.model.common.UserId import de.twomartens.timetable.model.common.UserId
import de.twomartens.timetable.types.HourAtDay import de.twomartens.timetable.types.HourAtDay
import de.twomartens.timetable.types.NonEmptyString import de.twomartens.timetable.types.NonEmptyString
import org.mapstruct.* import org.mapstruct.*
import java.time.Clock
import java.time.ZonedDateTime
@Mapper( @Mapper(
collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED, collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
@@ -18,14 +22,48 @@ interface BahnTimetableMapper {
@Mapping(target = "lastModified", ignore = true) @Mapping(target = "lastModified", ignore = true)
fun mapToDB(dto: de.twomartens.timetable.bahnApi.model.dto.BahnTimetable, fun mapToDB(dto: de.twomartens.timetable.bahnApi.model.dto.BahnTimetable,
userId: UserId, routeId: RouteId, userId: UserId, routeId: RouteId,
hourAtDay: HourAtDay): BahnTimetable { hourAtDay: HourAtDay,
clock: Clock): BahnTimetable {
return BahnTimetable( return BahnTimetable(
userId, userId,
routeId, routeId,
dto.eva, dto.eva,
hourAtDay, ZonedDateTime.of(hourAtDay.dateTime, clock.zone),
NonEmptyString(dto.station), NonEmptyString(dto.station),
dto.stops mapToDb(dto.stops, clock)
)
}
fun mapToDb(stops: List<de.twomartens.timetable.bahnApi.model.dto.BahnStationStop>, clock: Clock): List<BahnStationStop> {
return stops.map { mapToDB(it, clock) }
}
fun mapToDB(dto: de.twomartens.timetable.bahnApi.model.dto.BahnStationStop, clock: Clock): BahnStationStop {
return BahnStationStop(
dto.eva,
dto.id,
dto.tripLabel,
mapToDB(dto.arrival, clock),
mapToDB(dto.departure, clock)
)
}
fun mapToDB(dto: de.twomartens.timetable.bahnApi.model.dto.BahnStopEvent?, clock: Clock): BahnStopEvent? {
if (dto == null) {
return null
}
return BahnStopEvent(
dto.plannedDistantEndpoint,
dto.changedDistantEndpoint,
ZonedDateTime.of(dto.plannedTime, clock.zone),
ZonedDateTime.of(dto.changedTime, clock.zone),
dto.plannedPath,
dto.changedPath,
dto.plannedPlatform,
dto.changedPlatform,
dto.wings,
dto.line
) )
} }
} }
@@ -0,0 +1,12 @@
package de.twomartens.timetable.bahnApi.model.db
import de.twomartens.timetable.bahnApi.model.Eva
import de.twomartens.timetable.bahnApi.model.dto.BahnTripLabel
data class BahnStationStop(
var eva: Eva,
var id: String,
var tripLabel: BahnTripLabel,
var arrival: BahnStopEvent?,
var departure: BahnStopEvent?
)
@@ -0,0 +1,16 @@
package de.twomartens.timetable.bahnApi.model.db
import java.time.ZonedDateTime
data class BahnStopEvent(
var plannedDistantEndpoint: String,
var changedDistantEndpoint: String,
var plannedTime: ZonedDateTime?,
var changedTime: ZonedDateTime?,
var plannedPath: String,
var changedPath: String,
var plannedPlatform: String,
var changedPlatform: String,
var wings: String,
var line: String
)
@@ -1,10 +1,8 @@
package de.twomartens.timetable.bahnApi.model.db package de.twomartens.timetable.bahnApi.model.db
import de.twomartens.timetable.bahnApi.model.Eva import de.twomartens.timetable.bahnApi.model.Eva
import de.twomartens.timetable.bahnApi.model.dto.BahnStationStop
import de.twomartens.timetable.model.common.RouteId import de.twomartens.timetable.model.common.RouteId
import de.twomartens.timetable.model.common.UserId import de.twomartens.timetable.model.common.UserId
import de.twomartens.timetable.types.HourAtDay
import de.twomartens.timetable.types.NonEmptyString import de.twomartens.timetable.types.NonEmptyString
import org.bson.types.ObjectId import org.bson.types.ObjectId
import org.springframework.data.annotation.CreatedDate import org.springframework.data.annotation.CreatedDate
@@ -13,6 +11,7 @@ import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.mongodb.core.index.CompoundIndex import org.springframework.data.mongodb.core.index.CompoundIndex
import org.springframework.data.mongodb.core.mapping.Document import org.springframework.data.mongodb.core.mapping.Document
import java.time.Instant import java.time.Instant
import java.time.ZonedDateTime
@Document @Document
@CompoundIndex(def = "{'userId': 1, 'routeId': 1, 'eva': 1, 'hourAtDay': 1}", unique = true) @CompoundIndex(def = "{'userId': 1, 'routeId': 1, 'eva': 1, 'hourAtDay': 1}", unique = true)
@@ -20,7 +19,7 @@ data class BahnTimetable(
var userId: UserId, var userId: UserId,
var routeId: RouteId, var routeId: RouteId,
var eva: Eva, var eva: Eva,
var hourAtDay: HourAtDay, var hourAtDay: ZonedDateTime,
var station: NonEmptyString, var station: NonEmptyString,
var stops: List<BahnStationStop> var stops: List<BahnStationStop>
) { ) {
@@ -16,13 +16,15 @@ import org.springframework.data.domain.Example
import org.springframework.data.mongodb.core.BulkOperations import org.springframework.data.mongodb.core.BulkOperations
import org.springframework.data.mongodb.core.MongoTemplate import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.time.Clock
@Service @Service
open class BahnDatabaseService( open class BahnDatabaseService(
private val bahnStationRepository: BahnStationRepository, private val bahnStationRepository: BahnStationRepository,
private val stationRepository: StationRepository, private val stationRepository: StationRepository,
private val bahnTimetableRepository: BahnTimetableRepository, private val bahnTimetableRepository: BahnTimetableRepository,
private val mongoTemplate: MongoTemplate private val mongoTemplate: MongoTemplate,
private val clock: Clock
) { ) {
private val bahnStationMapper = Mappers.getMapper(BahnStationMapper::class.java) private val bahnStationMapper = Mappers.getMapper(BahnStationMapper::class.java)
private val bahnTimetableMapper = Mappers.getMapper(BahnTimetableMapper::class.java) private val bahnTimetableMapper = Mappers.getMapper(BahnTimetableMapper::class.java)
@@ -93,7 +95,7 @@ open class BahnDatabaseService(
fun storeTimetable(timetable: BahnTimetable, userId: UserId, routeId: RouteId, fun storeTimetable(timetable: BahnTimetable, userId: UserId, routeId: RouteId,
hourAtDay: HourAtDay) { hourAtDay: HourAtDay) {
val matcher = bahnTimetableRepository.getExampleMatcher() val matcher = bahnTimetableRepository.getExampleMatcher()
val dbTimetable = bahnTimetableMapper.mapToDB(timetable, userId, routeId, hourAtDay) val dbTimetable = bahnTimetableMapper.mapToDB(timetable, userId, routeId, hourAtDay, clock)
val existingTimetable = bahnTimetableRepository.findOne(Example.of(dbTimetable, matcher)) val existingTimetable = bahnTimetableRepository.findOne(Example.of(dbTimetable, matcher))
existingTimetable existingTimetable
.map { .map {