mirror of
https://github.com/2martens/tsw-timetable.git
synced 2026-10-17 16:06:51 +02:00
change: Use ZonedDateTime for storage in DB
This commit is contained in:
+9
-3
@@ -7,15 +7,21 @@ import java.time.format.DateTimeParseException
|
||||
import java.util.*
|
||||
|
||||
|
||||
class LocalDateTimeAdapter : XmlAdapter<String?, LocalDateTime>() {
|
||||
class LocalDateTimeAdapter : XmlAdapter<String?, LocalDateTime?>() {
|
||||
|
||||
@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())
|
||||
}
|
||||
|
||||
@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)
|
||||
}
|
||||
|
||||
|
||||
+41
-3
@@ -1,11 +1,15 @@
|
||||
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.model.common.RouteId
|
||||
import de.twomartens.timetable.model.common.UserId
|
||||
import de.twomartens.timetable.types.HourAtDay
|
||||
import de.twomartens.timetable.types.NonEmptyString
|
||||
import org.mapstruct.*
|
||||
import java.time.Clock
|
||||
import java.time.ZonedDateTime
|
||||
|
||||
@Mapper(
|
||||
collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
|
||||
@@ -18,14 +22,48 @@ interface BahnTimetableMapper {
|
||||
@Mapping(target = "lastModified", ignore = true)
|
||||
fun mapToDB(dto: de.twomartens.timetable.bahnApi.model.dto.BahnTimetable,
|
||||
userId: UserId, routeId: RouteId,
|
||||
hourAtDay: HourAtDay): BahnTimetable {
|
||||
hourAtDay: HourAtDay,
|
||||
clock: Clock): BahnTimetable {
|
||||
return BahnTimetable(
|
||||
userId,
|
||||
routeId,
|
||||
dto.eva,
|
||||
hourAtDay,
|
||||
ZonedDateTime.of(hourAtDay.dateTime, clock.zone),
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
+12
@@ -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?
|
||||
)
|
||||
+16
@@ -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
|
||||
)
|
||||
+2
-3
@@ -1,10 +1,8 @@
|
||||
package de.twomartens.timetable.bahnApi.model.db
|
||||
|
||||
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.UserId
|
||||
import de.twomartens.timetable.types.HourAtDay
|
||||
import de.twomartens.timetable.types.NonEmptyString
|
||||
import org.bson.types.ObjectId
|
||||
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.mapping.Document
|
||||
import java.time.Instant
|
||||
import java.time.ZonedDateTime
|
||||
|
||||
@Document
|
||||
@CompoundIndex(def = "{'userId': 1, 'routeId': 1, 'eva': 1, 'hourAtDay': 1}", unique = true)
|
||||
@@ -20,7 +19,7 @@ data class BahnTimetable(
|
||||
var userId: UserId,
|
||||
var routeId: RouteId,
|
||||
var eva: Eva,
|
||||
var hourAtDay: HourAtDay,
|
||||
var hourAtDay: ZonedDateTime,
|
||||
var station: NonEmptyString,
|
||||
var stops: List<BahnStationStop>
|
||||
) {
|
||||
|
||||
+4
-2
@@ -16,13 +16,15 @@ import org.springframework.data.domain.Example
|
||||
import org.springframework.data.mongodb.core.BulkOperations
|
||||
import org.springframework.data.mongodb.core.MongoTemplate
|
||||
import org.springframework.stereotype.Service
|
||||
import java.time.Clock
|
||||
|
||||
@Service
|
||||
open class BahnDatabaseService(
|
||||
private val bahnStationRepository: BahnStationRepository,
|
||||
private val stationRepository: StationRepository,
|
||||
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 bahnTimetableMapper = Mappers.getMapper(BahnTimetableMapper::class.java)
|
||||
@@ -93,7 +95,7 @@ open class BahnDatabaseService(
|
||||
fun storeTimetable(timetable: BahnTimetable, userId: UserId, routeId: RouteId,
|
||||
hourAtDay: HourAtDay) {
|
||||
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))
|
||||
existingTimetable
|
||||
.map {
|
||||
|
||||
Reference in New Issue
Block a user