oparl-service/oparl-server/src/main/java/de/twomartens/oparlservice/control/OParlController.java

281 lines
15 KiB
Java

package de.twomartens.oparlservice.control;
import de.twomartens.oparlservice.entity.System;
import de.twomartens.oparlservice.entity.*;
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("/")
@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();
}
@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.class)))
})
public ObjectList<Body> 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("/term/{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/term/{}", id);
return service.getLegislativeTerm(id).orElseThrow(() -> {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Legislaturperiode mit angefragter ID existiert nicht");
});
}
@GetMapping("/body/{id}/organizations")
@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.class))),
@ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorObject.class)))
})
public ObjectList<Organization> organizationsInBody(
@PathVariable
@Parameter(description = "body ID", example = "0")
String id) {
log.info("invoked method /v1.1/body/{}/organizations", id);
return service.getOrganizationsInBody(id).orElseThrow(() -> {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Körperschaft mit angefragter ID existiert");
});
}
@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("/body/{id}/persons")
@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.class))),
@ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorObject.class)))
})
public ObjectList<Person> persons(
@PathVariable
@Parameter(description = "body ID", example = "0")
String id) {
log.info("invoked method /v1.1/body/{}/persons", id);
return service.getPersonsInBody(id).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("/body/{id}/memberships")
@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.class))),
@ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorObject.class)))
})
public ObjectList<Membership> membershipsInBody(
@PathVariable
@Parameter(description = "body ID", example = "0")
String id) {
log.info("invoked method /v1.1/body/{}/memberships", id);
return service.getMembershipsInBody(id).orElseThrow(() -> {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Körperschaft mit angefragter ID existiert");
});
}
@GetMapping("/organization/{id}/memberships")
@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.class))),
@ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorObject.class)))
})
public ObjectList<Membership> membershipsInOrganization(
@PathVariable
@Parameter(description = "organization ID", example = "0")
String id) {
log.info("invoked method /v1.1/organization/{}/memberships", id);
return service.getMembershipsInOrganization(id).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("/body/{id}/meetings")
@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.class))),
@ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorObject.class)))
})
public ObjectList<Meeting> meetingsInBody(
@PathVariable
@Parameter(description = "body ID", example = "0")
String id) {
log.info("invoked method /v1.1/body/{}/meetings", id);
return service.getMeetingsInBody(id).orElseThrow(() -> {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Körperschaft mit angefragter ID existiert");
});
}
@GetMapping("/organization/{id}/meetings")
@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.class))),
@ApiResponse(responseCode = "404", description = "Not found", content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorObject.class)))
})
public ObjectList<Meeting> meetingsInOrganization(
@PathVariable
@Parameter(description = "organization ID", example = "0")
String id) {
log.info("invoked method /v1.1/organization/{}/meetings", id);
return service.getMeetingsInOrganization(id).orElseThrow(() -> {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Keine Organisation 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("/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");
});
}
@ExceptionHandler({ResponseStatusException.class})
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorObject notFound(ResponseStatusException exception) {
return ErrorObject.builder()
.message(exception.getReason())
.debug(exception.getLocalizedMessage())
.build();
}
}