refs #659. CommandTask API
parent
0f0b047c85
commit
794237dd90
|
@ -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
|
|
@ -111,3 +111,8 @@ services:
|
||||||
bind:
|
bind:
|
||||||
$collectionProvider: '@api_platform.doctrine.orm.state.collection_provider'
|
$collectionProvider: '@api_platform.doctrine.orm.state.collection_provider'
|
||||||
$itemProvider: '@api_platform.doctrine.orm.state.item_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'
|
|
@ -0,0 +1,96 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Dto\Input;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\ApiProperty;
|
||||||
|
use App\Dto\Output\CommandGroupOutput;
|
||||||
|
use App\Dto\Output\CommandOutput;
|
||||||
|
use App\Entity\CommandTask;
|
||||||
|
use App\Model\CommandTaskStatus;
|
||||||
|
use phpDocumentor\Reflection\Types\Boolean;
|
||||||
|
use Symfony\Component\Serializer\Annotation\Groups;
|
||||||
|
use Symfony\Component\Validator\Constraints as Assert;
|
||||||
|
|
||||||
|
final class CommandTaskInput
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var CommandOutput[]
|
||||||
|
*/
|
||||||
|
#[Groups(['command-task:write'])]
|
||||||
|
#[ApiProperty(
|
||||||
|
description: 'Los comandos de la tarea',
|
||||||
|
example: 'Comandos de la tarea',
|
||||||
|
)]
|
||||||
|
public array $commands = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var CommandGroupOutput[]
|
||||||
|
*/
|
||||||
|
#[Groups(['command-task:write'])]
|
||||||
|
#[ApiProperty(
|
||||||
|
description: 'Los grupos de comandos de la tarea',
|
||||||
|
example: 'Grupos de comandos de la tarea',
|
||||||
|
)]
|
||||||
|
public array $commandGroups = [];
|
||||||
|
|
||||||
|
#[Assert\NotBlank(message: 'validators.command_task.datetime.not_blank')]
|
||||||
|
#[Groups(['command-task:write'])]
|
||||||
|
#[ApiProperty(
|
||||||
|
description: 'La fecha y hora de la tarea',
|
||||||
|
example: '2021-10-01T00:00:00+00:00',
|
||||||
|
)]
|
||||||
|
public ?\DateTimeInterface $dateTime = null;
|
||||||
|
|
||||||
|
#[Groups(['command-task:write'])]
|
||||||
|
#[ApiProperty(
|
||||||
|
description: 'Los comentarios de la tarea',
|
||||||
|
example: 'Comentarios de la tarea',
|
||||||
|
)]
|
||||||
|
public ?string $notes = null;
|
||||||
|
|
||||||
|
public function __construct(?CommandTask $commandTask = null)
|
||||||
|
{
|
||||||
|
if (!$commandTask) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($commandTask->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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,13 +10,13 @@ use Symfony\Component\Serializer\Annotation\Groups;
|
||||||
#[Get(shortName: 'CommandGroup')]
|
#[Get(shortName: 'CommandGroup')]
|
||||||
final class CommandGroupOutput extends AbstractOutput
|
final class CommandGroupOutput extends AbstractOutput
|
||||||
{
|
{
|
||||||
#[Groups(['command-group:read'])]
|
#[Groups(['command-group:read', 'command-task:read'])]
|
||||||
public string $name;
|
public string $name;
|
||||||
|
|
||||||
#[Groups(['command-group:read'])]
|
#[Groups(['command-group:read', 'command-task:read'])]
|
||||||
public array $commands = [];
|
public array $commands = [];
|
||||||
|
|
||||||
#[Groups(['command-group:read'])]
|
#[Groups(['command-group:read', 'command-task:read'])]
|
||||||
public ?int $position = null;
|
public ?int $position = null;
|
||||||
|
|
||||||
#[Groups(['command-group:read'])]
|
#[Groups(['command-group:read'])]
|
||||||
|
|
|
@ -9,10 +9,10 @@ use Symfony\Component\Serializer\Annotation\Groups;
|
||||||
#[Get(shortName: 'Command')]
|
#[Get(shortName: 'Command')]
|
||||||
final class CommandOutput extends AbstractOutput
|
final class CommandOutput extends AbstractOutput
|
||||||
{
|
{
|
||||||
#[Groups(['command:read'])]
|
#[Groups(['command:read', 'command-group:read', 'command-task:read'])]
|
||||||
public string $name;
|
public string $name;
|
||||||
|
|
||||||
#[Groups(['command:read'])]
|
#[Groups(['command:read', 'command-group:read', 'command-task:read'])]
|
||||||
public ?string $script = '';
|
public ?string $script = '';
|
||||||
|
|
||||||
#[Groups(['command:read'])]
|
#[Groups(['command:read'])]
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Dto\Output;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\Get;
|
||||||
|
use App\Entity\Command;
|
||||||
|
use App\Entity\CommandGroup;
|
||||||
|
use App\Entity\CommandTask;
|
||||||
|
use Symfony\Component\Serializer\Annotation\Groups;
|
||||||
|
|
||||||
|
#[Get(shortName: 'CommandTask')]
|
||||||
|
final class CommandTaskOutput extends AbstractOutput
|
||||||
|
{
|
||||||
|
#[Groups(['command-task:read'])]
|
||||||
|
public array $commands = [];
|
||||||
|
|
||||||
|
#[Groups(['command-task:read'])]
|
||||||
|
public array $commandGroups = [];
|
||||||
|
|
||||||
|
#[Groups(['command-task:read'])]
|
||||||
|
public \DateTimeInterface $dateTime;
|
||||||
|
|
||||||
|
#[Groups(['command-task:read'])]
|
||||||
|
public ?string $notes = null;
|
||||||
|
|
||||||
|
#[Groups(['command-task:read'])]
|
||||||
|
public ?string $status = null;
|
||||||
|
|
||||||
|
#[Groups(['command-task:read'])]
|
||||||
|
public \DateTime $createdAt;
|
||||||
|
|
||||||
|
#[Groups(['command-task:read'])]
|
||||||
|
public ?string $createdBy = null;
|
||||||
|
|
||||||
|
|
||||||
|
public function __construct(CommandTask $commandTask)
|
||||||
|
{
|
||||||
|
parent::__construct($commandTask);
|
||||||
|
|
||||||
|
$this->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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -54,7 +54,6 @@ class CommandGroup extends AbstractEntity
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function addCommand(Command $command): static
|
public function addCommand(Command $command): static
|
||||||
{
|
{
|
||||||
if (!$this->commands->contains($command)) {
|
if (!$this->commands->contains($command)) {
|
||||||
|
|
|
@ -48,6 +48,17 @@ class CommandTask extends AbstractEntity
|
||||||
return $this->commands;
|
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
|
public function addCommand(Command $command): static
|
||||||
{
|
{
|
||||||
if (!$this->commands->contains($command)) {
|
if (!$this->commands->contains($command)) {
|
||||||
|
@ -72,6 +83,17 @@ class CommandTask extends AbstractEntity
|
||||||
return $this->commandGroups;
|
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
|
public function addCommandGroup(CommandGroup $commandGroup): static
|
||||||
{
|
{
|
||||||
if (!$this->commandGroups->contains($commandGroup)) {
|
if (!$this->commandGroups->contains($commandGroup)) {
|
||||||
|
|
|
@ -9,35 +9,10 @@ use Doctrine\Persistence\ManagerRegistry;
|
||||||
/**
|
/**
|
||||||
* @extends ServiceEntityRepository<CommandTask>
|
* @extends ServiceEntityRepository<CommandTask>
|
||||||
*/
|
*/
|
||||||
class CommandTaskRepository extends ServiceEntityRepository
|
class CommandTaskRepository extends AbstractRepository
|
||||||
{
|
{
|
||||||
public function __construct(ManagerRegistry $registry)
|
public function __construct(ManagerRegistry $registry)
|
||||||
{
|
{
|
||||||
parent::__construct($registry, CommandTask::class);
|
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()
|
|
||||||
// ;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
<?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\CommandTaskInput;
|
||||||
|
use App\Dto\Output\CommandTaskOutput;
|
||||||
|
use App\Repository\CommandTaskRepository;
|
||||||
|
|
||||||
|
readonly class CommandTaskProcessor implements ProcessorInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private CommandTaskRepository $commandTaskRepository,
|
||||||
|
private ValidatorInterface $validator
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): CommandTaskOutput|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 = []): 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,71 @@
|
||||||
|
<?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\CommandTaskInput;
|
||||||
|
use App\Dto\Output\CommandTaskOutput;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
|
|
||||||
|
readonly class CommandTaskProvider implements ProviderInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private ProviderInterface $collectionProvider,
|
||||||
|
private 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|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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue