refs #423. New table and Endpoint Menu

pull/7/head
Manuel Aranda Rosales 2024-06-13 15:15:01 +02:00
parent 6be6e67797
commit 58d075ee46
16 changed files with 533 additions and 14 deletions

View File

@ -0,0 +1,30 @@
resources:
App\Entity\Menu:
processor: App\State\Processor\MenuProcessor
input: App\Dto\Input\MenuInput
output: App\Dto\Output\MenuOutput
normalization_context:
groups: ['default', 'menu:read']
denormalization_context:
groups: ['menu:write']
operations:
ApiPlatform\Metadata\GetCollection:
provider: App\State\Provider\MenuProvider
filters:
- 'api_platform.filter.menu.order'
- 'api_platform.filter.menu.search'
ApiPlatform\Metadata\Get:
provider: App\State\Provider\MenuProvider
ApiPlatform\Metadata\Put:
provider: App\State\Provider\MenuProvider
ApiPlatform\Metadata\Patch:
provider: App\State\Provider\MenuProvider
ApiPlatform\Metadata\Post: ~
ApiPlatform\Metadata\Delete: ~
properties:
App\Entity\Menu:
id:
identifier: false
uuid:
identifier: true

View File

@ -1,6 +1,5 @@
resources:
App\Entity\User:
security: 'is_granted("ROLE_SUPER_ADMIN")'
input: App\Dto\Input\UserInput
output: App\Dto\Output\UserOutput
processor: App\State\Processor\UserProcessor

View File

@ -54,3 +54,8 @@ services:
bind:
$collectionProvider: '@api_platform.doctrine.orm.state.collection_provider'
$itemProvider: '@api_platform.doctrine.orm.state.item_provider'
App\State\Provider\MenuProvider:
bind:
$collectionProvider: '@api_platform.doctrine.orm.state.collection_provider'
$itemProvider: '@api_platform.doctrine.orm.state.item_provider'

View File

@ -0,0 +1,43 @@
<?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 Version20240613090444 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('CREATE TABLE menu (id INT AUTO_INCREMENT NOT NULL, uuid CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', migration_id VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, created_by VARCHAR(255) DEFAULT NULL, updated_by VARCHAR(255) DEFAULT NULL, title VARCHAR(255) NOT NULL, resolution VARCHAR(255) NOT NULL, comments VARCHAR(255) DEFAULT NULL, public_url VARCHAR(255) DEFAULT NULL, private_url VARCHAR(255) DEFAULT NULL, name VARCHAR(255) NOT NULL, UNIQUE INDEX UNIQ_7D053A93D17F50A6 (uuid), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE client ADD menu_id INT DEFAULT NULL, CHANGE name name VARCHAR(255) NOT NULL');
$this->addSql('ALTER TABLE client ADD CONSTRAINT FK_C7440455CCD7E912 FOREIGN KEY (menu_id) REFERENCES menu (id)');
$this->addSql('CREATE INDEX IDX_C7440455CCD7E912 ON client (menu_id)');
$this->addSql('ALTER TABLE hardware CHANGE name name VARCHAR(255) NOT NULL');
$this->addSql('ALTER TABLE organizational_unit CHANGE name name VARCHAR(255) NOT NULL');
$this->addSql('ALTER TABLE user_group CHANGE name name VARCHAR(255) NOT NULL');
}
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_C7440455CCD7E912');
$this->addSql('DROP TABLE menu');
$this->addSql('ALTER TABLE hardware CHANGE name name VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE user_group CHANGE name name VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE organizational_unit CHANGE name name VARCHAR(255) DEFAULT NULL');
$this->addSql('DROP INDEX IDX_C7440455CCD7E912 ON client');
$this->addSql('ALTER TABLE client DROP menu_id, CHANGE name name VARCHAR(255) DEFAULT NULL');
}
}

View File

