From 99b545801802b895547c48c9ed02ba8a61f9c903 Mon Sep 17 00:00:00 2001 From: Christoph Reiter Date: Thu, 9 Nov 2023 14:10:22 +0100 Subject: [PATCH] Port BookOrder --- src/API/SublibraryProviderInterface.php | 1 - src/{Entity => ApiPlatform}/BookOrder.php | 46 +---------------- .../BookOrderProvider.php} | 27 +++++----- .../BookOrderItemDataProvider.php | 38 -------------- src/Resources/config/api_resources.yaml | 49 +++++++++++++++++++ src/Service/AlmaApi.php | 11 +++-- src/Service/DummySublibraryProvider.php | 6 ++- tests/ApiTest.php | 2 +- 8 files changed, 80 insertions(+), 100 deletions(-) rename src/{Entity => ApiPlatform}/BookOrder.php (55%) rename src/{DataProvider/BookOrderCollectionDataProvider.php => ApiPlatform/BookOrderProvider.php} (56%) delete mode 100644 src/DataProvider/BookOrderItemDataProvider.php diff --git a/src/API/SublibraryProviderInterface.php b/src/API/SublibraryProviderInterface.php index 28b0ae6..fd68a73 100644 --- a/src/API/SublibraryProviderInterface.php +++ b/src/API/SublibraryProviderInterface.php @@ -6,7 +6,6 @@ use Dbp\Relay\BasePersonBundle\Entity\Person; use Dbp\Relay\CoreBundle\Exception\ApiError; -use Dbp\Relay\SublibraryBundle\Entity\Sublibrary; interface SublibraryProviderInterface { diff --git a/src/Entity/BookOrder.php b/src/ApiPlatform/BookOrder.php similarity index 55% rename from src/Entity/BookOrder.php rename to src/ApiPlatform/BookOrder.php index adf0f5d..61d4428 100644 --- a/src/Entity/BookOrder.php +++ b/src/ApiPlatform/BookOrder.php @@ -2,61 +2,20 @@ declare(strict_types=1); -namespace Dbp\Relay\SublibraryBundle\Entity; +namespace Dbp\Relay\SublibraryBundle\ApiPlatform; -use ApiPlatform\Core\Annotation\ApiProperty; -use ApiPlatform\Core\Annotation\ApiResource; use DateTimeInterface; -use Dbp\Relay\SublibraryBundle\ApiPlatform\BookOrderItem; use Symfony\Component\Serializer\Annotation\Groups; -/** - * @ApiResource( - * attributes={ - * "security" = "is_granted('IS_AUTHENTICATED_FULLY') and is_granted('ROLE_LIBRARY_MANAGER')" - * }, - * collectionOperations={ - * "get" = { - * "security" = "is_granted('IS_AUTHENTICATED_FULLY') and is_granted('ROLE_LIBRARY_MANAGER')", - * "path" = "/sublibrary/book-orders", - * "openapi_context" = { - * "tags" = {"Sublibrary"}, - * "parameters" = { - * {"name" = "sublibrary", "in" = "query", "description" = "Get all book offers of a sublibrary (ID of Sublibrary resource)", "required" = true, "type" = "string", "example" = "1190"}, - * } - * } - * } - * }, - * itemOperations={ - * "get" = { - * "security" = "is_granted('IS_AUTHENTICATED_FULLY') and is_granted('ROLE_LIBRARY_MANAGER')", - * "path" = "/sublibrary/book-orders/{identifier}", - * "openapi_context" = { - * "tags" = {"Sublibrary"}, - * }, - * } - * }, - * iri="http://schema.org/Order", - * shortName="LibraryBookOrder", - * description="A book order from the library", - * normalizationContext={ - * "jsonld_embed_context" = true, - * "groups" = {"LibraryBookOrder:output"} - * }, - * ) - */ class BookOrder { /** - * @ApiProperty(identifier=true) - * * @var string */ private $identifier; /** * @Groups({"LibraryBookOrder:output"}) - * @ApiProperty(iri="http://schema.org/orderNumber") * * @var string */ @@ -64,7 +23,6 @@ class BookOrder /** * @Groups({"LibraryBookOrder:output"}) - * @ApiProperty(iri="http://schema.org/Text") * * @var string */ @@ -72,7 +30,6 @@ class BookOrder /** * @Groups({"LibraryBookOrder:output"}) - * @ApiProperty(iri="http://schema.org/OrderItem") * * @var BookOrderItem */ @@ -87,7 +44,6 @@ class BookOrder /** * @Groups({"LibraryBookOrder:output"}) - * @ApiProperty(iri="http://schema.org/DateTime") * * @var DateTimeInterface */ diff --git a/src/DataProvider/BookOrderCollectionDataProvider.php b/src/ApiPlatform/BookOrderProvider.php similarity index 56% rename from src/DataProvider/BookOrderCollectionDataProvider.php rename to src/ApiPlatform/BookOrderProvider.php index fff33f9..5f2fb07 100644 --- a/src/DataProvider/BookOrderCollectionDataProvider.php +++ b/src/ApiPlatform/BookOrderProvider.php @@ -2,15 +2,15 @@ declare(strict_types=1); -namespace Dbp\Relay\SublibraryBundle\DataProvider; +namespace Dbp\Relay\SublibraryBundle\ApiPlatform; -use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface; -use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; +use ApiPlatform\Metadata\CollectionOperationInterface; +use ApiPlatform\Metadata\Operation; +use ApiPlatform\State\ProviderInterface; use Dbp\Relay\CoreBundle\Helpers\ArrayFullPaginator; -use Dbp\Relay\SublibraryBundle\Entity\BookOrder; use Dbp\Relay\SublibraryBundle\Service\AlmaApi; -final class BookOrderCollectionDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface +final class BookOrderProvider implements ProviderInterface { public const ITEMS_PER_PAGE = 100; @@ -22,15 +22,20 @@ public function __construct(AlmaApi $api) $this->api = $api; } - public function supports(string $resourceClass, string $operationName = null, array $context = []): bool - { - return BookOrder::class === $resourceClass; - } - - public function getCollection(string $resourceClass, string $operationName = null, array $context = []): ArrayFullPaginator + /** + * @return ArrayFullPaginator|BookOrder + */ + public function provide(Operation $operation, array $uriVariables = [], array $context = []) { $this->api->checkPermissions(); + if (!$operation instanceof CollectionOperationInterface) { + $id = $uriVariables['identifier']; + assert(is_string($id)); + + return $this->api->getBookOrder($id); + } + $filters = $context['filters'] ?? []; $collection = $this->api->getBookOrders($filters); diff --git a/src/DataProvider/BookOrderItemDataProvider.php b/src/DataProvider/BookOrderItemDataProvider.php deleted file mode 100644 index c95b332..0000000 --- a/src/DataProvider/BookOrderItemDataProvider.php +++ /dev/null @@ -1,38 +0,0 @@ -almaApi = $almaApi; - } - - public function supports(string $resourceClass, string $operationName = null, array $context = []): bool - { - return BookOrder::class === $resourceClass; - } - - /** - * Fetches a book order from the list of book orders on an organization (there is no valid other api to do this). - * - * @throws ItemNotFoundException - */ - public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?BookOrder - { - $this->almaApi->checkPermissions(); - - return $this->almaApi->getBookOrder($id); - } -} diff --git a/src/Resources/config/api_resources.yaml b/src/Resources/config/api_resources.yaml index 106e5f1..5faeab4 100644 --- a/src/Resources/config/api_resources.yaml +++ b/src/Resources/config/api_resources.yaml @@ -253,3 +253,52 @@ resources: iri: "http://schema.org/price" priceCurrency: iri: "http://schema.org/priceCurrency" + + + Dbp\Relay\SublibraryBundle\ApiPlatform\BookOrder: + security: "is_granted('IS_AUTHENTICATED_FULLY') and is_granted('ROLE_LIBRARY_MANAGER')" + types: ['http://schema.org/Order'] + description: "A book order from the library" + shortName: "LibraryBookOrder" + routePrefix: "/sublibrary" + normalizationContext: + groups: ["LibraryBookOrder:output"] + jsonld_embed_context: true + + operations: + + ApiPlatform\Metadata\GetCollection: + security: "is_granted('IS_AUTHENTICATED_FULLY') and is_granted('ROLE_LIBRARY_MANAGER')" + provider: Dbp\Relay\SublibraryBundle\ApiPlatform\BookOrderProvider + uriTemplate: "/book-orders" + openapiContext: + tags: ["Sublibrary"] + parameters: + - name: sublibrary + in: query + description: 'Get all book offers of a sublibrary (ID of Sublibrary resource)' + required: true + type: "string" + example: "1190" + + ApiPlatform\Metadata\Get: + security: "is_granted('IS_AUTHENTICATED_FULLY') and is_granted('ROLE_LIBRARY_MANAGER')" + provider: Dbp\Relay\SublibraryBundle\ApiPlatform\BookOrderProvider + uriTemplate: "/book-orders/{identifier}" + openapiContext: + tags: ["Sublibrary"] + + properties: + identifier: + identifier: true + iri: "http://schema.org/identifier" + orderNumber: + iri: "http://schema.org/orderNumber" + receivingNote: + iri: "http://schema.org/Text" + orderedItem: + iri: "http://schema.org/OrderItem" + orderStatus: + iri: "http://schema.org/Text" + orderDate: + iri: "http://schema.org/DateTime" diff --git a/src/Service/AlmaApi.php b/src/Service/AlmaApi.php index 213e932..465925c 100644 --- a/src/Service/AlmaApi.php +++ b/src/Service/AlmaApi.php @@ -15,6 +15,7 @@ use Dbp\Relay\CoreBundle\Rest\Options; use Dbp\Relay\SublibraryBundle\API\SublibraryProviderInterface; use Dbp\Relay\SublibraryBundle\ApiPlatform\Book; +use Dbp\Relay\SublibraryBundle\ApiPlatform\BookOrder; use Dbp\Relay\SublibraryBundle\ApiPlatform\BookOrderItem; use Dbp\Relay\SublibraryBundle\ApiPlatform\BudgetMonetaryAmount; use Dbp\Relay\SublibraryBundle\ApiPlatform\DeliveryEvent; @@ -23,7 +24,6 @@ use Dbp\Relay\SublibraryBundle\Entity\BookLoan; use Dbp\Relay\SublibraryBundle\Entity\BookLocation; use Dbp\Relay\SublibraryBundle\Entity\BookOffer; -use Dbp\Relay\SublibraryBundle\Entity\BookOrder; use Dbp\Relay\SublibraryBundle\Entity\Sublibrary; use Dbp\Relay\SublibraryBundle\Helpers\ItemNotFoundException; use Dbp\Relay\SublibraryBundle\Helpers\ItemNotLoadedException; @@ -1988,12 +1988,17 @@ public function checkCurrentPersonLibraryPermissions(Sublibrary $library) } } - public function getCurrentPerson(): ?Person + public function getCurrentPerson(): Person { $options = []; Options::requestLocalDataAttributes($options, [self::EMAIL_ATTRIBUTE, self::ALMA_ID_ATTRIBUTE, self::TUG_FUNCTIONS_ATTRIBUTE]); - return $this->personProvider->getCurrentPerson($options); + $person = $this->personProvider->getCurrentPerson($options); + if ($person === null) { + throw new AccessDeniedException('Person required'); + } + + return $person; } public function getPerson(string $personIdentifier, bool $addInternalAttributes = true): ?Person diff --git a/src/Service/DummySublibraryProvider.php b/src/Service/DummySublibraryProvider.php index e4651e3..bd149ad 100644 --- a/src/Service/DummySublibraryProvider.php +++ b/src/Service/DummySublibraryProvider.php @@ -19,7 +19,11 @@ class DummySublibraryProvider implements SublibraryProviderInterface */ public function getSublibrary(string $identifier, array $options = []): ?SublibraryInterface { - return new Sublibrary(); + $lib = new Sublibrary(); + $lib->setIdentifier($identifier); + $lib->setCode($identifier); + + return $lib; } /** diff --git a/tests/ApiTest.php b/tests/ApiTest.php index b45b524..2821161 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -19,7 +19,7 @@ public function testNotAuth() $path = str_replace('.{_format}', '', $path); foreach ($route->getMethods() as $method) { $client = self::createClient(); - $response = $client->request($method, $path); + $response = $client->request($method, $path.'?sublibrary=1234'); $this->assertContains($response->getStatusCode(), [401, 404], $path); } }