feat(bookstack) : add BookStackSearchResult API resource for shelf-scoped search
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
35
src/ApiResource/BookStackSearchResult.php
Normal file
35
src/ApiResource/BookStackSearchResult.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\ApiResource;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\ApiResource;
|
||||||
|
use ApiPlatform\Metadata\GetCollection;
|
||||||
|
use App\State\BookStackSearchResultProvider;
|
||||||
|
use Symfony\Component\Serializer\Attribute\Groups;
|
||||||
|
|
||||||
|
#[ApiResource(
|
||||||
|
operations: [
|
||||||
|
new GetCollection(
|
||||||
|
uriTemplate: '/tasks/{taskId}/bookstack/search',
|
||||||
|
normalizationContext: ['groups' => ['bookstack_search:read']],
|
||||||
|
provider: BookStackSearchResultProvider::class,
|
||||||
|
security: "is_granted('IS_AUTHENTICATED_FULLY')",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)]
|
||||||
|
final class BookStackSearchResult
|
||||||
|
{
|
||||||
|
#[Groups(['bookstack_search:read'])]
|
||||||
|
public int $id = 0;
|
||||||
|
|
||||||
|
#[Groups(['bookstack_search:read'])]
|
||||||
|
public string $type = '';
|
||||||
|
|
||||||
|
#[Groups(['bookstack_search:read'])]
|
||||||
|
public string $name = '';
|
||||||
|
|
||||||
|
#[Groups(['bookstack_search:read'])]
|
||||||
|
public string $url = '';
|
||||||
|
}
|
||||||
62
src/State/BookStackSearchResultProvider.php
Normal file
62
src/State/BookStackSearchResultProvider.php
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\State;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use ApiPlatform\State\ProviderInterface;
|
||||||
|
use App\ApiResource\BookStackSearchResult;
|
||||||
|
use App\Entity\Task;
|
||||||
|
use App\Exception\BookStackApiException;
|
||||||
|
use App\Service\BookStackApiService;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||||
|
|
||||||
|
final readonly class BookStackSearchResultProvider implements ProviderInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private BookStackApiService $bookStackApiService,
|
||||||
|
private EntityManagerInterface $em,
|
||||||
|
private RequestStack $requestStack,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
|
||||||
|
{
|
||||||
|
$taskId = $uriVariables['taskId'] ?? 0;
|
||||||
|
$task = $this->em->getRepository(Task::class)->find($taskId);
|
||||||
|
|
||||||
|
if (null === $task || null === $task->getProject()) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$shelfId = $task->getProject()->getBookstackShelfId();
|
||||||
|
if (null === $shelfId) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$request = $this->requestStack->getCurrentRequest();
|
||||||
|
$query = $request?->query->get('q', '') ?? '';
|
||||||
|
|
||||||
|
if ('' === trim($query)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$results = $this->bookStackApiService->searchInShelf($shelfId, $query);
|
||||||
|
} catch (BookStackApiException $e) {
|
||||||
|
throw new BadRequestHttpException($e->getMessage(), $e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_map(static function (array $item): BookStackSearchResult {
|
||||||
|
$dto = new BookStackSearchResult();
|
||||||
|
$dto->id = $item['id'];
|
||||||
|
$dto->type = $item['type'];
|
||||||
|
$dto->name = $item['name'];
|
||||||
|
$dto->url = $item['url'];
|
||||||
|
|
||||||
|
return $dto;
|
||||||
|
}, $results);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user