Added controller to trigger timetable fetch

This commit is contained in:
2023-10-08 13:34:46 +02:00
parent e9b17707ab
commit 06ad9eaa31
12 changed files with 99 additions and 14 deletions
@@ -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
) {
@@ -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<BahnStationStop>
) {
@Id
@@ -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)
@@ -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?,
@@ -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<BahnStationStop>
) {
@@ -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<BahnStation, String>
@@ -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
// TODO: figure out what ID type should be
interface BahnTimetableRepository : MongoRepository<BahnTimetable, String>
@@ -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<ScheduledFetchTask, String>
@@ -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)
}
}
@@ -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"
}
}
}
@@ -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.
*
@@ -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<TswRoute, ObjectId> {
fun findByName(name: NonEmptyString): TswRoute?
}