Allow to create parameters via the API

This commit is contained in:
Jan Böhmer 2024-03-02 21:49:16 +01:00
parent e3d2012809
commit e843286ea7
12 changed files with 48 additions and 2 deletions

View file

@ -63,6 +63,7 @@ use LogicException;
use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName; use Symfony\Component\Serializer\Annotation\SerializedName;
use Symfony\Component\Serializer\Attribute\DiscriminatorMap;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
use function sprintf; use function sprintf;
@ -82,7 +83,7 @@ use function sprintf;
shortName: 'Parameter', shortName: 'Parameter',
operations: [ operations: [
new Get(security: 'is_granted("read", object)'), new Get(security: 'is_granted("read", object)'),
//new Post(securityPostDenormalize: 'is_granted("create", object)'), new Post(securityPostDenormalize: 'is_granted("create", object)'),
new Patch(security: 'is_granted("edit", object)'), new Patch(security: 'is_granted("edit", object)'),
new Delete(security: 'is_granted("delete", object)'), new Delete(security: 'is_granted("delete", object)'),
], ],
@ -93,8 +94,20 @@ use function sprintf;
#[ApiFilter(DateFilter::class, strategy: DateFilter::EXCLUDE_NULL)] #[ApiFilter(DateFilter::class, strategy: DateFilter::EXCLUDE_NULL)]
#[ApiFilter(RangeFilter::class, properties: ["value_min", "value_typical", "value_max"])] #[ApiFilter(RangeFilter::class, properties: ["value_min", "value_typical", "value_max"])]
#[ApiFilter(OrderFilter::class, properties: ['name', 'id', 'addedDate', 'lastModified'])] #[ApiFilter(OrderFilter::class, properties: ['name', 'id', 'addedDate', 'lastModified'])]
//This discriminator map is required for API platform to know which class to use for deserialization, when creating a new parameter.
#[DiscriminatorMap(typeProperty: '_type', mapping: self::API_DISCRIMINATOR_MAP)]
abstract class AbstractParameter extends AbstractNamedDBElement abstract class AbstractParameter extends AbstractNamedDBElement
{ {
/*
* The discriminator map used for API platform. The key should be the same as the api platform short type (the @type JSONLD field).
*/
private const API_DISCRIMINATOR_MAP = ["Part" => PartParameter::class,
"AttachmentType" => AttachmentTypeParameter::class, "Category" => CategoryParameter::class, "Currency" => CurrencyParameter::class,
"Project" => ProjectParameter::class, "Footprint" => FootprintParameter::class, "Group" => GroupParameter::class,
"Manufacturer" => ManufacturerParameter::class, "MeasurementUnit" => MeasurementUnitParameter::class,
"StorageLocation" => StorageLocationParameter::class, "Supplier" => SupplierParameter::class];
/** /**
* @var string The class of the element that can be passed to this attachment. Must be overridden in subclasses. * @var string The class of the element that can be passed to this attachment. Must be overridden in subclasses.
*/ */

View file

@ -44,8 +44,10 @@ namespace App\Entity\Parameters;
use App\Repository\ParameterRepository; use App\Repository\ParameterRepository;
use App\Entity\Attachments\AttachmentType; use App\Entity\Attachments\AttachmentType;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Serializer\OverrideClassDenormalizer;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Attribute\Context;
#[UniqueEntity(fields: ['name', 'group', 'element'])] #[UniqueEntity(fields: ['name', 'group', 'element'])]
#[ORM\Entity(repositoryClass: ParameterRepository::class)] #[ORM\Entity(repositoryClass: ParameterRepository::class)]
@ -57,5 +59,6 @@ class AttachmentTypeParameter extends AbstractParameter
*/ */
#[ORM\ManyToOne(targetEntity: AttachmentType::class, inversedBy: 'parameters')] #[ORM\ManyToOne(targetEntity: AttachmentType::class, inversedBy: 'parameters')]
#[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')] #[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')]
#[Context(denormalizationContext: [OverrideClassDenormalizer::CONTEXT_KEY => self::ALLOWED_ELEMENT_CLASS])]
protected ?AbstractDBElement $element = null; protected ?AbstractDBElement $element = null;
} }

