diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/timetable/TimetableController.kt b/module-server/src/main/kotlin/de/twomartens/timetable/timetable/TimetableController.kt index b07f99a..c12a903 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/timetable/TimetableController.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/timetable/TimetableController.kt @@ -130,6 +130,10 @@ class TimetableController( content = [Content( schema = Schema(implementation = Timetable::class) )] + ), ApiResponse( + responseCode = "400", + description = "Provided request is invalid", + content = [Content(mediaType = "text/plain")] ), ApiResponse( responseCode = "403", description = "Access forbidden for user", @@ -168,6 +172,16 @@ class TimetableController( created = true timetable = mapper.mapToDB(userIdConverted, body) timetable.timetableState = TimetableState.PROCESSING + + if (!timetable.fetchDate.isAfter(LocalDate.now(clock))) { + throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Date of timetable must be in the future") + } + val route = routeRepository.findByUserIdAndName(userIdConverted, NonEmptyString(timetable.routeName)) + ?: throw ResponseStatusException( + HttpStatus.BAD_REQUEST, + "Route name must belong to existing route" + ) + scheduledTaskService.triggerTimetableFetch(route, timetable.fetchDate) } else { timetable.name = NonEmptyString(body.name) } @@ -222,38 +236,4 @@ class TimetableController( return ResponseEntity.status(HttpStatus.NO_CONTENT).build() } - - @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" - )] - ) - @SecurityRequirement(name = "bearer") - @SecurityRequirement(name = "oauth2") - @PostMapping("/{userId}/{routeName}/fetchTimetable/{date}") - fun scheduleFetch( - @PathVariable @Parameter(description = "The id of the user", - example = "1", - required = true) userId: UserId, - @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.findByUserIdAndName(userId, routeName) - ?: throw ResponseStatusException(HttpStatus.BAD_REQUEST, - "Route name must belong to existing route") - scheduledTaskService.triggerTimetableFetch(route, date) - } } \ No newline at end of file