change: Allow overwriting BahnTimetable

This commit is contained in:
2024-01-18 09:27:50 +01:00
parent e75d54c547
commit 55275dd85d
3 changed files with 22 additions and 2 deletions
@@ -14,7 +14,7 @@ import java.time.Instant
import java.time.LocalDateTime
@Document
@CompoundIndex(def = "{'userId': 1, 'routeId': 1, 'eva': 1, 'fetchedDateTime': 1}", unique = true)
@CompoundIndex(def = "{'userId': 1, 'routeId': 1, 'eva': 1, 'fetchedDateTime': 1}")
data class ScheduledFetchTask(
var userId: UserId,
var routeId: RouteId,
@@ -4,9 +4,18 @@ import de.twomartens.timetable.bahnApi.model.Eva
import de.twomartens.timetable.bahnApi.model.db.BahnTimetable
import de.twomartens.timetable.types.HourAtDay
import org.bson.types.ObjectId
import org.springframework.data.domain.ExampleMatcher
import org.springframework.data.mongodb.repository.MongoRepository
// TODO: figure out what ID type should be
interface BahnTimetableRepository : MongoRepository<BahnTimetable, ObjectId> {
fun findByEvaAndHourAtDay(eva: Eva, hourAtDay: HourAtDay): BahnTimetable?
fun getExampleMatcher(): ExampleMatcher {
return ExampleMatcher.matching()
.withMatcher("userId", ExampleMatcher.GenericPropertyMatchers.exact())
.withMatcher("routeId", ExampleMatcher.GenericPropertyMatchers.exact())
.withMatcher("eva", ExampleMatcher.GenericPropertyMatchers.exact())
.withMatcher("hourAtDay", ExampleMatcher.GenericPropertyMatchers.exact())
}
}
@@ -12,6 +12,7 @@ import de.twomartens.timetable.model.db.Station
import de.twomartens.timetable.model.repository.StationRepository
import de.twomartens.timetable.types.HourAtDay
import org.mapstruct.factory.Mappers
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
@@ -91,7 +92,17 @@ open class BahnDatabaseService(
fun storeTimetable(timetable: BahnTimetable, userId: UserId, routeId: RouteId,
hourAtDay: HourAtDay) {
bahnTimetableRepository.save(bahnTimetableMapper.mapToDB(timetable, userId, routeId, hourAtDay))
val matcher = bahnTimetableRepository.getExampleMatcher()
val dbTimetable = bahnTimetableMapper.mapToDB(timetable, userId, routeId, hourAtDay)
val existingTimetable = bahnTimetableRepository.findOne(Example.of(dbTimetable, matcher))
existingTimetable
.map {
it.stops = dbTimetable.stops
it.station = dbTimetable.station
it
}
.orElse(dbTimetable)
bahnTimetableRepository.save(dbTimetable)
}
companion object {