@ -4,6 +4,7 @@ namespace App\Dto\Input;
use ApiPlatform\Metadata\ApiProperty;
use App\Dto\Output\HardwareProfileOutput;
use App\Dto\Output\MenuOutput;
use App\Dto\Output\OrganizationalUnitOutput;
use App\Entity\Client;
use Symfony\Component\Serializer\Annotation\Groups;
@ -50,6 +51,11 @@ final class ClientInput
#[ApiProperty(description: 'The hardware profile of the client')]
public ?HardwareProfileOutput $hardwareProfile = null;
#[Assert\NotNull]
#[Groups(['client:write'])]
#[ApiProperty(description: 'The menu of the client')]
public ?MenuOutput $menu = null;
public function __construct(?Client $client = null)
{
if (!$client) {
@ -61,6 +67,7 @@ final class ClientInput
$this->netiface = $client->getNetiface();
$this->organizationalUnit = new OrganizationalUnitOutput($client->getOrganizationalUnit());
$this->hardwareProfile = new HardwareProfileOutput($client->getHardwareProfile());
$this->menu = new MenuOutput($client->getMenu());
$this->netDriver = $client->getNetDriver();
$this->mac = $client->getMac();
$this->ip = $client->getIp();
@ -78,6 +85,7 @@ final class ClientInput
$client->setNetiface($this->netiface);
$client->setOrganizationalUnit($this->organizationalUnit->getEntity());
$client->setHardwareProfile($this->hardwareProfile->getEntity());
$client->setMenu($this->menu->getEntity());
$client->setNetDriver($this->netDriver);
$client->setMac($this->mac);
$client->setIp($this->ip);

View File

@ -0,0 +1,69 @@
<?php
namespace App\Dto\Input;
use ApiPlatform\Metadata\ApiProperty;
use App\Dto\Output\OrganizationalUnitOutput;
use App\Entity\HardwareProfile;
use App\Entity\Menu;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
final class MenuInput
{
#[Assert\NotNull()]
#[Groups(['menu:write'])]
#[ApiProperty(description: 'The name of the menu', example: "Menu 1")]
public ?string $name = null;
#[Groups(['menu:write'])]
#[ApiProperty(description: 'The title of the menu', example: "Menu 1 title")]
public ?string $title = null;
#[Groups(['menu:write'])]
#[ApiProperty(description: 'Comments of the menu', example: "Menu 1 comments")]
public ?string $comments = null;
#[Assert\NotNull()]
#[Groups(['menu:write'])]
#[ApiProperty(description: 'The resolution of the menu', example: "1920x1080")]
public ?string $resolution = null;
#[Groups(['menu:write'])]
#[ApiProperty(description: 'The public url of the menu', example: "http://example.com")]
public ?string $publicUrl = null;
#[Groups(['menu:write'])]
#[ApiProperty(description: 'The private url of the menu', example: "http://example.com")]
public ?string $privateUrl = null;
public function __construct(?Menu $menu = null)
{
if (!$menu) {
return;
}
$this->name = $menu->getName();
$this->title = $menu->getTitle();
$this->comments = $menu->getComments();
$this->resolution = $menu->getResolution();
$this->publicUrl = $menu->getPublicUrl();
$this->privateUrl = $menu->getPrivateUrl();
}
public function createOrUpdateEntity(?Menu $menu = null): Menu
{
if (!$menu) {
$menu = new Menu();
}
$menu->setName($this->name);
$menu->setTitle($this->title);
$menu->setComments($this->comments);
$menu->setResolution($this->resolution);
$menu->setPublicUrl($this->publicUrl);
$menu->setPrivateUrl($this->privateUrl);
return $menu;
}
}

View File

@ -25,10 +25,13 @@ final class ClientOutput extends AbstractOutput
public ?HardwareProfileOutput $hardwareProfile = null;
#[Groups(['client:read'])]
public \DateTime $createAt;
public ?MenuOutput $menu = null;
#[Groups(['client:read'])]
public ?string $createBy = null;
public \DateTime $createdAt;
#[Groups(['client:read'])]
public ?string $createdBy = null;
public function __construct(Client $client)
{
@ -43,7 +46,10 @@ final class ClientOutput extends AbstractOutput
if($client->getHardwareProfile()) {
$this->hardwareProfile = new HardwareProfileOutput($client->getHardwareProfile());
}
$this->createAt = $client->getCreatedAt();
$this->createBy = $client->getCreatedBy();
if($client->getMenu()) {
$this->menu = new MenuOutput($client->getMenu());
}
$this->createdAt = $client->getCreatedAt();
$this->createdBy = $client->getCreatedBy();
}
}

View File

@ -19,10 +19,10 @@ final class HardwareProfileOutput extends AbstractOutput
public ?OrganizationalUnitOutput $organizationalUnit = null;
#[Groups(['hardware-profile:read'])]
public \DateTime $createAt;
public \DateTime $createdAt;
#[Groups(['hardware-profile:read'])]
public ?string $createBy = null;
public ?string $createdBy = null;
public function __construct(HardwareProfile $hardwareProfile)
{
@ -33,7 +33,7 @@ final class HardwareProfileOutput extends AbstractOutput
if($hardwareProfile->getOrganizationalUnit()) {
$this->organizationalUnit = new OrganizationalUnitOutput($hardwareProfile->getOrganizationalUnit());
}
$this->createAt = $hardwareProfile->getCreatedAt();
$this->createBy = $hardwareProfile->getCreatedBy();
$this->createdAt = $hardwareProfile->getCreatedAt();
$this->createdBy = $hardwareProfile->getCreatedBy();
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Dto\Output;
use ApiPlatform\Metadata\Get;
use App\Entity\HardwareProfile;
use App\Entity\Menu;
use Symfony\Component\Serializer\Annotation\Groups;
#[Get(shortName: 'Menu')]
final class MenuOutput extends AbstractOutput
{
#[Groups(['menu:read'])]
public string $name;
#[Groups(['menu:read'])]
public ?string $title = null;
#[Groups(['menu:read'])]
public string $resolution;
#[Groups(['menu:read'])]
public ?string $comments = null;
#[Groups(['menu:read'])]
public ?string $publicUrl = null;
#[Groups(['menu:read'])]
public ?string $privateUrl = null;
#[Groups(['menu:read'])]
public \DateTime $createdAt;
#[Groups(['menu:read'])]
public ?string $createdBy = null;
public function __construct(Menu $menu)
{
parent::__construct($menu);
$this->name = $menu->getName();
$this->title = $menu->getTitle();
$this->resolution = $menu->getResolution();
$this->comments = $menu->getComments();
$this->publicUrl = $menu->getPublicUrl();
$this->privateUrl = $menu->getPrivateUrl();
$this->createdAt = $menu->getCreatedAt();
$this->createdBy = $menu->getCreatedBy();
}
}

View File

@ -41,10 +41,10 @@ final class NetworkSettingsOutput
public ?string $mcastMode = null;
#[Groups(['organizational-unit:read'])]
public \DateTime $createAt;
public \DateTime $createdAt;
#[Groups(['organizational-unit:read'])]
public ?string $createBy = null;
public ?string $createdBy = null;
public function __construct(NetworkSettings $networkSettings)
{
@ -59,7 +59,7 @@ final class NetworkSettingsOutput
$this->mcastSpeed = $networkSettings->getMcastSpeed();
$this->mcastPort = $networkSettings->getMcastPort();
$this->mcastMode = $networkSettings->getMcastMode();
$this->createAt = $networkSettings->getCreatedAt();
$this->createBy = $networkSettings->getCreatedBy();
$this->createdAt = $networkSettings->getCreatedAt();
$this->createdBy = $networkSettings->getCreatedBy();
}
}

View File

@ -36,7 +36,6 @@ final class OrganizationalUnitOutput extends AbstractOutput
#[Groups(['organizational-unit:read'])]
public ?string $createdBy = null;
public function __construct(OrganizationalUnit $organizationalUnit)
{
parent::__construct($organizationalUnit);

View File

@ -34,6 +34,9 @@ class Client extends AbstractEntity
#[ORM\ManyToOne(inversedBy: 'clients')]
private ?HardwareProfile $hardwareProfile = null;
#[ORM\ManyToOne(inversedBy: 'clients')]
private ?Menu $menu = null;
public function getSerialNumber(): ?string
{
return $this->serialNumber;
@ -129,4 +132,16 @@ class Client extends AbstractEntity
return $this;
}
public function getMenu(): ?Menu
{
return $this->menu;
}
public function setMenu(?Menu $menu): static
{
$this->menu = $menu;
return $this;
}
}

131
src/Entity/Menu.php 100644
View File

@ -0,0 +1,131 @@
<?php
namespace App\Entity;
use App\Repository\MenuRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: MenuRepository::class)]
class Menu extends AbstractEntity
{
use NameableTrait;
#[ORM\Column(length: 255)]
private ?string $title = null;
#[ORM\Column(length: 255)]
private ?string $resolution = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $comments = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $publicUrl = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $privateUrl = null;
/**
* @var Collection<int, Client>
*/
#[ORM\OneToMany(mappedBy: 'menu', targetEntity: Client::class)]
private Collection $clients;
public function __construct()
{
parent::__construct();
$this->clients = new ArrayCollection();
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): static
{
$this->title = $title;
return $this;
}
public function getResolution(): ?string
{
return $this->resolution;
}
public function setResolution(string $resolution): static
{
$this->resolution = $resolution;
return $this;
}
public function getComments(): ?string
{
return $this->comments;
}
public function setComments(?string $comments): static
{
$this->comments = $comments;
return $this;
}
public function getPublicUrl(): ?string
{
return $this->publicUrl;
}
public function setPublicUrl(?string $publicUrl): static
{
$this->publicUrl = $publicUrl;
return $this;
}
public function getPrivateUrl(): ?string
{
return $this->privateUrl;
}
public function setPrivateUrl(?string $privateUrl): static
{
$this->privateUrl = $privateUrl;
return $this;
}
/**
* @return Collection<int, Client>
*/
public function getClients(): Collection
{
return $this->clients;
}
public function addClient(Client $client): static
{
if (!$this->clients->contains($client)) {
$this->clients->add($client);
$client->setMenu($this);
}
return $this;
}
public function removeClient(Client $client): static
{
if ($this->clients->removeElement($client)) {
// set the owning side to null (unless already changed)
if ($client->getMenu() === $this) {
$client->setMenu(null);
}
}
return $this;
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace App\Repository;
use App\Entity\Menu;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Menu>
*/
class MenuRepository extends AbstractRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Menu::class);
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace App\State\Processor;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\Validator\ValidatorInterface;
use App\Dto\Input\MenuInput;
use App\Dto\Input\UserGroupInput;
use App\Dto\Output\MenuOutput;
use App\Dto\Output\UserGroupOutput;
use App\Repository\MenuRepository;
use App\Repository\UserGroupRepository;
class MenuProcessor implements ProcessorInterface
{
public function __construct(
private readonly MenuRepository $menuRepository,
private readonly ValidatorInterface $validator
)
{
}
/**
* @throws \Exception
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): MenuOutput|null
{
switch ($operation){
case $operation instanceof Post:
case $operation instanceof Put:
case $operation instanceof Patch:
return $this->processCreateOrUpdate($data, $operation, $uriVariables, $context);
case $operation instanceof Delete:
return $this->processDelete($data, $operation, $uriVariables, $context);
}
}
/**
* @throws \Exception
*/
private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): MenuOutput
{
if (!($data instanceof MenuInput)) {
throw new \Exception(sprintf('data is not instance of %s', UserGroupInput::class));
}
$entity = null;
if (isset($uriVariables['uuid'])) {
$entity = $this->menuRepository->findOneByUuid($uriVariables['uuid']);
}
$userGroup = $data->createOrUpdateEntity($entity);
$this->validator->validate($userGroup);
$this->menuRepository->save($userGroup);
return new MenuOutput($userGroup);
}
private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null
{
$user = $this->menuRepository->findOneByUuid($uriVariables['uuid']);
$this->menuRepository->delete($user);
return null;
}
}

