refs #659. CommandGroup execute endpoint

feature/actions
Manuel Aranda Rosales 2024-09-24 09:09:29 +02:00
parent d1105a1e89
commit 34c2144ffd
7 changed files with 75 additions and 5 deletions

View File

@ -134,8 +134,6 @@ docker exec ogcore-php php bin/console opengnsys:migration:hardware-profiles #ca
docker exec ogcore-php php bin/console opengnsys:migration:clients #cargamos los clientes
docker exec ogcore-php php bin/console opengnsys:migration:os #cargamos los sistemas operativos
docker exec ogcore-php php bin/console opengnsys:migration:partition #cargamos las particiones
```
## Objetos de interés

View File

@ -24,7 +24,7 @@ resources:
ApiPlatform\Metadata\Post: ~
ApiPlatform\Metadata\Delete: ~
execute:
command_execute:
class: ApiPlatform\Metadata\Post
method: POST
input: App\Dto\Input\CommandExecuteInput

View File

@ -31,6 +31,13 @@ resources:
uriTemplate: /command-groups/{uuid}/add-commands
controller: App\Controller\CommandGroupAddCommandsAction
command_group_execute:
class: ApiPlatform\Metadata\Post
method: POST
input: App\Dto\Input\CommandGroupExecuteInput
uriTemplate: /command-groups/{uuid}/execute
controller: App\Controller\CommandGroupExecuteAction
properties:
App\Entity\CommandGroup:
id:

View File

@ -40,4 +40,6 @@ api_platform:
api_keys:
apiKey:
name: Authorization
type: header
type: header
exception_to_status:
Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException: 409

View File

@ -0,0 +1,46 @@
<?php
namespace App\Controller;
use App\Dto\Input\CommandGroupAddCommandsInput;
use App\Dto\Input\CommandGroupExecuteInput;
use App\Entity\Client;
use App\Entity\Command;
use App\Entity\CommandGroup;
use App\Entity\Trace;
use App\Model\TraceStatus;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
class CommandGroupExecuteAction extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager
)
{
}
public function __invoke(CommandGroupExecuteInput $input, CommandGroup $commandGroup): JsonResponse
{
$clients = $input->clients;
foreach ($commandGroup->getCommands() as $command) {
/** @var Client $client */
foreach ($clients as $client) {
$trace = new Trace();
$trace->setClient($client->getEntity());
$trace->setCommand($command);
$trace->setStatus(TraceStatus::IN_PROGRESS);
$trace->setExecutedAt(new \DateTimeImmutable());
$this->entityManager->persist($trace);
}
}
$this->entityManager->flush();
return new JsonResponse(data: 'Command group executed successfully', status: Response::HTTP_OK);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace App\Dto\Input;
use App\Dto\Output\ClientOutput;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
final class CommandGroupExecuteInput
{
/**
* @var ClientOutput[]
*/
#[Assert\NotNull]
#[Groups(['command-group:write'])]
public array $clients = [];
}

View File

@ -31,7 +31,7 @@ class Command extends AbstractEntity
/**
* @var Collection<int, CommandTask>
*/
#[ORM\ManyToMany(targetEntity: CommandTask::class, mappedBy: 'command')]
#[ORM\ManyToMany(targetEntity: CommandTask::class, mappedBy: 'commands')]
private Collection $commandTasks;
public function __construct()