mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Added a new class StoredObjectVersionApiController in ChillDocGeneratorBundle which lists versions of a specified stored object. Corresponding unit test has been added as well. Made modifications in `StoredObject.php` to make the versions selectable. Also updated the API specifications to include a new GET route for retrieving versions.
65 lines
2.5 KiB
PHP
65 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\DocStoreBundle\Controller;
|
|
|
|
use Chill\DocStoreBundle\Entity\StoredObject;
|
|
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
|
|
use Chill\MainBundle\Pagination\PaginatorFactoryInterface;
|
|
use Chill\MainBundle\Serializer\Model\Collection;
|
|
use Doctrine\Common\Collections\Criteria;
|
|
use Doctrine\Common\Collections\Order;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
use Symfony\Component\Security\Core\Security;
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
|
|
|
final readonly class StoredObjectVersionApiController
|
|
{
|
|
public function __construct(
|
|
private PaginatorFactoryInterface $paginatorFactory,
|
|
private SerializerInterface $serializer,
|
|
private Security $security,
|
|
) {}
|
|
|
|
/**
|
|
* Lists the versions of the specified stored object.
|
|
*
|
|
* @param StoredObject $storedObject the stored object whose versions are to be listed
|
|
*
|
|
* @return JsonResponse a JSON response containing the serialized versions of the stored object, encapsulated in a collection
|
|
*
|
|
* @throws AccessDeniedHttpException if the user is not allowed to see the stored object
|
|
*/
|
|
#[Route('/api/1.0/doc-store/stored-object/{uuid}/versions', name: 'chill_doc_store_stored_object_versions_list')]
|
|
public function listVersions(StoredObject $storedObject): JsonResponse
|
|
{
|
|
if (!$this->security->isGranted(StoredObjectRoleEnum::SEE->value, $storedObject)) {
|
|
throw new AccessDeniedHttpException('not allowed to see this stored object');
|
|
}
|
|
|
|
$total = $storedObject->getVersions()->count();
|
|
$paginator = $this->paginatorFactory->create($total);
|
|
|
|
$criteria = Criteria::create();
|
|
$criteria->orderBy(['id' => Order::Ascending]);
|
|
$criteria->setMaxResults($paginator->getItemsPerPage())->setFirstResult($paginator->getCurrentPageFirstItemNumber());
|
|
$items = $storedObject->getVersions()->matching($criteria);
|
|
|
|
return new JsonResponse(
|
|
$this->serializer->serialize(new Collection($items, $paginator), 'json', [AbstractNormalizer::GROUPS => ['read']]),
|
|
json: true
|
|
);
|
|
}
|
|
}
|