diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/BahnStation.kt b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/BahnStation.kt index adac075..a1b48de 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/BahnStation.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/BahnStation.kt @@ -1,5 +1,7 @@ package de.twomartens.timetable.bahnApi.model.db +import de.twomartens.timetable.model.Eva +import de.twomartens.timetable.model.NonEmptyString import org.bson.types.ObjectId import org.springframework.data.annotation.CreatedDate import org.springframework.data.annotation.Id @@ -11,8 +13,8 @@ import java.time.Instant @Document data class BahnStation( - @TextIndexed var name: String, - @Indexed(unique = true) var eva: Int?, + @TextIndexed var name: NonEmptyString, + @Indexed(unique = true) var eva: Eva, var ds100: String, var db: Boolean ) { diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/BahnTimetable.kt b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/BahnTimetable.kt index 2923360..41ce5b8 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/BahnTimetable.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/BahnTimetable.kt @@ -1,6 +1,8 @@ package de.twomartens.timetable.bahnApi.model.db import de.twomartens.timetable.bahnApi.model.dto.BahnStationStop +import de.twomartens.timetable.model.Eva +import de.twomartens.timetable.model.NonEmptyString import org.bson.types.ObjectId import org.springframework.data.annotation.CreatedDate import org.springframework.data.annotation.Id @@ -14,9 +16,9 @@ import java.time.LocalDateTime @Document @CompoundIndex(def = "{'eva': 1, 'dateTime': 1}", unique = true) data class BahnTimetable( - var eva: Int, + var eva: Eva, var dateTime: LocalDateTime, - @TextIndexed var station: String, + @TextIndexed var station: NonEmptyString, var stops: List ) { @Id diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/dto/BahnStation.kt b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/dto/BahnStation.kt index d3862dc..afc0027 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/dto/BahnStation.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/dto/BahnStation.kt @@ -1,18 +1,19 @@ package de.twomartens.timetable.bahnApi.model.dto +import de.twomartens.timetable.model.Eva import jakarta.xml.bind.annotation.XmlAccessType import jakarta.xml.bind.annotation.XmlAccessorType import jakarta.xml.bind.annotation.XmlAttribute @XmlAccessorType(XmlAccessType.FIELD) data class BahnStation( - @field:XmlAttribute + @field:XmlAttribute var name: String, - @field:XmlAttribute - var eva: Int?, - @field:XmlAttribute + @field:XmlAttribute + var eva: Eva?, + @field:XmlAttribute var ds100: String, - @field:XmlAttribute + @field:XmlAttribute var db: Boolean ) { constructor(): this("", null, "", true) diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/dto/BahnStationStop.kt b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/dto/BahnStationStop.kt index 912cf2b..d36337b 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/dto/BahnStationStop.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/dto/BahnStationStop.kt @@ -1,5 +1,6 @@ package de.twomartens.timetable.bahnApi.model.dto +import de.twomartens.timetable.model.Eva import jakarta.xml.bind.annotation.XmlAccessType import jakarta.xml.bind.annotation.XmlAccessorType import jakarta.xml.bind.annotation.XmlAttribute @@ -7,7 +8,7 @@ import jakarta.xml.bind.annotation.XmlElement @XmlAccessorType(XmlAccessType.FIELD) data class BahnStationStop( - @field:XmlAttribute var eva: Int?, + @field:XmlAttribute var eva: Eva?, @field:XmlAttribute var id: String, @field:XmlElement(name = "tl") var tripLabel: BahnTripLabel?, @field:XmlElement(name = "ar") var arrival: BahnStopEvent?, diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/dto/BahnTimetable.kt b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/dto/BahnTimetable.kt index 45645a2..45cc8c8 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/dto/BahnTimetable.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/model/dto/BahnTimetable.kt @@ -1,11 +1,12 @@ package de.twomartens.timetable.bahnApi.model.dto +import de.twomartens.timetable.model.Eva import jakarta.xml.bind.annotation.* @XmlRootElement(name = "timetable") @XmlAccessorType(XmlAccessType.FIELD) data class BahnTimetable( - @field:XmlAttribute var eva: Int?, + @field:XmlAttribute var eva: Eva?, @field:XmlAttribute var station: String, @field:XmlElement(name = "s") var stops: List ) { diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/BahnStationRepository.kt b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/BahnStationRepository.kt index ed1285d..25bf5ea 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/BahnStationRepository.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/BahnStationRepository.kt @@ -3,4 +3,5 @@ package de.twomartens.timetable.bahnApi.repository import de.twomartens.timetable.bahnApi.model.db.BahnStation import org.springframework.data.mongodb.repository.MongoRepository +// TODO: figure out what ID type should be interface BahnStationRepository : MongoRepository diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/BahnTimetableRepository.kt b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/BahnTimetableRepository.kt index 3bbb55c..c6f89fd 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/BahnTimetableRepository.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/BahnTimetableRepository.kt @@ -1,7 +1,7 @@ package de.twomartens.timetable.bahnApi.repository -import de.twomartens.timetable.bahnApi.model.db.BahnStation import de.twomartens.timetable.bahnApi.model.db.BahnTimetable import org.springframework.data.mongodb.repository.MongoRepository -interface BahnTimetableRepository : MongoRepository +// TODO: figure out what ID type should be +interface BahnTimetableRepository : MongoRepository \ No newline at end of file diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/ScheduledFetchTaskRepository.kt b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/ScheduledFetchTaskRepository.kt index 952d092..85dee16 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/ScheduledFetchTaskRepository.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/bahnApi/repository/ScheduledFetchTaskRepository.kt @@ -3,4 +3,5 @@ package de.twomartens.timetable.bahnApi.repository import de.twomartens.timetable.bahnApi.model.db.ScheduledFetchTask import org.springframework.data.mongodb.repository.MongoRepository +// TODO: what should be ID type be? interface ScheduledFetchTaskRepository : MongoRepository diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/controller/TimetableController.kt b/module-server/src/main/kotlin/de/twomartens/timetable/controller/TimetableController.kt new file mode 100644 index 0000000..935bfc3 --- /dev/null +++ b/module-server/src/main/kotlin/de/twomartens/timetable/controller/TimetableController.kt @@ -0,0 +1,55 @@ +package de.twomartens.timetable.controller + +import de.twomartens.timetable.model.NonEmptyString +import de.twomartens.timetable.repository.TswRouteRepository +import de.twomartens.timetable.service.ScheduledTaskService +import io.swagger.v3.oas.annotations.Operation +import io.swagger.v3.oas.annotations.Parameter +import io.swagger.v3.oas.annotations.responses.ApiResponse +import io.swagger.v3.oas.annotations.tags.Tag +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController +import org.springframework.web.server.ResponseStatusException +import java.time.Clock +import java.time.LocalDate + +@RestController +@RequestMapping(value = ["/timetable/v1"]) +@Tag(name = "Timetables", description = "all requests relating to timetables") +class TimetableController( + private val clock: Clock, + private val routeRepository: TswRouteRepository, + private val scheduledTaskService: ScheduledTaskService +) { + @Operation( + summary = "Task the backend with fetching a timetable for specified route and date", + responses = [ApiResponse( + responseCode = "200", + description = "Timetable will be fetched as requested" + ), ApiResponse( + responseCode = "400", + description = "Request does not follow specification" + )] + ) + @PostMapping("/{routeName}/fetchTimetable/{date}") + fun scheduleFetch( + @PathVariable @Parameter(description = "The name of the TSW route.", + example = "CologneAachen", + required = true) routeName: NonEmptyString, + @PathVariable @Parameter( + description = "The date to fetch timetable for. Must be in the future.", + example = "2023-09-12", + required = true) date: LocalDate + ) { + if (!date.isAfter(LocalDate.now(clock))) { + throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Date must be in the future") + } + val route = routeRepository.findByName(routeName) + ?: throw ResponseStatusException(HttpStatus.BAD_REQUEST, + "Route name must belong to existing route") + scheduledTaskService.scheduleTimetableFetch(route, date) + } +} \ No newline at end of file diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/model/NonEmptyString.kt b/module-server/src/main/kotlin/de/twomartens/timetable/model/NonEmptyString.kt new file mode 100644 index 0000000..f30dfc5 --- /dev/null +++ b/module-server/src/main/kotlin/de/twomartens/timetable/model/NonEmptyString.kt @@ -0,0 +1,10 @@ +package de.twomartens.timetable.model + +@JvmInline +value class NonEmptyString(val name: String) { + init { + require(name.isNotBlank()) { + "Name must not be blank" + } + } +} \ No newline at end of file diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/model/db/TswRoute.kt b/module-server/src/main/kotlin/de/twomartens/timetable/model/db/TswRoute.kt index b70609c..cd20405 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/model/db/TswRoute.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/model/db/TswRoute.kt @@ -1,6 +1,7 @@ package de.twomartens.timetable.model.db import de.twomartens.timetable.model.Eva +import de.twomartens.timetable.model.NonEmptyString import org.bson.types.ObjectId import org.springframework.data.annotation.CreatedDate import org.springframework.data.annotation.Id @@ -11,7 +12,7 @@ import java.time.Instant @Document data class TswRoute( - @Indexed(unique = true) var name: String, + @Indexed(unique = true) var name: NonEmptyString, /** * Ordered list of stations that represent the order of stations on the route. * diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/repository/TswRouteRepository.kt b/module-server/src/main/kotlin/de/twomartens/timetable/repository/TswRouteRepository.kt new file mode 100644 index 0000000..ddc0ccf --- /dev/null +++ b/module-server/src/main/kotlin/de/twomartens/timetable/repository/TswRouteRepository.kt @@ -0,0 +1,10 @@ +package de.twomartens.timetable.repository + +import de.twomartens.timetable.model.NonEmptyString +import de.twomartens.timetable.model.db.TswRoute +import org.bson.types.ObjectId +import org.springframework.data.mongodb.repository.MongoRepository + +interface TswRouteRepository : MongoRepository { + fun findByName(name: NonEmptyString): TswRoute? +}