mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-08 17:34:32 +02:00
Allow to automatically find or create entities from database based on info providers
This commit is contained in:
parent
f9bce3dfdb
commit
6cd9640b30
4 changed files with 118 additions and 1 deletions
|
@ -25,13 +25,16 @@ namespace App\Services\InfoProviderSystem;
|
|||
|
||||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Entity\Attachments\PartAttachment;
|
||||
use App\Entity\Base\AbstractStructuralDBElement;
|
||||
use App\Entity\Parameters\AbstractParameter;
|
||||
use App\Entity\Parameters\PartParameter;
|
||||
use App\Entity\Parts\Manufacturer;
|
||||
use App\Entity\Parts\ManufacturingStatus;
|
||||
use App\Entity\Parts\Part;
|
||||
use App\Services\InfoProviderSystem\DTOs\FileDTO;
|
||||
use App\Services\InfoProviderSystem\DTOs\ParameterDTO;
|
||||
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
/**
|
||||
* This class converts DTOs to entities which can be persisted in the DB
|
||||
|
@ -39,6 +42,10 @@ use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
|
|||
class DTOtoEntityConverter
|
||||
{
|
||||
|
||||
public function __construct(private readonly EntityManagerInterface $em)
|
||||
{
|
||||
}
|
||||
|
||||
public function convertParameter(ParameterDTO $dto, PartParameter $entity = new PartParameter()): PartParameter
|
||||
{
|
||||
$entity->setName($dto->name);
|
||||
|
@ -79,6 +86,8 @@ class DTOtoEntityConverter
|
|||
$entity->setDescription($dto->description ?? '');
|
||||
$entity->setComment($dto->notes ?? '');
|
||||
|
||||
$entity->setManufacturer($this->getOrCreateEntity(Manufacturer::class, $dto->manufacturer));
|
||||
|
||||
$entity->setManufacturerProductNumber($dto->mpn ?? '');
|
||||
$entity->setManufacturingStatus($dto->manufacturing_status ?? ManufacturingStatus::NOT_SET);
|
||||
|
||||
|
@ -95,4 +104,14 @@ class DTOtoEntityConverter
|
|||
return $entity;
|
||||
}
|
||||
|
||||
private function getOrCreateEntity(string $class, ?string $name): ?AbstractStructuralDBElement
|
||||
{
|
||||
//Fall through to make converting easier
|
||||
if ($name === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->em->getRepository($class)->findOrCreateForInfoProvider($name);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue