refs #659. CommandGroup. Added 'add-command' endpoint

feature/actions
Manuel Aranda Rosales 2024-09-16 13:55:57 +02:00
parent ad02456de6
commit 1afe73bd09
4 changed files with 60 additions and 1 deletions

View File

@ -24,6 +24,13 @@ resources:
ApiPlatform\Metadata\Post: ~
ApiPlatform\Metadata\Delete: ~
add_commands:
class: ApiPlatform\Metadata\Post
method: POST
input: App\Dto\Input\CommandGroupAddCommandsInput
uriTemplate: /command-groups/{uuid}/add-commands
controller: App\Controller\CommandGroupAddCommandsAction
properties:
App\Entity\CommandGroup:
id:

View File

@ -0,0 +1,35 @@
<?php
namespace App\Controller;
use App\Dto\Input\CommandGroupAddCommandsInput;
use App\Entity\Command;
use App\Entity\CommandGroup;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
class CommandGroupAddCommandsAction extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager
)
{
}
public function __invoke(CommandGroupAddCommandsInput $input, CommandGroup $commandGroup): JsonResponse
{
$commands = $input->commands;
/** @var Command $command */
foreach ($commands as $command) {
$commandGroup->addCommand($command->getEntity());
}
$this->entityManager->persist($commandGroup);
$this->entityManager->flush();
return new JsonResponse(data: 'Commands added to command group successfully', status: Response::HTTP_OK);
}
}

View File

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

View File

@ -36,7 +36,7 @@ readonly class CommandGroupProvider implements ProviderInterface
}
}
private function provideCollection(Operation $operation, array $uriVariables = [], array $context = []): object
private function provideCollection(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$paginator = $this->collectionProvider->provide($operation, $uriVariables, $context);