View file

@ -44,8 +44,10 @@ namespace App\Entity\Parameters;
use App\Repository\ParameterRepository; use App\Repository\ParameterRepository;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\Parts\Category; use App\Entity\Parts\Category;
use App\Serializer\OverrideClassDenormalizer;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Attribute\Context;
#[UniqueEntity(fields: ['name', 'group', 'element'])] #[UniqueEntity(fields: ['name', 'group', 'element'])]
#[ORM\Entity(repositoryClass: ParameterRepository::class)] #[ORM\Entity(repositoryClass: ParameterRepository::class)]
@ -57,5 +59,6 @@ class CategoryParameter extends AbstractParameter
*/ */
#[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'parameters')] #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'parameters')]
#[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')] #[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')]
#[Context(denormalizationContext: [OverrideClassDenormalizer::CONTEXT_KEY => self::ALLOWED_ELEMENT_CLASS])]
protected ?AbstractDBElement $element = null; protected ?AbstractDBElement $element = null;
} }

View file

@ -44,8 +44,10 @@ namespace App\Entity\Parameters;
use App\Repository\ParameterRepository; use App\Repository\ParameterRepository;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\PriceInformations\Currency; use App\Entity\PriceInformations\Currency;
use App\Serializer\OverrideClassDenormalizer;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Attribute\Context;
/** /**
* An attachment attached to a category element. * An attachment attached to a category element.
@ -61,5 +63,6 @@ class CurrencyParameter extends AbstractParameter
*/ */
#[ORM\ManyToOne(targetEntity: Currency::class, inversedBy: 'parameters')] #[ORM\ManyToOne(targetEntity: Currency::class, inversedBy: 'parameters')]
#[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')] #[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')]
#[Context(denormalizationContext: [OverrideClassDenormalizer::CONTEXT_KEY => self::ALLOWED_ELEMENT_CLASS])]
protected ?AbstractDBElement $element = null; protected ?AbstractDBElement $element = null;
} }

View file

@ -44,8 +44,10 @@ namespace App\Entity\Parameters;
use App\Repository\ParameterRepository; use App\Repository\ParameterRepository;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\Parts\Footprint; use App\Entity\Parts\Footprint;
use App\Serializer\OverrideClassDenormalizer;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Attribute\Context;
#[UniqueEntity(fields: ['name', 'group', 'element'])] #[UniqueEntity(fields: ['name', 'group', 'element'])]
#[ORM\Entity(repositoryClass: ParameterRepository::class)] #[ORM\Entity(repositoryClass: ParameterRepository::class)]
@ -58,5 +60,6 @@ class FootprintParameter extends AbstractParameter
*/ */
#[ORM\ManyToOne(targetEntity: Footprint::class, inversedBy: 'parameters')] #[ORM\ManyToOne(targetEntity: Footprint::class, inversedBy: 'parameters')]
#[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')] #[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')]
#[Context(denormalizationContext: [OverrideClassDenormalizer::CONTEXT_KEY => self::ALLOWED_ELEMENT_CLASS])]
protected ?AbstractDBElement $element = null; protected ?AbstractDBElement $element = null;
} }

View file

