Skip to content

Commit

Permalink
Port BookOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
lazka committed Nov 9, 2023
1 parent 3a4b274 commit 99b5458
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 100 deletions.
1 change: 0 additions & 1 deletion src/API/SublibraryProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Dbp\Relay\BasePersonBundle\Entity\Person;
use Dbp\Relay\CoreBundle\Exception\ApiError;
use Dbp\Relay\SublibraryBundle\Entity\Sublibrary;

interface SublibraryProviderInterface
{
Expand Down
46 changes: 1 addition & 45 deletions src/Entity/BookOrder.php → src/ApiPlatform/BookOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,34 @@

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
*/
private $orderNumber;

/**
* @Groups({"LibraryBookOrder:output"})
* @ApiProperty(iri="http://schema.org/Text")
*
* @var string
*/
private $receivingNote;

/**
* @Groups({"LibraryBookOrder:output"})
* @ApiProperty(iri="http://schema.org/OrderItem")
*
* @var BookOrderItem
*/
Expand All @@ -87,7 +44,6 @@ class BookOrder

/**
* @Groups({"LibraryBookOrder:output"})
* @ApiProperty(iri="http://schema.org/DateTime")
*
* @var DateTimeInterface
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
38 changes: 0 additions & 38 deletions src/DataProvider/BookOrderItemDataProvider.php

This file was deleted.

49 changes: 49 additions & 0 deletions src/Resources/config/api_resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
11 changes: 8 additions & 3 deletions src/Service/AlmaApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/Service/DummySublibraryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 99b5458

Please sign in to comment.