View File

@ -0,0 +1,75 @@
<?php
namespace App\State\Provider;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\Pagination\TraversablePaginator;
use ApiPlatform\State\ProviderInterface;
use App\Dto\Input\ClientInput;
use App\Dto\Input\HardwareInput;
use App\Dto\Input\MenuInput;
use App\Dto\Output\ClientOutput;
use App\Dto\Output\HardwareOutput;
use App\Dto\Output\MenuOutput;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class MenuProvider implements ProviderInterface
{
public function __construct(
private readonly ProviderInterface $collectionProvider,
private readonly ProviderInterface $itemProvider
)
{
}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
switch ($operation){
case $operation instanceof GetCollection:
return $this->provideCollection($operation, $uriVariables, $context);
case $operation instanceof Patch:
case $operation instanceof Put:
return $this->provideInput($operation, $uriVariables, $context);
case $operation instanceof Get:
return $this->provideItem($operation, $uriVariables, $context);
}
}
private function provideCollection(Operation $operation, array $uriVariables = [], array $context = []): object
{
$paginator = $this->collectionProvider->provide($operation, $uriVariables, $context);
$items = new \ArrayObject();
foreach ($paginator->getIterator() as $item){
$items[] = new MenuOutput($item);
}
return new TraversablePaginator($items, $paginator->getCurrentPage(), $paginator->getItemsPerPage(), $paginator->getTotalItems());
}
public function provideItem(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$item = $this->itemProvider->provide($operation, $uriVariables, $context);
if (!$item) {
throw new NotFoundHttpException('Menu not found');
}
return new MenuOutput($item);
}
public function provideInput(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if (isset($uriVariables['uuid'])) {
$item = $this->itemProvider->provide($operation, $uriVariables, $context);
return $item !== null ? new MenuInput($item) : null;
}
return new MenuInput();
}
}