@ -44,8 +44,10 @@ namespace App\Entity\Parameters;
use App\Repository\ParameterRepository; use App\Repository\ParameterRepository;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\UserSystem\Group; use App\Entity\UserSystem\Group;
use App\Serializer\OverrideClassDenormalizer;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Attribute\Context;
#[UniqueEntity(fields: ['name', 'group', 'element'])] #[UniqueEntity(fields: ['name', 'group', 'element'])]
#[ORM\Entity(repositoryClass: ParameterRepository::class)] #[ORM\Entity(repositoryClass: ParameterRepository::class)]
@ -58,5 +60,6 @@ class GroupParameter extends AbstractParameter
*/ */
#[ORM\ManyToOne(targetEntity: Group::class, inversedBy: 'parameters')] #[ORM\ManyToOne(targetEntity: Group::class, inversedBy: 'parameters')]
#[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')] #[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')]
#[Context(denormalizationContext: [OverrideClassDenormalizer::CONTEXT_KEY => self::ALLOWED_ELEMENT_CLASS])]
protected ?AbstractDBElement $element = null; protected ?AbstractDBElement $element = null;
} }

View file

@ -44,8 +44,10 @@ namespace App\Entity\Parameters;
use App\Repository\ParameterRepository; use App\Repository\ParameterRepository;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\Parts\Manufacturer; use App\Entity\Parts\Manufacturer;
use App\Serializer\OverrideClassDenormalizer;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Attribute\Context;
#[UniqueEntity(fields: ['name', 'group', 'element'])] #[UniqueEntity(fields: ['name', 'group', 'element'])]
#[ORM\Entity(repositoryClass: ParameterRepository::class)] #[ORM\Entity(repositoryClass: ParameterRepository::class)]
@ -58,5 +60,6 @@ class ManufacturerParameter extends AbstractParameter
*/ */
#[ORM\ManyToOne(targetEntity: Manufacturer::class, inversedBy: 'parameters')] #[ORM\ManyToOne(targetEntity: Manufacturer::class, inversedBy: 'parameters')]
#[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')] #[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')]
#[Context(denormalizationContext: [OverrideClassDenormalizer::CONTEXT_KEY => self::ALLOWED_ELEMENT_CLASS])]
protected ?AbstractDBElement $element = null; protected ?AbstractDBElement $element = null;
} }

View file

@ -44,8 +44,10 @@ namespace App\Entity\Parameters;
use App\Repository\ParameterRepository; use App\Repository\ParameterRepository;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\Parts\MeasurementUnit; use App\Entity\Parts\MeasurementUnit;
use App\Serializer\OverrideClassDenormalizer;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Attribute\Context;
#[UniqueEntity(fields: ['name', 'group', 'element'])] #[UniqueEntity(fields: ['name', 'group', 'element'])]
#[ORM\Entity(repositoryClass: ParameterRepository::class)] #[ORM\Entity(repositoryClass: ParameterRepository::class)]
@ -58,5 +60,6 @@ class MeasurementUnitParameter extends AbstractParameter
*/ */
#[ORM\ManyToOne(targetEntity: MeasurementUnit::class, inversedBy: 'parameters')] #[ORM\ManyToOne(targetEntity: MeasurementUnit::class, inversedBy: 'parameters')]
#[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')] #[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')]
#[Context(denormalizationContext: [OverrideClassDenormalizer::CONTEXT_KEY => self::ALLOWED_ELEMENT_CLASS])]
protected ?AbstractDBElement $element = null; protected ?AbstractDBElement $element = null;
} }

View file

@ -44,8 +44,10 @@ namespace App\Entity\Parameters;
use App\Repository\ParameterRepository; use App\Repository\ParameterRepository;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\Parts\Part; use App\Entity\Parts\Part;
use App\Serializer\OverrideClassDenormalizer;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Attribute\Context;
/** /**
* @see \App\Tests\Entity\Parameters\PartParameterTest * @see \App\Tests\Entity\Parameters\PartParameterTest
@ -61,5 +63,6 @@ class PartParameter extends AbstractParameter
*/ */
#[ORM\ManyToOne(targetEntity: Part::class, inversedBy: 'parameters')] #[ORM\ManyToOne(targetEntity: Part::class, inversedBy: 'parameters')]
#[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')] #[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')]
#[Context(denormalizationContext: [OverrideClassDenormalizer::CONTEXT_KEY => self::ALLOWED_ELEMENT_CLASS])]
protected ?AbstractDBElement $element = null; protected ?AbstractDBElement $element = null;
} }

