chill-bundles/src/Bundle/ChillPersonBundle/Controller/SocialWorkSocialActionApiController.php
2021-11-30 13:54:58 +01:00

53 lines
1.6 KiB
PHP

<?php
/**
* 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.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Controller;
use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Serializer\Model\Collection;
use Chill\PersonBundle\Repository\SocialWork\SocialIssueRepository;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use function count;
class SocialWorkSocialActionApiController extends ApiController
{
private PaginatorFactory $paginator;
private SocialIssueRepository $socialIssueRepository;
public function __construct(SocialIssueRepository $socialIssueRepository, PaginatorFactory $paginator)
{
$this->socialIssueRepository = $socialIssueRepository;
$this->paginator = $paginator;
}
public function listBySocialIssueApi($id, Request $request)
{
$socialIssue = $this->socialIssueRepository
->find($id);
if (null === $socialIssue) {
throw $this->createNotFoundException('socialIssue not found');
}
$socialActions = $socialIssue->getRecursiveSocialActions();
$pagination = $this->paginator->create(count($socialActions));
// max one page
$pagination->setItemsPerPage(count($socialActions));
$collection = new Collection($socialActions, $pagination);
return $this->json($collection, JsonResponse::HTTP_OK, [], ['groups' => ['read']]);
}
}