From 3486957447887b150c4f55666a312c1bc2ba36c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 6 Aug 2023 20:57:32 +0200 Subject: [PATCH] Added endpoint for suppliers --- src/Entity/Parts/Manufacturer.php | 2 ++ src/Entity/Parts/Supplier.php | 30 +++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Entity/Parts/Manufacturer.php b/src/Entity/Parts/Manufacturer.php index b83024ef..0eb9653a 100644 --- a/src/Entity/Parts/Manufacturer.php +++ b/src/Entity/Parts/Manufacturer.php @@ -96,6 +96,8 @@ class Manufacturer extends AbstractCompany #[Assert\Valid] #[ORM\OneToMany(targetEntity: ManufacturerParameter::class, mappedBy: 'element', cascade: ['persist', 'remove'], orphanRemoval: true)] #[ORM\OrderBy(['group' => 'ASC', 'name' => 'ASC'])] + #[Groups(['manufacturer:read', 'manufacturer:write'])] + #[ApiProperty(readableLink: false, writableLink: true)] protected Collection $parameters; public function __construct() { diff --git a/src/Entity/Parts/Supplier.php b/src/Entity/Parts/Supplier.php index 4dca8f36..94986435 100644 --- a/src/Entity/Parts/Supplier.php +++ b/src/Entity/Parts/Supplier.php @@ -22,6 +22,12 @@ declare(strict_types=1); namespace App\Entity\Parts; +use ApiPlatform\Metadata\ApiFilter; +use ApiPlatform\Metadata\ApiProperty; +use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\GetCollection; +use ApiPlatform\Metadata\Link; +use ApiPlatform\Serializer\Filter\PropertyFilter; use App\Entity\Attachments\Attachment; use App\Entity\Attachments\AttachmentTypeAttachment; use App\Repository\Parts\SupplierRepository; @@ -49,6 +55,19 @@ use Symfony\Component\Validator\Constraints as Assert; #[ORM\Table('`suppliers`')] #[ORM\Index(name: 'supplier_idx_name', columns: ['name'])] #[ORM\Index(name: 'supplier_idx_parent_name', columns: ['parent_id', 'name'])] +#[ApiResource( + normalizationContext: ['groups' => ['supplier:read', 'company:read', 'api:basic:read'], 'openapi_definition_name' => 'Read'], + denormalizationContext: ['groups' => ['supplier:write', 'company:write', 'api:basic:write'], 'openapi_definition_name' => 'Write'], +)] +#[ApiResource( + uriTemplate: '/suppliers/{id}/children.{_format}', + operations: [new GetCollection(openapiContext: ['summary' => 'Retrieves the children elements of a supplier'])], + uriVariables: [ + 'id' => new Link(fromClass: Supplier::class, fromProperty: 'children') + ], + normalizationContext: ['groups' => ['supplier:read', 'company:read', 'api:basic:read'], 'openapi_definition_name' => 'Read'] +)] +#[ApiFilter(PropertyFilter::class)] class Supplier extends AbstractCompany { #[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')] @@ -57,11 +76,10 @@ class Supplier extends AbstractCompany #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] #[ORM\JoinColumn(name: 'parent_id')] + #[Groups(['supplier:read', 'supplier:write'])] + #[ApiProperty(readableLink: false, writableLink: false)] protected ?AbstractStructuralDBElement $parent = null; - /** - * @var Collection|Orderdetail[] - */ /** * @var Collection|Orderdetail[] */ @@ -91,10 +109,14 @@ class Supplier extends AbstractCompany #[Assert\Valid] #[ORM\OneToMany(targetEntity: SupplierAttachment::class, mappedBy: 'element', cascade: ['persist', 'remove'], orphanRemoval: true)] #[ORM\OrderBy(['name' => 'ASC'])] + #[Groups(['supplier:read', 'supplier:write'])] + #[ApiProperty(readableLink: false, writableLink: true)] protected Collection $attachments; #[ORM\ManyToOne(targetEntity: SupplierAttachment::class)] #[ORM\JoinColumn(name: 'id_preview_attachment', onDelete: 'SET NULL')] + #[Groups(['supplier:read', 'supplier:write'])] + #[ApiProperty(readableLink: false, writableLink: true)] protected ?Attachment $master_picture_attachment = null; /** @var Collection @@ -102,6 +124,8 @@ class Supplier extends AbstractCompany #[Assert\Valid] #[ORM\OneToMany(targetEntity: SupplierParameter::class, mappedBy: 'element', cascade: ['persist', 'remove'], orphanRemoval: true)] #[ORM\OrderBy(['group' => 'ASC', 'name' => 'ASC'])] + #[Groups(['supplier:read', 'supplier:write'])] + #[ApiProperty(readableLink: false, writableLink: true)] protected Collection $parameters; /**