refs #485. Organizational unit delete funcionality
parent
208394181c
commit
230faaebd3
|
@ -25,6 +25,12 @@ resources:
|
|||
groups: ['organizational-unit:patch' ]
|
||||
ApiPlatform\Metadata\Post: ~
|
||||
ApiPlatform\Metadata\Delete: ~
|
||||
change_parent:
|
||||
class: ApiPlatform\Metadata\Post
|
||||
method: POST
|
||||
input: false
|
||||
uriTemplate: /organizational-units/{uuid}/change-parent
|
||||
controller: App\Controller\OrganizationalUnitChangeParentAction
|
||||
|
||||
properties:
|
||||
App\Entity\OrganizationalUnit:
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
*/
|
||||
final class Version20240701123613 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
// this up() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('ALTER TABLE client DROP FOREIGN KEY FK_C7440455FB84408A');
|
||||
$this->addSql('ALTER TABLE client ADD CONSTRAINT FK_C7440455FB84408A FOREIGN KEY (organizational_unit_id) REFERENCES organizational_unit (id) ON DELETE CASCADE');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('ALTER TABLE client DROP FOREIGN KEY FK_C7440455FB84408A');
|
||||
$this->addSql('ALTER TABLE client ADD CONSTRAINT FK_C7440455FB84408A FOREIGN KEY (organizational_unit_id) REFERENCES organizational_unit (id)');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Dto\Input\OrganizationalUnitInput;
|
||||
use App\Entity\OrganizationalUnit;
|
||||
use App\Handler\OrganizationalUnitChangeParentHandler;
|
||||
use App\Repository\OrganizationalUnitRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
|
||||
class OrganizationalUnitChangeParentAction extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly OrganizationalUnitChangeParentHandler $organizationalUnitChangeParentHandler
|
||||
)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function __invoke(OrganizationalUnit $organizationalUnit): OrganizationalUnitInput
|
||||
{
|
||||
$this->organizationalUnitChangeParentHandler->handle($organizationalUnit);
|
||||
|
||||
return new OrganizationalUnitInput($organizationalUnit);
|
||||
}
|
||||
}
|
|
@ -13,13 +13,13 @@ final class ClientOutput extends AbstractOutput
|
|||
{
|
||||
CONST string TYPE = 'client';
|
||||
|
||||
#[Groups(['client:read'])]
|
||||
#[Groups(['client:read', 'organizational-unit:read'])]
|
||||
public string $name;
|
||||
|
||||
#[Groups(['client:read'])]
|
||||
#[Groups(['client:read', 'organizational-unit:read'])]
|
||||
public string $type = self::TYPE;
|
||||
|
||||
#[Groups(['client:read'])]
|
||||
#[Groups(['client:read', 'organizational-unit:read'])]
|
||||
public ?string $serialNumber = '';
|
||||
|
||||
#[Groups(['client:read'])]
|
||||
|
@ -53,7 +53,7 @@ final class ClientOutput extends AbstractOutput
|
|||
$this->serialNumber = $client->getSerialNumber();
|
||||
$this->netiface = $client->getNetiface();
|
||||
|
||||
if ($client->getOrganizationalUnit()) {
|
||||
if ($this->organizationalUnit && $client->getOrganizationalUnit()) {
|
||||
$this->organizationalUnit = new OrganizationalUnitOutput($client->getOrganizationalUnit());
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace App\Dto\Output;
|
|||
|
||||
use ApiPlatform\Metadata\ApiProperty;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use App\Entity\Client;
|
||||
use App\Entity\OrganizationalUnit;
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
|
||||
|
@ -62,7 +63,10 @@ final class OrganizationalUnitOutput extends AbstractOutput
|
|||
$this->capacity = $organizationalUnit->getCapacity();
|
||||
$this->type = $organizationalUnit->getType();
|
||||
$this->networkSettings = $organizationalUnit->getNetworkSettings() ? new NetworkSettingsOutput($organizationalUnit->getNetworkSettings()) : null;
|
||||
$this->clients = $organizationalUnit->getClients()->toArray();
|
||||
|
||||
$this->clients = $organizationalUnit->getClients()->map(
|
||||
fn(Client $client) => new ClientOutput($client)
|
||||
)->toArray();
|
||||
|
||||
if ($organizationalUnit->getParent()) {
|
||||
$this->parent = new self($organizationalUnit->getParent());
|
||||
|
|
|
@ -36,6 +36,7 @@ class Client extends AbstractEntity
|
|||
private ?string $status = null;
|
||||
|
||||
#[ORM\ManyToOne(inversedBy: 'clients')]
|
||||
#[ORM\JoinColumn( onDelete: 'CASCADE')]
|
||||
private ?OrganizationalUnit $organizationalUnit = null;
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace App\Handler;
|
||||
|
||||
use App\Entity\OrganizationalUnit;
|
||||
use App\Repository\OrganizationalUnitRepository;
|
||||
|
||||
readonly class OrganizationalUnitChangeParentHandler
|
||||
{
|
||||
public function __construct(
|
||||
private OrganizationalUnitRepository $organizationalUnitRepository
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function handle(OrganizationalUnit $organizationalUnit): OrganizationalUnit
|
||||
{
|
||||
$parent = $organizationalUnit->getParent();
|
||||
|
||||
if (!$parent) {
|
||||
return throw new \InvalidArgumentException('The organizational unit has no parent.');
|
||||
}
|
||||
|
||||
foreach ($organizationalUnit->getOrganizationalUnits() as $child) {
|
||||
$child->setParent($parent);
|
||||
$this->organizationalUnitRepository->save($child);
|
||||
}
|
||||
|
||||
$this->organizationalUnitRepository->delete($organizationalUnit);
|
||||
|
||||
return $organizationalUnit;
|
||||
}
|
||||
}
|
|
@ -30,7 +30,7 @@ class UserProvider implements ProviderInterface
|
|||
return $this->provideCollection($operation, $uriVariables, $context);
|
||||
case $operation instanceof Patch:
|
||||
case $operation instanceof Put:
|
||||
return $this->provideInput($operation, $uriVariables, $context);
|
||||
return $this->provideInput($operation, $uriVariables, $context);
|
||||
case $operation instanceof Get:
|
||||
return $this->provideItem($operation, $uriVariables, $context);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue