diff options
| author | Jan-Hendrik Willms <tleilax+github@gmail.com> | 2023-12-07 14:04:18 +0100 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2024-10-30 12:40:05 +0000 |
| commit | 288f22cd7f789e6408c4fc8dcfac19627d0ff01b (patch) | |
| tree | f38fd112175dd78dd3833036e97b4f7e42ff6f85 /lib/classes/JsonApi/Routes | |
| parent | 363c78feaae65f3dfaba40b39463e2f1156048d4 (diff) | |
initial commit, re #2799tic-2799
Diffstat (limited to 'lib/classes/JsonApi/Routes')
10 files changed, 775 insertions, 0 deletions
diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceBookingIndex.php b/lib/classes/JsonApi/Routes/Resources/ResourceBookingIndex.php new file mode 100644 index 0000000..9d2b880 --- /dev/null +++ b/lib/classes/JsonApi/Routes/Resources/ResourceBookingIndex.php @@ -0,0 +1,269 @@ +<?php +namespace JsonApi\Routes\Resources; + +use CourseDate; +use JsonApi\ComplexFilter; +use JsonApi\Schemas\ResourceBookingSchema; +use JsonApi\Errors\BadRequestException; +use JsonApi\Errors\RecordNotFoundException; +use JsonApi\JsonApiController; +use Neomerx\JsonApi\Contracts\Http\Query\BaseQueryParserInterface; +use Neomerx\JsonApi\Exceptions\JsonApiException; +use Neomerx\JsonApi\Schema\ErrorCollection; +use Psr\Http\Message\{ + RequestInterface as Request, + ResponseInterface as Response +}; +use Resource; +use ResourceBooking; +use Slim\Routing\RouteContext; +use User; + +final class ResourceBookingIndex extends JsonApiController +{ + protected $allowedPagingParameters = ['offset', 'limit']; + protected $allowedIncludePaths = [ + ResourceBookingSchema::REL_INTERVALS, + ResourceBookingSchema::REL_RESOURCE, + ]; + protected $allowedSortFields = [ + 'begin', + 'end', + 'mkdate', + ]; + protected $allowedFilteringParameters = [ + 'assigned-course-date-id', + 'assigned-user-id', + 'begin', + 'booking-type', + 'booking-user-id', + 'end', + 'range-id', + 'resource-id', + ]; + + public function __invoke(Request $request, Response $response, array $args): Response + { + $filters = $this->getFilters(); + $order = $this->getOrder(); + [$offset, $limit] = $this->getOffsetAndLimit(); + + $routeName = RouteContext::fromRequest($request)->getRoute()->getName(); + if ($routeName === 'bookings-of-resource') { + if (isset($filters['resource-id'])) { + throw new BadRequestException('You may not use the resource-id filter for this route.'); + } + + if (!Resource::exists($args['id'])) { + throw new RecordNotFoundException("No resource found with id {$args['id']}."); + } + + $filters['resource-id'] = $args['id']; + } + + [$condition, $parameters] = $this->getConditionAndParameters($filters); + + $total = ResourceBooking::countBySql($condition, $parameters); + $bookings = ResourceBooking::findBySQL( + "{$condition} {$order} LIMIT {$offset}, {$limit}", + $parameters + ); + + return $this->getPaginatedContentResponse($bookings, $total); + } + + private function getFilters(): array + { + $filters = iterator_to_array($this->getQueryParameters()->getFilters()); + $errors = new ErrorCollection(); + + if (array_key_exists('assigned-course-date-id', $filters)) { + if (!CourseDate::exists($filters['assigned-course-date-id'])) { + $errors->addQueryParameterError( + BaseQueryParserInterface::PARAM_FILTER, + sprintf( + 'Filter assigned-course-date-id links to an unknown course date with id %s.', + $filters['assigned-course-date-id'] + ) + ); + } + } + + if (array_key_exists('assigned-user-id', $filters)) { + if (!User::exists($filters['assigned-user-id'])) { + $errors->addQueryParameterError( + BaseQueryParserInterface::PARAM_FILTER, + sprintf( + 'Filter assigned-user-id links to an unknown user with id %s.', + $filters['assigned-user-id'] + ) + ); + } + } + + if (array_key_exists('begin', $filters)) { + if (ComplexFilter::detect($filters['begin'])) { + $filters['begin'] = ComplexFilter::create($filters['begin']); + } elseif (!is_numeric($filters['begin'])) { + $errors->addQueryParameterError( + BaseQueryParserInterface::PARAM_FILTER, + 'Filter begin must be numeric.' + ); + } else { + $filters['begin'] = (int) $filters['begin']; + } + } + + if (array_key_exists('booking-type', $filters)) { + if (!is_numeric($filters['booking-type'])) { + $errors->addQueryParameterError( + BaseQueryParserInterface::PARAM_FILTER, + 'Filter booking-type must be numeric.' + ); + } else { + $filters['booking-type'] = (int) $filters['booking-type']; + } + } + + if (array_key_exists('booking-user-id', $filters)) { + if (!User::exists($filters['booking-user-id'])) { + $errors->addQueryParameterError( + BaseQueryParserInterface::PARAM_FILTER, + sprintf( + 'Filter booking-user-id links to an unknown user with id %s.', + $filters['booking-user-id'] + ) + ); + } + } + + if (array_key_exists('end', $filters)) { + if (ComplexFilter::detect($filters['end'])) { + $filters['end'] = ComplexFilter::create($filters['end']); + } elseif (!is_numeric($filters['end'])) { + $errors->addQueryParameterError( + BaseQueryParserInterface::PARAM_FILTER, + 'Filter end must be numeric.' + ); + } else { + $filters['end'] = (int) $filters['end']; + } + } + + if (array_key_exists('range-id', $filters)) { + if ( + !CourseDate::exists($filters['range-id']) + && !User::exists($filters['range-id']) + ) { + $errors->addQueryParameterError( + BaseQueryParserInterface::PARAM_FILTER, + sprintf( + 'Filter range-id links to an unknown course date or user with id %s.', + $filters['range-id'] + ) + ); + } + } + + if (array_key_exists('resource-id', $filters)) { + if (!Resource::exists($filters['resource-id'])) { + $errors->addQueryParameterError( + BaseQueryParserInterface::PARAM_FILTER, + sprintf( + 'Filter resource-id links to an unknown resource with id %s.', + $filters['resource-id'] + ) + ); + } + } + + if (count($errors) > 0) { + throw new JsonApiException($errors, JsonApiException::HTTP_CODE_BAD_REQUEST); + } + + return $filters; + } + + private function getOrder(): string + { + $result = []; + foreach ($this->getQueryParameters()->getSorts() as $column => $ascending) { + if ($ascending) { + $result[] = $column; + } else { + $result[] = "{$column} DESC"; + } + } + + return count($result) > 0 ? 'ORDER BY ' . implode(', ', $result) : ''; + } + + private function getConditionAndParameters(array $filters): array + { + $conditions = []; + $joins = []; + $parameters = []; + + if (array_key_exists('assigned-course-date-id', $filters)) { + $conditions[] = 'range_id = :assigned_course_date_id'; + $parameters[':assigned_course_date_id'] = $filters['assigned-course-date-id']; + } + + if (array_key_exists('assigned-user-id', $filters)) { + $conditions[] = 'range_id = :assigned_user_id'; + $parameters[':assigned_user_id'] = $filters['assigned-user-id']; + } + + if (array_key_exists('begin', $filters)) { + if ($filters['begin'] instanceof ComplexFilter) { + $filters['begin']->apply($conditions, $parameters, 'begin'); + } else { + $conditions[] = 'begin = :begin'; + $parameters[':begin'] = $filters['begin']; + } + } + + if (array_key_exists('booking-type', $filters)) { + $conditions[] = 'booking_type = :booking_type'; + $parameters[':booking_type'] = $filters['booking-type']; + } + + if (array_key_exists('booking-user-id', $filters)) { + $conditions[] = 'booking_user_id = :booking_user_id'; + $parameters[':booking_user_id'] = $filters['booking-user-id']; + } + + if (array_key_exists('end', $filters)) { + if ($filters['end'] instanceof ComplexFilter) { + $filters['end']->apply($conditions, $parameters, 'end'); + } else { + $conditions[] = 'end = :end'; + $parameters[':end'] = $filters['end']; + } + } + + if (array_key_exists('range-id', $filters)) { + $conditions[] = 'range_id = :range_id'; + $parameters[':range_id'] = $filters['range_id']; + } + + if (array_key_exists('resource-id', $filters)) { + $conditions[] = 'resource_id = :resource_id'; + $parameters[':resource_id'] = $filters['resource-id']; + } + + $condition = implode(' ', $joins); + if ($condition) { + $condition .= ' WHERE '; + } + $condition .= '(' . implode(') AND (', $conditions ?: [1]) . ')'; + + return [$condition, $parameters]; + } + + + private function decomposeFilter(string $key, $value) + { + + } +} diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceBookingIntervalIndex.php b/lib/classes/JsonApi/Routes/Resources/ResourceBookingIntervalIndex.php new file mode 100644 index 0000000..578e1fe --- /dev/null +++ b/lib/classes/JsonApi/Routes/Resources/ResourceBookingIntervalIndex.php @@ -0,0 +1,209 @@ +<?php +namespace JsonApi\Routes\Resources; + +use JsonApi\ComplexFilter; +use JsonApi\Schemas\ResourceBookingIntervalSchema; +use JsonApi\Errors\BadRequestException; +use JsonApi\Errors\RecordNotFoundException; +use JsonApi\JsonApiController; +use Neomerx\JsonApi\Contracts\Http\Query\BaseQueryParserInterface; +use Neomerx\JsonApi\Exceptions\JsonApiException; +use Neomerx\JsonApi\Schema\ErrorCollection; +use Psr\Http\Message\RequestInterface as Request; +use Psr\Http\Message\ResponseInterface as Response; +use Resource; +use ResourceBooking; +use ResourceBookingInterval; +use Slim\Routing\RouteContext; + +final class ResourceBookingIntervalIndex extends JsonApiController +{ + protected $allowedPagingParameters = ['offset', 'limit']; + protected $allowedIncludePaths = [ + ResourceBookingIntervalSchema::REL_BOOKING, + ResourceBookingIntervalSchema::REL_RESOURCE, + ]; + protected $allowedSortFields = [ + 'begin', + 'end', + 'mkdate', + ]; + protected $allowedFilteringParameters = [ + 'begin', + 'booking-id', + 'end', + 'takes-place', + 'resource-id', + ]; + protected $allowedFieldSetTypes = [ + ResourceBookingIntervalSchema::TYPE => ['begin', 'end', 'takes-place', 'mkdate', 'chdate'], + ]; + + public function __invoke(Request $request, Response $response, array $args): Response + { + $filters = $this->getFilters(); + $order = $this->getOrder(); + [$offset, $limit] = $this->getOffsetAndLimit(); + + $routeName = RouteContext::fromRequest($request)->getRoute()->getName(); + if ($routeName === 'intervals-of-booking') { + if (isset($filters['booking-id'])) { + throw new BadRequestException('You may not use the booking-id filter for this route.'); + } + + if (!ResourceBooking::exists($args['id'])) { + throw new RecordNotFoundException("No resource booking found with id {$args['id']}."); + } + + $filters['booking-id'] = $args['id']; + } elseif ($routeName === 'intervals-of-resource') { + if (isset($filters['resource-id'])) { + throw new BadRequestException('You may not use the resource-id filter for this route.'); + } + + if (!Resource::exists($args['id'])) { + throw new RecordNotFoundException("No resource found with id {$args['id']}."); + } + + $filters['resource-id'] = $args['id']; + } + + [$condition, $parameters] = $this->getConditionAndParameters($filters); + + $total = ResourceBookingInterval::countBySql($condition, $parameters); + $bookings = ResourceBookingInterval::findBySQL( + "{$condition} {$order} LIMIT {$offset}, {$limit}", + $parameters + ); + + return $this->getPaginatedContentResponse($bookings, $total); + } + + private function getFilters(): array + { + $filters = iterator_to_array($this->getQueryParameters()->getFilters()); + $errors = new ErrorCollection(); + + if (array_key_exists('begin', $filters)) { + if (ComplexFilter::detect($filters['begin'])) { + $filters['begin'] = ComplexFilter::create($filters['begin']); + } elseif (!is_numeric($filters['begin'])) { + $errors->addQueryParameterError( + BaseQueryParserInterface::PARAM_FILTER, + 'Filter begin must be numeric.' + ); + } else { + $filters['begin'] = (int)$filters['begin']; + } + } + + if (array_key_exists('booking-id', $filters)) { + if (!ResourceBooking::exists($filters['booking-id'])) { + $errors->addQueryParameterError( + BaseQueryParserInterface::PARAM_FILTER, + sprintf( + 'Filter booking-id links to an unknown resource booking with id %s.', + $filters['booking-id'] + ) + ); + } + } + + if (array_key_exists('end', $filters)) { + if (ComplexFilter::detect($filters['end'])) { + $filters['end'] = ComplexFilter::create($filters['end']); + } elseif (!is_numeric($filters['end'])) { + $errors->addQueryParameterError( + BaseQueryParserInterface::PARAM_FILTER, + 'Filter end must be numeric.' + ); + } else { + $filters['end'] = (int)$filters['end']; + } + } + + if (array_key_exists('takes-place', $filters)) { + $filters['takes-place'] = (bool) $filters['takes-place']; + } + + if (array_key_exists('resource-id', $filters)) { + if (!Resource::exists($filters['resource-id'])) { + $errors->addQueryParameterError( + BaseQueryParserInterface::PARAM_FILTER, + sprintf( + 'Filter resource-id links to an unknown resource with id %s.', + $filters['resource-id'] + ) + ); + } + } + + if (count($errors) > 0) { + throw new JsonApiException($errors, JsonApiException::HTTP_CODE_BAD_REQUEST); + } + + return $filters; + } + + private function getOrder(): string + { + $result = []; + foreach ($this->getQueryParameters()->getSorts() as $column => $ascending) { + if ($ascending) { + $result[] = $column; + } else { + $result[] = "{$column} DESC"; + } + } + + return count($result) > 0 ? 'ORDER BY ' . implode(', ', $result) : ''; + } + + private function getConditionAndParameters(array $filters): array + { + $conditions = []; + $joins = []; + $parameters = []; + + if (array_key_exists('begin', $filters)) { + if ($filters['begin'] instanceof ComplexFilter) { + $filters['begin']->apply($conditions, $parameters, 'begin'); + } else { + $conditions[] = 'begin = :begin'; + $parameters[':begin'] = $filters['begin']; + } + } + + if (array_key_exists('booking-id', $filters)) { + $conditions[] = 'booking_id = :booking_id'; + $parameters[':booking_id'] = $filters['booking-id']; + } + + if (array_key_exists('end', $filters)) { + if ($filters['end'] instanceof ComplexFilter) { + $filters['end']->apply($conditions, $parameters, 'end'); + } else { + $conditions[] = 'end = :end'; + $parameters[':end'] = $filters['end']; + } + } + + if (array_key_exists('takes-place', $filters)) { + $conditions[] = 'takes_place = :takes_place'; + $parameters[':takes_place'] = (int) $filters['takes-place']; + } + + if (array_key_exists('resource-id', $filters)) { + $conditions[] = 'resource_id = :resource_id'; + $parameters[':resource_id'] = $filters['resource-id']; + } + + $condition = implode(' ', $joins); + if ($condition) { + $condition .= ' WHERE '; + } + $condition .= '(' . implode(') AND (', $conditions ?: [1]) . ')'; + + return [$condition, $parameters]; + } +} diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceBookingIntervalShow.php b/lib/classes/JsonApi/Routes/Resources/ResourceBookingIntervalShow.php new file mode 100644 index 0000000..b561b42 --- /dev/null +++ b/lib/classes/JsonApi/Routes/Resources/ResourceBookingIntervalShow.php @@ -0,0 +1,27 @@ +<?php +namespace JsonApi\Routes\Resources; + +use JsonApi\Schemas\ResourceBookingIntervalSchema; +use JsonApi\Errors\RecordNotFoundException; +use JsonApi\JsonApiController; +use Psr\Http\Message\RequestInterface as Request; +use Psr\Http\Message\ResponseInterface as Response; +use ResourceBookingInterval; + +final class ResourceBookingIntervalShow extends JsonApiController +{ + protected $allowedIncludePaths = [ + ResourceBookingIntervalSchema::REL_BOOKING, + ResourceBookingIntervalSchema::REL_RESOURCE, + ]; + + public function __invoke(Request $request, Response $response, array $args): Response + { + $interval = ResourceBookingInterval::find($args['id']); + if (!$interval) { + throw new RecordNotFoundException("No booking interval with the id {$args['id']}"); + } + + return $this->getContentResponse($interval); + } +} diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceBookingShow.php b/lib/classes/JsonApi/Routes/Resources/ResourceBookingShow.php new file mode 100644 index 0000000..f5f0dc8 --- /dev/null +++ b/lib/classes/JsonApi/Routes/Resources/ResourceBookingShow.php @@ -0,0 +1,23 @@ +<?php +namespace JsonApi\Routes\Resources; + +use JsonApi\Errors\RecordNotFoundException; +use JsonApi\JsonApiController; +use Psr\Http\Message\{ + RequestInterface as Request, + ResponseInterface as Response +}; +use ResourceBooking; + +final class ResourceBookingShow extends JsonApiController +{ + public function __invoke(Request $request, Response $response, array $args): Response + { + $booking = ResourceBooking::find($args['id']); + if (!$booking) { + throw new RecordNotFoundException("No booking with the id {$args['id']}"); + } + + return $this->getContentResponse($booking); + } +} diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceCategoryIndex.php b/lib/classes/JsonApi/Routes/Resources/ResourceCategoryIndex.php new file mode 100644 index 0000000..4eed857 --- /dev/null +++ b/lib/classes/JsonApi/Routes/Resources/ResourceCategoryIndex.php @@ -0,0 +1,91 @@ +<?php +namespace JsonApi\Routes\Resources; + +use JsonApi\Errors\BadRequestException; +use JsonApi\JsonApiController; +use Psr\Http\Message\{ + RequestInterface as Request, + ResponseInterface as Response +}; + +final class ResourceCategoryIndex extends JsonApiController +{ + protected $allowedFilteringParameters = ['class_name', 'system']; + protected $allowedPagingParameters = ['offset', 'limit']; + + public function __invoke(Request $request, Response $response, array $args): Response + { + [$offset, $limit] = $this->getOffsetAndLimit(); + [$condition, $parameters] = $this->getConditionAndParameters( + $this->getFilters() + ); + + $total = \ResourceCategory::countBySql($condition, $parameters); + $resources = \ResourceCategory::findBySQL( + "{$condition} LIMIT {$offset}, {$limit}", + $parameters + ); + + return $this->getPaginatedContentResponse($resources, $total); + } + + private function getFilters() + { + $filters = []; + + $filtering = $this->getQueryParameters()->getFilteringParameters() ?? []; + + if (array_key_exists('class_name', $filtering)) { + if (empty($filtering['class_name'])) { + throw new BadRequestException('Class name filter must be not be empty.'); + } + + $filters['class_name'] = $filtering['class_name']; + } + + if (array_key_exists('system', $filtering)) { + $filters['system'] = (bool) $filtering['system']; + } + + return $filters; + } + + private function getConditionAndParameters(array $filters): array + { + $joins = []; + $conditions = []; + $parameters = []; + + if (array_key_exists('class_name', $filters)) { + $conditions[] = '`class_name` = :class'; + $parameters[':class'] = $filters['class']; + } + + if (array_key_exists('system', $filters)) { + $conditions[] = '`system` = :system'; + $parameters[':system'] = (int) $filters['system']; + } + + // Build condition + $condition = implode(' ', $joins); + if ($condition) { + $condition .= ' WHERE '; + } + + if (count($conditions) === 0) { + $conditions[] = '1'; + } + + $condition .= implode(' AND ', array_map( + function ($condition): string { + return "({$condition})"; + }, + $conditions + )); + + return [ + $condition, + $parameters, + ]; + } +} diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceCategoryShow.php b/lib/classes/JsonApi/Routes/Resources/ResourceCategoryShow.php new file mode 100644 index 0000000..f73d06e --- /dev/null +++ b/lib/classes/JsonApi/Routes/Resources/ResourceCategoryShow.php @@ -0,0 +1,27 @@ +<?php +namespace JsonApi\Routes\Resources; + +use JsonApi\Errors\BadRequestException; +use JsonApi\Errors\RecordNotFoundException; +use JsonApi\JsonApiController; +use Psr\Http\Message\{ + RequestInterface as Request, + ResponseInterface as Response +}; + +final class ResourceCategoryShow extends JsonApiController +{ + public function __invoke(Request $request, Response $response, array $args): Response + { + if (empty($args['id'])) { + throw new BadRequestException('Id must not be empty.'); + } + + $resource = \ResourceCategory::find($args['id']); + if ($resource === null) { + throw new RecordNotFoundException("No resource category found with id {$args['id']}"); + } + + return $this->getContentResponse($resource); + } +} diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceIndex.php b/lib/classes/JsonApi/Routes/Resources/ResourceIndex.php new file mode 100644 index 0000000..07f5b12 --- /dev/null +++ b/lib/classes/JsonApi/Routes/Resources/ResourceIndex.php @@ -0,0 +1,99 @@ +<?php +namespace JsonApi\Routes\Resources; + +use JsonApi\Schemas\ResourceSchema; +use JsonApi\Errors\BadRequestException; +use JsonApi\JsonApiController; +use Psr\Http\Message\{ + RequestInterface as Request, + ResponseInterface as Response +}; + +final class ResourceIndex extends JsonApiController +{ + protected $allowedFilteringParameters = ['level', 'class']; + protected $allowedIncludePaths = [ResourceSchema::REL_CATEGORY]; + protected $allowedPagingParameters = ['offset', 'limit']; + + public function __invoke(Request $request, Response $response, array $args): Response + { + [$offset, $limit] = $this->getOffsetAndLimit(); + [$condition, $parameters] = $this->getConditionAndParameters( + $this->getFilters() + ); + + $total = \Resource::countBySql($condition, $parameters); + $resources = \Resource::findBySQL( + "{$condition} LIMIT {$offset}, {$limit}", + $parameters + ); + + return $this->getPaginatedContentResponse($resources, $total); + } + + private function getFilters() + { + $filters = []; + + $filtering = $this->getQueryParameters()->getFilteringParameters() ?? []; + + if (array_key_exists('level', $filtering)) { + if (!ctype_digit($filtering['level'])) { + throw new BadRequestException('Level filter must be an int.'); + } + + $filters['level'] = (int) $filtering['level']; + } + + if (array_key_exists('class', $filtering)) { + if (empty($filtering['class'])) { + throw new BadRequestException('Class filter must be not be empty.'); + } + + $filters['class'] = $filtering['class']; + } + + return $filters; + } + + private function getConditionAndParameters(array $filters): array + { + $joins = []; + $conditions = []; + $parameters = []; + + if (array_key_exists('level', $filters)) { + $conditions[] = '`resources`.`level` = :level'; + $parameters[':level'] = $filters['level']; + } + + if (array_key_exists('class', $filters)) { + $joins[] = 'JOIN `resource_categories` + ON `resources`.`category_id` = `resource_categories`.`id`'; + $conditions[] = '`resource_categories`.`class_name` = :class'; + $parameters[':class'] = $filters['class']; + } + + // Build condition + $condition = implode(' ', $joins); + if ($condition) { + $condition .= ' WHERE '; + } + + if (count($conditions) === 0) { + $conditions[] = '1'; + } + + $condition .= implode(' AND ', array_map( + function ($condition): string { + return "({$condition})"; + }, + $conditions + )); + + return [ + $condition, + $parameters, + ]; + } +} diff --git a/lib/classes/JsonApi/Routes/Resources/ResourcePropertyIndex.php b/lib/classes/JsonApi/Routes/Resources/ResourcePropertyIndex.php new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/classes/JsonApi/Routes/Resources/ResourcePropertyIndex.php diff --git a/lib/classes/JsonApi/Routes/Resources/ResourcePropertyShow.php b/lib/classes/JsonApi/Routes/Resources/ResourcePropertyShow.php new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/classes/JsonApi/Routes/Resources/ResourcePropertyShow.php diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceShow.php b/lib/classes/JsonApi/Routes/Resources/ResourceShow.php new file mode 100644 index 0000000..19dceb9 --- /dev/null +++ b/lib/classes/JsonApi/Routes/Resources/ResourceShow.php @@ -0,0 +1,30 @@ +<?php +namespace JsonApi\Routes\Resources; + +use JsonApi\Schemas\ResourceSchema; +use JsonApi\Errors\BadRequestException; +use JsonApi\Errors\RecordNotFoundException; +use JsonApi\JsonApiController; +use Psr\Http\Message\{ + RequestInterface as Request, + ResponseInterface as Response +}; + +final class ResourceShow extends JsonApiController +{ + protected $allowedIncludePaths = [ResourceSchema::REL_CATEGORY]; + + public function __invoke(Request $request, Response $response, array $args): Response + { + if (empty($args['id'])) { + throw new BadRequestException('Id must not be empty.'); + } + + $resource = \Resource::find($args['id']); + if ($resource === null) { + throw new RecordNotFoundException("No resource found with id {$args['id']}"); + } + + return $this->getContentResponse($resource); + } +} |
