package de.twomartens.oparlservice.control; import de.twomartens.oparlservice.entity.dto.System; import de.twomartens.oparlservice.entity.dto.*; import de.twomartens.oparlservice.service.OParlService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import org.springframework.web.server.ResponseStatusException; @Slf4j @RestController @RequestMapping(path = "/v1.1") public class OParlController { private final OParlService service; OParlController(OParlService service) { this.service = service; } @GetMapping("/agendaItem/{id}") @Operation(summary = "information about agenda item", description = "returns the requested agenda item", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Body.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public AgendaItem agendaItem( @PathVariable @Parameter(description = "agendaItem ID", example = "0") String id) { log.info("invoked method /v1.1/agendaItem/{}", id); return service.getAgendaItem(id).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Kein Tagesordnungspunkt mit angefragter ID existiert"); }); } @GetMapping(value = {"/body/{id}/agendaItems", "/body/{id}/agendaItems/{pageNumber}"}) @Operation(summary = "List of all agenda items in body", description = "returns a list of all agenda items in requested body", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ObjectList.ObjectListAgendaItem.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public ObjectList agendaItemsInBody( @PathVariable @Parameter(description = "body ID", example = "0") String id, @PathVariable(required = false) @Parameter(description = "page number", example = "1") Integer pageNumber) { log.info("invoked method /v1.1/body/{}/agendaItems/{}", id, pageNumber); if (pageNumber == null) { pageNumber = 1; } return service.getAgendaItemsInBody(id, pageNumber).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Körperschaft mit angefragter ID existiert"); }); } @GetMapping("/bodies") @Operation(summary = "List of available bodies", description = "returns a list of available bodies in this OParl system", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ObjectList.ObjectListBody.class))) }) public ObjectList bodies() { log.info("method invoked /v1.1/bodies"); return this.service.getBodies(); } @GetMapping("/body/{id}") @Operation(summary = "information about body", description = "returns information about the requested body", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Body.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public Body body( @PathVariable @Parameter(description = "body ID", example = "0") String id) { log.info("method invoked /v1.1/body/{}", id); return service.getBody(id).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Körperschaft mit angefragter ID existiert nicht"); }); } @GetMapping("/consultation/{id}") @Operation(summary = "information about consultation", description = "returns the requested consultation", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Consultation.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public Consultation consultation( @PathVariable @Parameter(description = "consultation ID", example = "0") String id) { log.info("invoked method /v1.1/consultation/{}", id); return service.getConsultation(id).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Beratung mit angefragter ID existiert"); }); } @GetMapping(value = {"/body/{id}/consultations", "/body/{id}/consultations/{pageNumber}"}) @Operation(summary = "List of all consultations in body", description = "returns a list of all consultations in requested body", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ObjectList.ObjectListConsultation.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public ObjectList consultationsInBody( @PathVariable @Parameter(description = "body ID", example = "0") String id, @PathVariable(required = false) @Parameter(description = "page number", example = "1") Integer pageNumber) { log.info("invoked method /v1.1/body/{}/consultations", id); if (pageNumber == null) { pageNumber = 1; } return service.getConsultationsInBody(id, pageNumber).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Körperschaft mit angefragter ID existiert"); }); } @GetMapping("/file/{id}") @Operation(summary = "information about file", description = "returns the requested file", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = File.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public File file( @PathVariable @Parameter(description = "file ID", example = "0") String id) { log.info("invoked method /v1.1/file/{}", id); return service.getFile(id).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Datei mit angefragter ID existiert"); }); } @GetMapping(value = {"/body/{id}/files", "/body/{id}/files/{pageNumber}"}) @Operation(summary = "List of all files in body", description = "returns a list of all files in requested body", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ObjectList.ObjectListFile.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public ObjectList filesInBody( @PathVariable @Parameter(description = "body ID", example = "0") String id, @PathVariable(required = false) @Parameter(description = "page number", example = "1") Integer pageNumber) { log.info("invoked method /v1.1/body/{}/files", id); if (pageNumber == null) { pageNumber = 1; } return service.getFilesInBody(id, pageNumber).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Körperschaft mit angefragter ID existiert"); }); } @GetMapping("/legislativeTerm/{id}") @Operation(summary = "information about legislative term", description = "returns information about the requested legislative term", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = LegislativeTerm.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public LegislativeTerm legislativeTerm( @PathVariable @Parameter(description = "legislative term ID", example = "21") String id) { log.info("method invoked /v1.1/legislativeTerm/{}", id); return service.getLegislativeTerm(id).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Legislaturperiode mit angefragter ID existiert nicht"); }); } @GetMapping(value = {"/body/{id}/legislativeTerms", "/body/{id}/legislativeTerms/{pageNumber}"}) @Operation(summary = "List of all legislative terms of a body", description = "returns a list of all legislative terms of requested body", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ObjectList.ObjectListLegislativeTerm.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public ObjectList legislativeTermsOfBody( @PathVariable @Parameter(description = "body ID", example = "0") String id, @PathVariable(required = false) @Parameter(description = "page number", example = "1") Integer pageNumber) { log.info("invoked method /v1.1/body/{}/legislativeTerms", id); if (pageNumber == null) { pageNumber = 1; } return service.getLegislativeTermsOfBody(id, pageNumber).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Körperschaft mit angefragter ID existiert"); }); } @GetMapping("/location/{id}") @Operation(summary = "information about location", description = "returns the requested location", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Location.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public Location location( @PathVariable @Parameter(description = "location ID", example = "0") String id) { log.info("invoked method /v1.1/location/{}", id); return service.getLocation(id).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Kein Ort mit angefragter ID existiert"); }); } @GetMapping(value = {"/body/{id}/locations", "/body/{id}/locations/{pageNumber}"}) @Operation(summary = "List of all locations in body", description = "returns a list of all locations in requested body", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ObjectList.ObjectListLocation.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public ObjectList locationsInBody( @PathVariable @Parameter(description = "body ID", example = "0") String id, @PathVariable(required = false) @Parameter(description = "page number", example = "1") Integer pageNumber) { log.info("invoked method /v1.1/body/{}/locations", id); if (pageNumber == null) { pageNumber = 1; } return service.getLocationsInBody(id, pageNumber).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Körperschaft mit angefragter ID existiert"); }); } @GetMapping("/meeting/{id}") @Operation(summary = "information about meeting", description = "returns the requested meeting", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Meeting.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public Meeting meeting( @PathVariable @Parameter(description = "meeting ID", example = "0") String id) { log.info("invoked method /v1.1/meeting/{}", id); return service.getMeeting(id).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Sitzung mit angefragter ID existiert"); }); } @GetMapping(value = {"/body/{id}/meetings", "/body/{id}/meetings/{pageNumber}"}) @Operation(summary = "List of all meetings in body", description = "returns a list of all meetings in requested body", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ObjectList.ObjectListMeeting.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public ObjectList meetingsInBody( @PathVariable @Parameter(description = "body ID", example = "0") String id, @PathVariable(required = false) @Parameter(description = "page number", example = "1") Integer pageNumber) { log.info("invoked method /v1.1/body/{}/meetings", id); if (pageNumber == null) { pageNumber = 1; } return service.getMeetingsInBody(id, pageNumber).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Körperschaft mit angefragter ID existiert"); }); } @GetMapping(value = {"/organization/{id}/meetings", "/organization/{id}/meetings/{pageNumber}"}) @Operation(summary = "List of all meetings in organization", description = "returns a list of all meetings in requested organization", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ObjectList.ObjectListMeeting.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public ObjectList meetingsInOrganization( @PathVariable @Parameter(description = "organization ID", example = "0") String id, @PathVariable(required = false) @Parameter(description = "page number", example = "1") Integer pageNumber) { log.info("invoked method /v1.1/organization/{}/meetings", id); if (pageNumber == null) { pageNumber = 1; } return service.getMeetingsInOrganization(id, pageNumber).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Organisation mit angefragter ID existiert"); }); } @GetMapping("/membership/{id}") @Operation(summary = "information about membership", description = "returns the requested membership", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Membership.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public Membership membership( @PathVariable @Parameter(description = "membership ID", example = "0") String id) { log.info("invoked method /v1.1/membership/{}", id); return service.getMembership(id).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Mitgliedschaft mit angefragter ID existiert"); }); } @GetMapping(value = {"/body/{id}/memberships", "/body/{id}/memberships/{pageNumber}"}) @Operation(summary = "List of all memberships in body", description = "returns a list of all memberships in requested body", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ObjectList.ObjectListMembership.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public ObjectList membershipsInBody( @PathVariable @Parameter(description = "body ID", example = "0") String id, @PathVariable(required = false) @Parameter(description = "page number", example = "1") Integer pageNumber) { log.info("invoked method /v1.1/body/{}/memberships", id); if (pageNumber == null) { pageNumber = 1; } return service.getMembershipsInBody(id, pageNumber).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Körperschaft mit angefragter ID existiert"); }); } @GetMapping(value = {"/organization/{id}/memberships", "/organization/{id}/memberships/{pageNumber}"}) @Operation(summary = "List of all memberships in organization", description = "returns a list of all memberships in requested organization", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ObjectList.ObjectListMembership.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public ObjectList membershipsInOrganization( @PathVariable @Parameter(description = "organization ID", example = "0") String id, @PathVariable(required = false) @Parameter(description = "page number", example = "1") Integer pageNumber) { log.info("invoked method /v1.1/organization/{}/memberships", id); if (pageNumber == null) { pageNumber = 1; } return service.getMembershipsInOrganization(id, pageNumber).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Organisation mit angefragter ID existiert"); }); } @ExceptionHandler({ResponseStatusException.class}) @ResponseStatus(HttpStatus.NOT_FOUND) public ErrorObject notFound(ResponseStatusException exception) { return ErrorObject.builder() .message(exception.getReason()) .debug(exception.getLocalizedMessage()) .build(); } @GetMapping("/organization/{id}") @Operation(summary = "information about organization", description = "returns the requested organization", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Organization.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public Organization organization( @PathVariable @Parameter(description = "organization ID", example = "0") String id) { log.info("invoked method /v1.1/organization/{}", id); return service.getOrganization(id).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Organisation mit angefragter ID existiert"); }); } @GetMapping(value = {"/body/{id}/organizations", "/body/{id}/organizations/{pageNumber}"}) @Operation(summary = "List of all organizations in body", description = "returns a list of all organizations in requested body", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ObjectList.ObjectListOrganization.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public ObjectList organizationsInBody( @PathVariable @Parameter(description = "body ID", example = "0") String id, @PathVariable(required = false) @Parameter(description = "page number", example = "1") Integer pageNumber) { log.info("invoked method /v1.1/body/{}/organizations", id); if (pageNumber == null) { pageNumber = 1; } return service.getOrganizationsInBody(id, pageNumber).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Körperschaft mit angefragter ID existiert"); }); } @GetMapping("/paper/{id}") @Operation(summary = "information about paper", description = "returns the requested paper", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Paper.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public Paper paper( @PathVariable @Parameter(description = "paper ID", example = "0") String id) { log.info("invoked method /v1.1/paper/{}", id); return service.getPaper(id).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Drucksache mit angefragter ID existiert"); }); } @GetMapping(value = {"/body/{id}/papers", "/body/{id}/papers/{pageNumber}"}) @Operation(summary = "List of all papers in body", description = "returns a list of all papers in requested body", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ObjectList.ObjectListPaper.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public ObjectList papersInBody( @PathVariable @Parameter(description = "body ID", example = "0") String id, @PathVariable(required = false) @Parameter(description = "page number", example = "1") Integer pageNumber) { log.info("invoked method /v1.1/body/{}/papers", id); if (pageNumber == null) { pageNumber = 1; } return service.getPapersInBody(id, pageNumber).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Körperschaft mit angefragter ID existiert"); }); } @GetMapping("/person/{id}") @Operation(summary = "information about person", description = "returns the requested person", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Person.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public Person person( @PathVariable @Parameter(description = "person ID", example = "0") String id) { log.info("invoked method /v1.1/person/{}", id); return service.getPerson(id).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Person mit angefragter ID existiert"); }); } @GetMapping(value = {"/body/{id}/persons", "/body/{id}/persons/{pageNumber}"}) @Operation(summary = "List of all persons in body", description = "returns a list of all persons in requested body", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ObjectList.ObjectListPerson.class))), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorObject.class))) }) public ObjectList persons( @PathVariable @Parameter(description = "body ID", example = "0") String id, @PathVariable(required = false) @Parameter(description = "page number", example = "1") Integer pageNumber) { log.info("invoked method /v1.1/body/{}/persons", id); if (pageNumber == null) { pageNumber = 1; } return service.getPersonsInBody(id, pageNumber).orElseThrow(() -> { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Körperschaft mit angefragter ID existiert"); }); } @GetMapping("/") @Operation(summary = "System information", description = "returns information about the OParl system", responses = { @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content( mediaType = "application/json", schema = @Schema(implementation = System.class) )) }) public System system() { log.info("method invoked /v1.1"); return service.getSystem(); } }