View file

@ -44,8 +44,10 @@ namespace App\Entity\Parameters;
use App\Repository\ParameterRepository; use App\Repository\ParameterRepository;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\ProjectSystem\Project; use App\Entity\ProjectSystem\Project;
use App\Serializer\OverrideClassDenormalizer;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Attribute\Context;
#[UniqueEntity(fields: ['name', 'group', 'element'])] #[UniqueEntity(fields: ['name', 'group', 'element'])]
#[ORM\Entity(repositoryClass: ParameterRepository::class)] #[ORM\Entity(repositoryClass: ParameterRepository::class)]
@ -58,5 +60,6 @@ class ProjectParameter extends AbstractParameter
*/ */
#[ORM\ManyToOne(targetEntity: Project::class, inversedBy: 'parameters')] #[ORM\ManyToOne(targetEntity: Project::class, inversedBy: 'parameters')]
#[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')] #[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')]
#[Context(denormalizationContext: [OverrideClassDenormalizer::CONTEXT_KEY => self::ALLOWED_ELEMENT_CLASS])]
protected ?AbstractDBElement $element = null; protected ?AbstractDBElement $element = null;
} }

View file

@ -44,8 +44,10 @@ namespace App\Entity\Parameters;
use App\Repository\ParameterRepository; use App\Repository\ParameterRepository;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\Parts\StorageLocation; use App\Entity\Parts\StorageLocation;
use App\Serializer\OverrideClassDenormalizer;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Attribute\Context;
#[UniqueEntity(fields: ['name', 'group', 'element'])] #[UniqueEntity(fields: ['name', 'group', 'element'])]
#[ORM\Entity(repositoryClass: ParameterRepository::class)] #[ORM\Entity(repositoryClass: ParameterRepository::class)]
@ -58,5 +60,6 @@ class StorageLocationParameter extends AbstractParameter
*/ */
#[ORM\ManyToOne(targetEntity: StorageLocation::class, inversedBy: 'parameters')] #[ORM\ManyToOne(targetEntity: StorageLocation::class, inversedBy: 'parameters')]
#[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')] #[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')]
#[Context(denormalizationContext: [OverrideClassDenormalizer::CONTEXT_KEY => self::ALLOWED_ELEMENT_CLASS])]
protected ?AbstractDBElement $element = null; protected ?AbstractDBElement $element = null;
} }

View file

@ -44,8 +44,10 @@ namespace App\Entity\Parameters;
use App\Repository\ParameterRepository; use App\Repository\ParameterRepository;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\Parts\Supplier; use App\Entity\Parts\Supplier;
use App\Serializer\OverrideClassDenormalizer;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Attribute\Context;
#[UniqueEntity(fields: ['name', 'group', 'element'])] #[UniqueEntity(fields: ['name', 'group', 'element'])]
#[ORM\Entity(repositoryClass: ParameterRepository::class)] #[ORM\Entity(repositoryClass: ParameterRepository::class)]
@ -54,9 +56,10 @@ class SupplierParameter extends AbstractParameter
final public const ALLOWED_ELEMENT_CLASS = Supplier::class; final public const ALLOWED_ELEMENT_CLASS = Supplier::class;
/** /**
* @var Supplier the element this para is associated with * @var Supplier the element this parameter is associated with
*/ */
#[ORM\ManyToOne(targetEntity: Supplier::class, inversedBy: 'parameters')] #[ORM\ManyToOne(targetEntity: Supplier::class, inversedBy: 'parameters')]
#[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')] #[ORM\JoinColumn(name: 'element_id', nullable: false, onDelete: 'CASCADE')]
#[Context(denormalizationContext: [OverrideClassDenormalizer::CONTEXT_KEY => self::ALLOWED_ELEMENT_CLASS])]
protected ?AbstractDBElement $element = null; protected ?AbstractDBElement $element = null;
} }