SuperSeller3000/src/Infrastructure/Http/Controller/Api/MappingController.php

104 lines
4.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace App\Infrastructure\Http\Controller\Api;
use App\Application\Channel\MappingService;
use App\Infrastructure\Channel\Ebay\EbayTaxonomyService;
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('/api/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);
}
#[Route('/ebay-category-aspects/{categoryId}', name: 'ebay_aspects', methods: ['GET'])]
public function ebayAspects(string $typeId, string $categoryId, EbayTaxonomyService $taxonomy): JsonResponse
{
try {
$aspects = $taxonomy->getCategoryAspects($categoryId);
} catch (\Throwable $e) {
return $this->json(['error' => 'eBay API error: '.$e->getMessage()], Response::HTTP_SERVICE_UNAVAILABLE);
}
return $this->json($aspects);
}
}