2023-10-09 00:55:42 +02:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use App\Entity\Parts\Category;
|
|
|
|
use App\Entity\Parts\Part;
|
2023-12-03 14:42:20 +01:00
|
|
|
use App\Services\EDA\KiCadHelper;
|
2023-10-09 00:55:42 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
|
|
|
|
|
|
#[Route('/kicad-api/v1')]
|
2023-12-03 00:43:34 +01:00
|
|
|
class KiCadApiController extends AbstractController
|
2023-10-09 00:55:42 +02:00
|
|
|
{
|
2023-11-29 20:17:17 +01:00
|
|
|
public function __construct(
|
2023-12-03 14:42:20 +01:00
|
|
|
private readonly KiCadHelper $kiCADHelper,
|
2023-11-29 20:17:17 +01:00
|
|
|
)
|
2023-10-09 00:55:42 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Route('/', name: 'kicad_api_root')]
|
|
|
|
public function root(): Response
|
|
|
|
{
|
2023-12-03 00:05:41 +01:00
|
|
|
$this->denyAccessUnlessGranted('HAS_ACCESS_PERMISSIONS');
|
|
|
|
|
2023-10-09 00:55:42 +02:00
|
|
|
//The API documentation says this can be either blank or the URL to the endpoints
|
|
|
|
return $this->json([
|
|
|
|
'categories' => '',
|
|
|
|
'parts' => '',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Route('/categories.json', name: 'kicad_api_categories')]
|
2023-12-03 00:43:34 +01:00
|
|
|
public function categories(): Response
|
2023-10-09 00:55:42 +02:00
|
|
|
{
|
2023-11-29 20:17:17 +01:00
|
|
|
$this->denyAccessUnlessGranted('@categories.read');
|
2023-10-09 00:55:42 +02:00
|
|
|
|
2023-11-29 20:17:17 +01:00
|
|
|
return $this->json($this->kiCADHelper->getCategories());
|
2023-10-09 00:55:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[Route('/parts/category/{category}.json', name: 'kicad_api_category')]
|
2023-12-03 15:03:00 +01:00
|
|
|
public function categoryParts(?Category $category): Response
|
2023-10-09 00:55:42 +02:00
|
|
|
{
|
2023-12-03 15:03:00 +01:00
|
|
|
if ($category) {
|
|
|
|
$this->denyAccessUnlessGranted('read', $category);
|
|
|
|
} else {
|
|
|
|
$this->denyAccessUnlessGranted('@categories.read');
|
|
|
|
}
|
2023-11-29 20:17:17 +01:00
|
|
|
$this->denyAccessUnlessGranted('@parts.read');
|
2023-10-09 00:55:42 +02:00
|
|
|
|
2023-11-29 20:17:17 +01:00
|
|
|
return $this->json($this->kiCADHelper->getCategoryParts($category));
|
2023-10-09 00:55:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[Route('/parts/{part}.json', name: 'kicad_api_part')]
|
|
|
|
public function partDetails(Part $part): Response
|
|
|
|
{
|
2023-11-29 21:28:06 +01:00
|
|
|
$this->denyAccessUnlessGranted('read', $part);
|
2023-10-09 00:55:42 +02:00
|
|
|
|
2023-11-29 21:28:06 +01:00
|
|
|
return $this->json($this->kiCADHelper->getKiCADPart($part));
|
2023-10-09 00:55:42 +02:00
|
|
|
}
|
|
|
|
}
|