feat(bookstack) : add BookStackShelf API resource for listing shelves
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
29
src/ApiResource/BookStackShelf.php
Normal file
29
src/ApiResource/BookStackShelf.php
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\ApiResource;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\ApiResource;
|
||||||
|
use ApiPlatform\Metadata\GetCollection;
|
||||||
|
use App\State\BookStackShelfProvider;
|
||||||
|
use Symfony\Component\Serializer\Attribute\Groups;
|
||||||
|
|
||||||
|
#[ApiResource(
|
||||||
|
operations: [
|
||||||
|
new GetCollection(
|
||||||
|
uriTemplate: '/bookstack/shelves',
|
||||||
|
normalizationContext: ['groups' => ['bookstack_shelf:read']],
|
||||||
|
provider: BookStackShelfProvider::class,
|
||||||
|
security: "is_granted('ROLE_ADMIN')",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)]
|
||||||
|
final class BookStackShelf
|
||||||
|
{
|
||||||
|
#[Groups(['bookstack_shelf:read'])]
|
||||||
|
public int $id = 0;
|
||||||
|
|
||||||
|
#[Groups(['bookstack_shelf:read'])]
|
||||||
|
public string $name = '';
|
||||||
|
}
|
||||||
36
src/State/BookStackShelfProvider.php
Normal file
36
src/State/BookStackShelfProvider.php
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\State;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use ApiPlatform\State\ProviderInterface;
|
||||||
|
use App\ApiResource\BookStackShelf;
|
||||||
|
use App\Exception\BookStackApiException;
|
||||||
|
use App\Service\BookStackApiService;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||||
|
|
||||||
|
final readonly class BookStackShelfProvider implements ProviderInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private BookStackApiService $bookStackApiService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$shelves = $this->bookStackApiService->listShelves();
|
||||||
|
} catch (BookStackApiException $e) {
|
||||||
|
throw new BadRequestHttpException($e->getMessage(), $e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_map(static function (array $shelf): BookStackShelf {
|
||||||
|
$dto = new BookStackShelf();
|
||||||
|
$dto->id = $shelf['id'] ?? 0;
|
||||||
|
$dto->name = $shelf['name'] ?? '';
|
||||||
|
|
||||||
|
return $dto;
|
||||||
|
}, $shelves);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user