91 lines
3.8 KiB
PHP
91 lines
3.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Infrastructure\Http\Controller\Api;
|
||
|
|
|
||
|
|
use App\Application\Channel\MappingService;
|
||
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||
|
|
use Symfony\Component\HttpFoundation\Request;
|
||
|
|
use Symfony\Component\HttpFoundation\Response;
|
||
|
|
use Symfony\Component\Routing\Attribute\Route;
|
||
|
|
use Symfony\Component\Uid\Uuid;
|
||
|
|
|
||
|
|
#[Route('/article-types/{typeId}/platform-configs', name: 'api_mapping_')]
|
||
|
|
final class MappingController extends AbstractController
|
||
|
|
{
|
||
|
|
public function __construct(private readonly MappingService $service)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Route('', name: 'list', methods: ['GET'])]
|
||
|
|
public function list(string $typeId): JsonResponse
|
||
|
|
{
|
||
|
|
$configs = $this->service->findConfigsByArticleType(Uuid::fromString($typeId));
|
||
|
|
|
||
|
|
return $this->json(array_map(static fn ($c) => [
|
||
|
|
'id' => $c->getId()->toRfc4122(),
|
||
|
|
'platform' => ['id' => $c->getPlatform()->getId()->toRfc4122(), 'label' => $c->getPlatform()->getLabel()],
|
||
|
|
'categoryId' => $c->getCategoryId(),
|
||
|
|
'mappings' => array_map(static fn ($m) => [
|
||
|
|
'id' => $m->getId()->toRfc4122(),
|
||
|
|
'attribute' => ['id' => $m->getAttributeDefinition()->getId()->toRfc4122(), 'name' => $m->getAttributeDefinition()->getName()],
|
||
|
|
'channelField' => ['id' => $m->getChannelField()->getId()->toRfc4122(), 'path' => $m->getChannelField()->getPath()],
|
||
|
|
'transformer' => $m->getTransformer(),
|
||
|
|
], $c->getAttributeMappings()->toArray()),
|
||
|
|
], $configs));
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Route('', name: 'create_config', methods: ['POST'])]
|
||
|
|
public function createConfig(string $typeId, Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$data = $request->toArray();
|
||
|
|
if (empty($data['platformId']) || empty($data['categoryId'])) {
|
||
|
|
return $this->json(['error' => 'platformId and categoryId are required'], Response::HTTP_BAD_REQUEST);
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$config = $this->service->createConfig(Uuid::fromString($typeId), Uuid::fromString($data['platformId']), $data['categoryId']);
|
||
|
|
} catch (\DomainException $e) {
|
||
|
|
return $this->json(['error' => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->json(['id' => $config->getId()->toRfc4122()], Response::HTTP_CREATED);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Route('/{configId}/mappings', name: 'add_mapping', methods: ['POST'])]
|
||
|
|
public function addMapping(string $typeId, string $configId, Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$data = $request->toArray();
|
||
|
|
if (empty($data['attributeDefinitionId']) || empty($data['channelFieldId'])) {
|
||
|
|
return $this->json(['error' => 'attributeDefinitionId and channelFieldId are required'], Response::HTTP_BAD_REQUEST);
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$mapping = $this->service->addMapping(
|
||
|
|
Uuid::fromString($configId),
|
||
|
|
Uuid::fromString($data['attributeDefinitionId']),
|
||
|
|
Uuid::fromString($data['channelFieldId']),
|
||
|
|
$data['transformer'] ?? null,
|
||
|
|
);
|
||
|
|
} catch (\DomainException $e) {
|
||
|
|
return $this->json(['error' => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->json(['id' => $mapping->getId()->toRfc4122()], Response::HTTP_CREATED);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Route('/mappings/{mappingId}', name: 'remove_mapping', methods: ['DELETE'])]
|
||
|
|
public function removeMapping(string $typeId, string $mappingId): JsonResponse
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$this->service->removeMapping(Uuid::fromString($mappingId));
|
||
|
|
} catch (\DomainException $e) {
|
||
|
|
return $this->json(['error' => $e->getMessage()], Response::HTTP_NOT_FOUND);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->json(null, Response::HTTP_NO_CONTENT);
|
||
|
|
}
|
||
|
|
}
|