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

86 lines
3.1 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace App\Infrastructure\Http\Controller\Api;
use App\Application\Channel\PlatformService;
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('/platforms', name: 'api_platforms_')]
final class PlatformController extends AbstractController
{
public function __construct(private readonly PlatformService $service)
{
}
#[Route('', name: 'list', methods: ['GET'])]
public function list(): JsonResponse
{
return $this->json(array_map(static fn ($p) => [
'id' => $p->getId()->toRfc4122(),
'type' => $p->getType(),
'label' => $p->getLabel(),
], $this->service->findAll()));
}
#[Route('', name: 'create', methods: ['POST'])]
public function create(Request $request): JsonResponse
{
$data = $request->toArray();
if (empty($data['type']) || empty($data['label'])) {
return $this->json(['error' => 'type and label are required'], Response::HTTP_BAD_REQUEST);
}
$platform = $this->service->create($data['type'], $data['label'], $data['config'] ?? []);
return $this->json(['id' => $platform->getId()->toRfc4122(), 'type' => $platform->getType()], Response::HTTP_CREATED);
}
#[Route('/{id}/channel-fields', name: 'list_fields', methods: ['GET'])]
public function listFields(string $id): JsonResponse
{
$fields = $this->service->findChannelFields(Uuid::fromString($id));
return $this->json(array_map(static fn ($f) => [
'id' => $f->getId()->toRfc4122(),
'label' => $f->getLabel(),
'path' => $f->getPath(),
], $fields));
}
#[Route('/{id}/channel-fields', name: 'add_field', methods: ['POST'])]
public function addField(string $id, Request $request): JsonResponse
{
$data = $request->toArray();
if (empty($data['label']) || empty($data['path'])) {
return $this->json(['error' => 'label and path are required'], Response::HTTP_BAD_REQUEST);
}
try {
$field = $this->service->addChannelField(Uuid::fromString($id), $data['label'], $data['path']);
} catch (\DomainException $e) {
return $this->json(['error' => $e->getMessage()], Response::HTTP_NOT_FOUND);
}
return $this->json(['id' => $field->getId()->toRfc4122(), 'label' => $field->getLabel()], Response::HTTP_CREATED);
}
#[Route('/channel-fields/{fieldId}', name: 'remove_field', methods: ['DELETE'])]
public function removeField(string $fieldId): JsonResponse
{
try {
$this->service->removeChannelField(Uuid::fromString($fieldId));
} catch (\DomainException $e) {
return $this->json(['error' => $e->getMessage()], Response::HTTP_NOT_FOUND);
}
return $this->json(null, Response::HTTP_NO_CONTENT);
}
}