diff --git a/config/api_platform/Command.yaml b/config/api_platform/Command.yaml index fd204fe..f8480ce 100644 --- a/config/api_platform/Command.yaml +++ b/config/api_platform/Command.yaml @@ -13,6 +13,7 @@ resources: filters: - 'api_platform.filter.command.order' - 'api_platform.filter.command.search' + - 'api_platform.filter.command.boolean' ApiPlatform\Metadata\Get: provider: App\State\Provider\CommandProvider diff --git a/config/api_platform/CommandGroup.yaml b/config/api_platform/CommandGroup.yaml new file mode 100644 index 0000000..6e0cda9 --- /dev/null +++ b/config/api_platform/CommandGroup.yaml @@ -0,0 +1,32 @@ +resources: + App\Entity\CommandGroup: + processor: App\State\Processor\CommandGroupProcessor + input: App\Dto\Input\CommandGroupInput + output: App\Dto\Output\CommandGroupOutput + normalizationContext: + groups: ['default', 'command-group:read'] + denormalizationContext: + groups: ['command-group:write'] + operations: + ApiPlatform\Metadata\GetCollection: + provider: App\State\Provider\CommandGroupProvider + filters: + - 'api_platform.filter.command.order' + - 'api_platform.filter.command.search' + - 'api_platform.filter.command.boolean' + + ApiPlatform\Metadata\Get: + provider: App\State\Provider\CommandGroupProvider + ApiPlatform\Metadata\Put: + provider: App\State\Provider\CommandGroupProvider + ApiPlatform\Metadata\Patch: + provider: App\State\Provider\CommandGroupProvider + ApiPlatform\Metadata\Post: ~ + ApiPlatform\Metadata\Delete: ~ + +properties: + App\Entity\CommandGroup: + id: + identifier: false + uuid: + identifier: true \ No newline at end of file diff --git a/config/services.yaml b/config/services.yaml index 99d1d6c..be56306 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -106,3 +106,8 @@ services: bind: $collectionProvider: '@api_platform.doctrine.orm.state.collection_provider' $itemProvider: '@api_platform.doctrine.orm.state.item_provider' + + App\State\Provider\CommandGroupProvider: + bind: + $collectionProvider: '@api_platform.doctrine.orm.state.collection_provider' + $itemProvider: '@api_platform.doctrine.orm.state.item_provider' \ No newline at end of file diff --git a/config/services/api_platform.yaml b/config/services/api_platform.yaml index d8f0fa4..466d458 100644 --- a/config/services/api_platform.yaml +++ b/config/services/api_platform.yaml @@ -11,6 +11,23 @@ services: arguments: [ { 'id': 'exact', 'name': 'partial', 'serialNumber': 'exact', organizationalUnit.id: 'exact' } ] tags: [ 'api_platform.filter' ] + api_platform.filter.command.order: + parent: 'api_platform.doctrine.orm.order_filter' + arguments: + $properties: { 'id': ~, 'name': ~ } + $orderParameterName: 'order' + tags: [ 'api_platform.filter' ] + + api_platform.filter.command.search: + parent: 'api_platform.doctrine.orm.search_filter' + arguments: [ { 'id': 'exact', 'name': 'exact'} ] + tags: [ 'api_platform.filter' ] + + api_platform.filter.command.boolean: + parent: 'api_platform.doctrine.orm.boolean_filter' + arguments: [ { 'enabled': ~ } ] + tags: [ 'api_platform.filter' ] + api_platform.filter.hardware.order: parent: 'api_platform.doctrine.orm.order_filter' arguments: diff --git a/src/Dto/Input/CommandGroupInput.php b/src/Dto/Input/CommandGroupInput.php new file mode 100644 index 0000000..2a3fe64 --- /dev/null +++ b/src/Dto/Input/CommandGroupInput.php @@ -0,0 +1,83 @@ +name = $commandGroup->getName(); + + if ($commandGroup->getCommands()) { + foreach ($commandGroup->getCommands() as $command) { + $this->commands[] = new CommandOutput($command); + } + } + + $this->position = $commandGroup->getPosition(); + $this->enabled = $commandGroup->isEnabled(); + } + + public function createOrUpdateEntity(?CommandGroup $commandGroup = null): CommandGroup + { + if (!$commandGroup) { + $commandGroup = new CommandGroup(); + } + + $commandGroup->setName($this->name); + + foreach ($this->commands as $command) { + $commandsToAdd[] = $command->getEntity(); + } + + $commandGroup->setCommands( $commandsToAdd ?? [] ); + $commandGroup->setPosition($this->position); + + return $commandGroup; + } +} diff --git a/src/Dto/Input/CommandInput.php b/src/Dto/Input/CommandInput.php index 7ddf219..10697d8 100644 --- a/src/Dto/Input/CommandInput.php +++ b/src/Dto/Input/CommandInput.php @@ -33,6 +33,13 @@ final class CommandInput )] public ?bool $readOnly = false; + #[Groups(['command:write'])] + #[ApiProperty( + description: 'Esta activo?', + example: 'true', + )] + public ?bool $enabled = true; + #[Groups(['command:write'])] #[ApiProperty( description: 'Los comentarios del comando', @@ -48,6 +55,7 @@ final class CommandInput $this->name = $command->getName(); $this->script = $command->getScript(); + $this->enabled = $command->isEnabled(); $this->readOnly = $command->isReadOnly(); $this->comments = $command->getComments(); } @@ -60,6 +68,7 @@ final class CommandInput $command->setName($this->name); $command->setScript($this->script); + $command->setEnabled($this->enabled); $command->setReadOnly($this->readOnly); $command->setComments($this->comments); diff --git a/src/Dto/Output/CommandGroupOutput.php b/src/Dto/Output/CommandGroupOutput.php new file mode 100644 index 0000000..636ca8e --- /dev/null +++ b/src/Dto/Output/CommandGroupOutput.php @@ -0,0 +1,42 @@ +name = $commandGroup->getName(); + + $this->commands = $commandGroup->getCommands()->map( + fn(Command $command) => new CommandOutput($command) + )->toArray(); + + $this->position = $commandGroup->getPosition(); + $this->createdAt = $commandGroup->getCreatedAt(); + $this->createdBy = $commandGroup->getCreatedBy(); + } +} \ No newline at end of file diff --git a/src/Dto/Output/CommandOutput.php b/src/Dto/Output/CommandOutput.php index 75f7d5e..5d92345 100644 --- a/src/Dto/Output/CommandOutput.php +++ b/src/Dto/Output/CommandOutput.php @@ -18,6 +18,9 @@ final class CommandOutput extends AbstractOutput #[Groups(['command:read'])] public ?bool $readOnly = false; + #[Groups(['command:read'])] + public ?bool $enabled = true; + #[Groups(['command:read'])] public ?string $comments = ''; @@ -34,6 +37,7 @@ final class CommandOutput extends AbstractOutput $this->name = $command->getName(); $this->script = $command->getScript(); $this->readOnly = $command->isReadOnly(); + $this->enabled = $command->isEnabled(); $this->comments = $command->getComments(); $this->createdAt = $command->getCreatedAt(); $this->createdBy = $command->getCreatedBy(); diff --git a/src/Entity/CommandGroup.php b/src/Entity/CommandGroup.php index aa1173d..f0061c3 100644 --- a/src/Entity/CommandGroup.php +++ b/src/Entity/CommandGroup.php @@ -36,6 +36,18 @@ class CommandGroup extends AbstractEntity return $this->commands; } + public function setCommands(array $commands): static + { + $this->commands->clear(); + + foreach ($commands as $command){ + $this->addCommand($command); + } + + return $this; + } + + public function addCommand(Command $command): static { if (!$this->commands->contains($command)) { diff --git a/src/Repository/CommandGroupRepository.php b/src/Repository/CommandGroupRepository.php index b669b14..f8cf8b2 100644 --- a/src/Repository/CommandGroupRepository.php +++ b/src/Repository/CommandGroupRepository.php @@ -9,35 +9,10 @@ use Doctrine\Persistence\ManagerRegistry; /** * @extends ServiceEntityRepository */ -class CommandGroupRepository extends ServiceEntityRepository +class CommandGroupRepository extends AbstractRepository { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, CommandGroup::class); } - - // /** - // * @return CommandGroup[] Returns an array of CommandGroup objects - // */ - // public function findByExampleField($value): array - // { - // return $this->createQueryBuilder('c') - // ->andWhere('c.exampleField = :val') - // ->setParameter('val', $value) - // ->orderBy('c.id', 'ASC') - // ->setMaxResults(10) - // ->getQuery() - // ->getResult() - // ; - // } - - // public function findOneBySomeField($value): ?CommandGroup - // { - // return $this->createQueryBuilder('c') - // ->andWhere('c.exampleField = :val') - // ->setParameter('val', $value) - // ->getQuery() - // ->getOneOrNullResult() - // ; - // } } diff --git a/src/State/Processor/CommandGroupProcessor.php b/src/State/Processor/CommandGroupProcessor.php new file mode 100644 index 0000000..65ffacd --- /dev/null +++ b/src/State/Processor/CommandGroupProcessor.php @@ -0,0 +1,69 @@ +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 = []): CommandGroupOutput + { + if (!($data instanceof CommandGroupInput)) { + throw new \Exception(sprintf('data is not instance of %s', CommandGroupInput::class)); + } + + $entity = null; + if (isset($uriVariables['uuid'])) { + $entity = $this->commandGroupRepository->findOneByUuid($uriVariables['uuid']); + } + + $command = $data->createOrUpdateEntity($entity); + $this->validator->validate($command); + $this->commandGroupRepository->save($command); + + return new CommandGroupOutput($command); + } + + private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null + { + $user = $this->commandGroupRepository->findOneByUuid($uriVariables['uuid']); + $this->commandGroupRepository->delete($user); + + return null; + } +} diff --git a/src/State/Provider/CommandGroupProvider.php b/src/State/Provider/CommandGroupProvider.php new file mode 100644 index 0000000..c3092db --- /dev/null +++ b/src/State/Provider/CommandGroupProvider.php @@ -0,0 +1,72 @@ +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 CommandGroupOutput($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('Command group not found'); + } + + return new CommandGroupOutput($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 CommandGroupInput($item) : null; + } + + return new CommandGroupInput(); + } +} diff --git a/src/State/Provider/CommandProvider.php b/src/State/Provider/CommandProvider.php index ac2de21..b5854b1 100644 --- a/src/State/Provider/CommandProvider.php +++ b/src/State/Provider/CommandProvider.php @@ -52,7 +52,7 @@ readonly class CommandProvider implements ProviderInterface $item = $this->itemProvider->provide($operation, $uriVariables, $context); if (!$item) { - throw new NotFoundHttpException('Hardware profile not found'); + throw new NotFoundHttpException('Command not found'); } return new CommandOutput($item); diff --git a/translations/validators.en.yaml b/translations/validators.en.yaml index e5b6e69..9f0f516 100644 --- a/translations/validators.en.yaml +++ b/translations/validators.en.yaml @@ -8,6 +8,18 @@ validators: organizational_unit: not_null: 'The organizational unit should not be null.' + command: + name: + not_blank: 'The name should not be blank.' + script: + not_blank: 'The script should not be blank.' + + command_group: + name: + not_blank: 'The name should not be blank.' + position: + not_blank: 'The position should not be blank.' + view: name: not_blank: 'The name should not be blank.' diff --git a/translations/validators.es.yaml b/translations/validators.es.yaml index 9bf6d15..5a2984d 100644 --- a/translations/validators.es.yaml +++ b/translations/validators.es.yaml @@ -8,6 +8,18 @@ validators: organizational_unit: not_null: 'La unidad organizativa no debería estar vacía.' + command: + name: + not_blank: 'El nombre no debería estar vacío.' + script: + not_blank: 'El script no debería estar vacío.' + + command_group: + name: + not_blank: 'El nombre no debería estar vacío.' + position: + not_blank: 'La posición no debería estar vacía.' + view: name: not_blank: 'El nombre no debería estar vacío.'