diff --git a/config/api_platform/CommandTask.yaml b/config/api_platform/CommandTask.yaml new file mode 100644 index 0000000..d008252 --- /dev/null +++ b/config/api_platform/CommandTask.yaml @@ -0,0 +1,32 @@ +resources: + App\Entity\CommandTask: + processor: App\State\Processor\CommandTaskProcessor + input: App\Dto\Input\CommandTaskInput + output: App\Dto\Output\CommandTaskOutput + normalizationContext: + groups: ['default', 'command-task:read'] + denormalizationContext: + groups: ['command-task:write'] + operations: + ApiPlatform\Metadata\GetCollection: + provider: App\State\Provider\CommandTaskProvider + filters: + - 'api_platform.filter.command_task.order' + - 'api_platform.filter.command_task.search' + - 'api_platform.filter.command_task.boolean' + + ApiPlatform\Metadata\Get: + provider: App\State\Provider\CommandTaskProvider + ApiPlatform\Metadata\Put: + provider: App\State\Provider\CommandTaskProvider + ApiPlatform\Metadata\Patch: + provider: App\State\Provider\CommandTaskProvider + ApiPlatform\Metadata\Post: ~ + ApiPlatform\Metadata\Delete: ~ + +properties: + App\Entity\CommandTask: + id: + identifier: false + uuid: + identifier: true \ No newline at end of file diff --git a/config/services.yaml b/config/services.yaml index be56306..4c0ac26 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -108,6 +108,11 @@ services: $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' + + App\State\Provider\CommandTaskProvider: 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/src/Dto/Input/CommandTaskInput.php b/src/Dto/Input/CommandTaskInput.php new file mode 100644 index 0000000..dd6b89a --- /dev/null +++ b/src/Dto/Input/CommandTaskInput.php @@ -0,0 +1,96 @@ +getCommands()) { + foreach ($commandTask->getCommands() as $command) { + $this->commands[] = new CommandOutput($command); + } + } + + if ($commandTask->getCommandGroups()) { + foreach ($commandTask->getCommandGroups() as $commandGroup) { + $this->commandGroups[] = new CommandGroupOutput($commandGroup); + } + } + + $this->dateTime = $commandTask->getDatetime(); + $this->notes = $commandTask->getNotes(); + } + + public function createOrUpdateEntity(?CommandTask $commandTask = null): CommandTask + { + if (!$commandTask) { + $commandTask = new CommandTask(); + } + + foreach ($this->commands as $command) { + $commandsToAdd[] = $command->getEntity(); + } + + $commandTask->setCommands( $commandsToAdd ?? [] ); + + foreach ($this->commandGroups as $commandGroup) { + $commandGroupsToAdd[] = $commandGroup->getEntity(); + } + + $commandTask->setCommandGroups( $commandGroupsToAdd ?? [] ); + $commandTask->setDatetime($this->dateTime); + $commandTask->setStatus(CommandTaskStatus::PENDING); + $commandTask->setNotes($this->notes); + + return $commandTask; + } +} diff --git a/src/Dto/Output/CommandGroupOutput.php b/src/Dto/Output/CommandGroupOutput.php index 636ca8e..8e6b645 100644 --- a/src/Dto/Output/CommandGroupOutput.php +++ b/src/Dto/Output/CommandGroupOutput.php @@ -10,13 +10,13 @@ use Symfony\Component\Serializer\Annotation\Groups; #[Get(shortName: 'CommandGroup')] final class CommandGroupOutput extends AbstractOutput { - #[Groups(['command-group:read'])] + #[Groups(['command-group:read', 'command-task:read'])] public string $name; - #[Groups(['command-group:read'])] + #[Groups(['command-group:read', 'command-task:read'])] public array $commands = []; - #[Groups(['command-group:read'])] + #[Groups(['command-group:read', 'command-task:read'])] public ?int $position = null; #[Groups(['command-group:read'])] diff --git a/src/Dto/Output/CommandOutput.php b/src/Dto/Output/CommandOutput.php index 5d92345..b65ee8d 100644 --- a/src/Dto/Output/CommandOutput.php +++ b/src/Dto/Output/CommandOutput.php @@ -9,10 +9,10 @@ use Symfony\Component\Serializer\Annotation\Groups; #[Get(shortName: 'Command')] final class CommandOutput extends AbstractOutput { - #[Groups(['command:read'])] + #[Groups(['command:read', 'command-group:read', 'command-task:read'])] public string $name; - #[Groups(['command:read'])] + #[Groups(['command:read', 'command-group:read', 'command-task:read'])] public ?string $script = ''; #[Groups(['command:read'])] diff --git a/src/Dto/Output/CommandTaskOutput.php b/src/Dto/Output/CommandTaskOutput.php new file mode 100644 index 0000000..c5852fe --- /dev/null +++ b/src/Dto/Output/CommandTaskOutput.php @@ -0,0 +1,54 @@ +commands = $commandTask->getCommands()->map( + fn(Command $command) => new CommandOutput($command) + )->toArray(); + + $this->commandGroups = $commandTask->getCommandGroups()->map( + fn(CommandGroup $commandGroup) => new CommandGroupOutput($commandGroup) + )->toArray(); + + $this->dateTime = $commandTask->getDateTime(); + $this->notes = $commandTask->getNotes(); + $this->status = $commandTask->getStatus(); + $this->createdAt = $commandTask->getCreatedAt(); + $this->createdBy = $commandTask->getCreatedBy(); + } +} \ No newline at end of file diff --git a/src/Entity/CommandGroup.php b/src/Entity/CommandGroup.php index 30befcb..6cf5271 100644 --- a/src/Entity/CommandGroup.php +++ b/src/Entity/CommandGroup.php @@ -54,7 +54,6 @@ class CommandGroup extends AbstractEntity return $this; } - public function addCommand(Command $command): static { if (!$this->commands->contains($command)) { diff --git a/src/Entity/CommandTask.php b/src/Entity/CommandTask.php index 3684eb4..8335067 100644 --- a/src/Entity/CommandTask.php +++ b/src/Entity/CommandTask.php @@ -48,6 +48,17 @@ class CommandTask 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)) { @@ -72,6 +83,17 @@ class CommandTask extends AbstractEntity return $this->commandGroups; } + public function setCommandGroups(array $commandGroups): static + { + $this->commandGroups->clear(); + + foreach ($commandGroups as $commandGroup){ + $this->addCommandGroup($commandGroup); + } + + return $this; + } + public function addCommandGroup(CommandGroup $commandGroup): static { if (!$this->commandGroups->contains($commandGroup)) { diff --git a/src/Repository/CommandTaskRepository.php b/src/Repository/CommandTaskRepository.php index e7b8cf7..fc859f8 100644 --- a/src/Repository/CommandTaskRepository.php +++ b/src/Repository/CommandTaskRepository.php @@ -9,35 +9,10 @@ use Doctrine\Persistence\ManagerRegistry; /** * @extends ServiceEntityRepository */ -class CommandTaskRepository extends ServiceEntityRepository +class CommandTaskRepository extends AbstractRepository { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, CommandTask::class); } - - // /** - // * @return CommandTask[] Returns an array of CommandTask 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): ?CommandTask - // { - // return $this->createQueryBuilder('c') - // ->andWhere('c.exampleField = :val') - // ->setParameter('val', $value) - // ->getQuery() - // ->getOneOrNullResult() - // ; - // } } diff --git a/src/State/Processor/CommandTaskProcessor.php b/src/State/Processor/CommandTaskProcessor.php new file mode 100644 index 0000000..bad348b --- /dev/null +++ b/src/State/Processor/CommandTaskProcessor.php @@ -0,0 +1,68 @@ +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 = []): CommandTaskOutput + { + if (!($data instanceof CommandTaskInput)) { + throw new \Exception(sprintf('data is not instance of %s', CommandTaskInput::class)); + } + + $entity = null; + if (isset($uriVariables['uuid'])) { + $entity = $this->commandTaskRepository->findOneByUuid($uriVariables['uuid']); + } + + $command = $data->createOrUpdateEntity($entity); + $this->validator->validate($command); + $this->commandTaskRepository->save($command); + + return new CommandTaskOutput($command); + } + + private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null + { + $user = $this->commandTaskRepository->findOneByUuid($uriVariables['uuid']); + $this->commandTaskRepository->delete($user); + + return null; + } +} diff --git a/src/State/Provider/CommandTaskProvider.php b/src/State/Provider/CommandTaskProvider.php new file mode 100644 index 0000000..49ef3d3 --- /dev/null +++ b/src/State/Provider/CommandTaskProvider.php @@ -0,0 +1,71 @@ +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|array|null + { + $paginator = $this->collectionProvider->provide($operation, $uriVariables, $context); + + $items = new \ArrayObject(); + foreach ($paginator->getIterator() as $item){ + $items[] = new CommandTaskOutput($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 task not found'); + } + + return new CommandTaskOutput($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 CommandTaskInput($item) : null; + } + + return new CommandTaskInput(); + } +}