From 55275dd85dd6dd88c6f481144dc8ce2d67b1b325 Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Thu, 18 Jan 2024 09:27:50 +0100 Subject: [PATCH] change: Allow overwriting BahnTimetable --- .../bahnApi/model/db/ScheduledFetchTask.kt | 2 +- .../bahnApi/repository/BahnTimetableRepository.kt | 9 +++++++++ .../bahnApi/service/BahnDatabaseService.kt | 13 ++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/ScheduledFetchTask.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/ScheduledFetchTask.kt index fc0ea50..3bc1cfb 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/ScheduledFetchTask.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/ScheduledFetchTask.kt @@ -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, diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/BahnTimetableRepository.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/BahnTimetableRepository.kt index e0fa71a..3946b25 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/BahnTimetableRepository.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/BahnTimetableRepository.kt @@ -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 { 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()) + } } \ No newline at end of file diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnDatabaseService.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnDatabaseService.kt index e518a8f..9bbf18d 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnDatabaseService.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnDatabaseService.kt @@ -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 {