mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-27 12:18:54 +02:00
Moved some logic from KICAD controller into its own service
This commit is contained in:
parent
22f8448c65
commit
08a1ce5f64
2 changed files with 112 additions and 22 deletions
|
@ -25,6 +25,7 @@ namespace App\Controller;
|
|||
|
||||
use App\Entity\Parts\Category;
|
||||
use App\Entity\Parts\Part;
|
||||
use App\Services\EDAIntegration\KiCADHelper;
|
||||
use App\Services\Trees\NodesListBuilder;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
|
@ -34,7 +35,11 @@ use Symfony\Component\Routing\Annotation\Route;
|
|||
#[Route('/kicad-api/v1')]
|
||||
class KiCADAPIController extends AbstractController
|
||||
{
|
||||
public function __construct(private readonly EntityManagerInterface $em)
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly KiCADHelper $kiCADHelper,
|
||||
|
||||
)
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -52,34 +57,18 @@ class KiCADAPIController extends AbstractController
|
|||
#[Route('/categories.json', name: 'kicad_api_categories')]
|
||||
public function categories(NodesListBuilder $nodesListBuilder): Response
|
||||
{
|
||||
$categories = $nodesListBuilder->typeToNodesList(Category::class);
|
||||
$result = [];
|
||||
foreach ($categories as $category) {
|
||||
$result[] = [
|
||||
'id' => (string) $category->getId(),
|
||||
'name' => $category->getFullPath('/'),
|
||||
];
|
||||
}
|
||||
$this->denyAccessUnlessGranted('@categories.read');
|
||||
|
||||
return $this->json($result);
|
||||
return $this->json($this->kiCADHelper->getCategories());
|
||||
}
|
||||
|
||||
#[Route('/parts/category/{category}.json', name: 'kicad_api_category')]
|
||||
public function categoryParts(Category $category): Response
|
||||
{
|
||||
$category_repo = $this->em->getRepository(Category::class);
|
||||
$parts = $category_repo->getParts($category);
|
||||
$this->denyAccessUnlessGranted('read', $category);
|
||||
$this->denyAccessUnlessGranted('@parts.read');
|
||||
|
||||
$result = [];
|
||||
foreach ($parts as $part) {
|
||||
$result[] = [
|
||||
'id' => (string) $part->getId(),
|
||||
'name' => $part->getName(),
|
||||
'description' => $part->getDescription(),
|
||||
];
|
||||
}
|
||||
|
||||
return $this->json($result);
|
||||
return $this->json($this->kiCADHelper->getCategoryParts($category));
|
||||
}
|
||||
|
||||
#[Route('/parts/{part}.json', name: 'kicad_api_part')]
|
||||
|
|
101
src/Services/EDAIntegration/KiCADHelper.php
Normal file
101
src/Services/EDAIntegration/KiCADHelper.php
Normal file
|
@ -0,0 +1,101 @@
|
|||
<?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\Services\EDAIntegration;
|
||||
|
||||
use App\Entity\Parts\Category;
|
||||
use App\Services\Trees\NodesListBuilder;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
||||
|
||||
class KiCADHelper
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
private readonly NodesListBuilder $nodesListBuilder,
|
||||
private readonly TagAwareCacheInterface $kicadCache,
|
||||
private readonly EntityManagerInterface $em,
|
||||
)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of objects containing all categories in the database in the format required by KiCAD.
|
||||
* The categories are flattened and sorted by their full path.
|
||||
* Categories, which contain no parts, are filtered out.
|
||||
* The result is cached for performance and invalidated on category changes.
|
||||
* @return array
|
||||
*/
|
||||
public function getCategories(): array
|
||||
{
|
||||
return $this->kicadCache->get('kicad_categories', function (ItemInterface $item) {
|
||||
//Invalidate the cache on category changes
|
||||
$secure_class_name = str_replace('\\', '_', Category::class);
|
||||
$item->tag($secure_class_name);
|
||||
|
||||
$categories = $this->nodesListBuilder->typeToNodesList(Category::class);
|
||||
$repo = $this->em->getRepository(Category::class);
|
||||
$result = [];
|
||||
foreach ($categories as $category) {
|
||||
/** @var $category Category */
|
||||
//Ensure that the category contains parts
|
||||
if ($repo->getPartsCount($category) < 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//Format the category for KiCAD
|
||||
$result[] = [
|
||||
'id' => (string) $category->getId(),
|
||||
'name' => $category->getFullPath('/'),
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of objects containing all parts for the given category in the format required by KiCAD.
|
||||
* The result is cached for performance and invalidated on category or part changes.
|
||||
* @param Category $category
|
||||
* @return array
|
||||
*/
|
||||
public function getCategoryParts(Category $category): array
|
||||
{
|
||||
$category_repo = $this->em->getRepository(Category::class);
|
||||
$parts = $category_repo->getParts($category);
|
||||
|
||||
$result = [];
|
||||
foreach ($parts as $part) {
|
||||
$result[] = [
|
||||
'id' => (string) $part->getId(),
|
||||
'name' => $part->getName(),
|
||||
'description' => $part->getDescription(),
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue