42 lines
1.4 KiB
PHP
42 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Dto\Input\ChangeOrganizationalUnitInput;
|
|
use App\Entity\Client;
|
|
use App\Repository\ClientRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class ChangeOrganizationalUnitAction extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private readonly ClientRepository $clientRepository,
|
|
private readonly EntityManagerInterface $entityManager
|
|
)
|
|
{
|
|
}
|
|
|
|
public function __invoke(ChangeOrganizationalUnitInput $input): JsonResponse
|
|
{
|
|
foreach ($input->clients as $client) {
|
|
/** @var Client $client */
|
|
$clientEntity = $this->clientRepository->find($client->getEntity()->getId());
|
|
if (!$clientEntity) {
|
|
throw new NotFoundHttpException('Client not found');
|
|
}
|
|
|
|
$organizationalUnit = $input->organizationalUnit->getEntity();
|
|
$clientEntity->setOrganizationalUnit($organizationalUnit);
|
|
|
|
$this->entityManager->persist($clientEntity);
|
|
}
|
|
|
|
$this->entityManager->flush();
|
|
|
|
return new JsonResponse( data: 'Clients updated successfully', status: Response::HTTP_OK);
|
|
}
|
|
} |