From 87e881f8e07a32180f9bcf838a28d4bb4b263cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Mon, 18 Sep 2023 23:38:06 +0200 Subject: [PATCH] Show attachments and preview image in API response --- config/packages/api_platform.yaml | 3 +++ src/Entity/Attachments/Attachment.php | 3 +-- src/Entity/Parts/Category.php | 4 ++-- src/Entity/Parts/Footprint.php | 6 +++--- src/Entity/Parts/MeasurementUnit.php | 4 ++-- src/Entity/Parts/Part.php | 7 +++++-- src/Entity/UserSystem/User.php | 5 ++--- 7 files changed, 18 insertions(+), 14 deletions(-) diff --git a/config/packages/api_platform.yaml b/config/packages/api_platform.yaml index 7d367346..e304a134 100644 --- a/config/packages/api_platform.yaml +++ b/config/packages/api_platform.yaml @@ -5,6 +5,9 @@ api_platform: version: '0.1.0' +# eager_loading: +# max_joins: 100 + swagger: api_keys: # overridden in OpenApiFactoryDecorator diff --git a/src/Entity/Attachments/Attachment.php b/src/Entity/Attachments/Attachment.php index 5916b603..84d0260d 100644 --- a/src/Entity/Attachments/Attachment.php +++ b/src/Entity/Attachments/Attachment.php @@ -62,7 +62,6 @@ use LogicException; #[ORM\Index(name: 'attachments_idx_class_name_id', columns: ['class_name', 'id'])] #[ORM\Index(name: 'attachment_name_idx', columns: ['name'])] #[ORM\Index(name: 'attachment_element_idx', columns: ['class_name', 'element_id'])] - #[ApiResource( operations: [ new Get(security: 'is_granted("read", object)'), @@ -128,7 +127,7 @@ abstract class Attachment extends AbstractNamedDBElement * ORM mapping is done in subclasses (like PartAttachment). * @phpstan-param T|null $element */ - #[Groups(['attachment:read:standalone', 'attachment:read'])] + #[Groups(['attachment:read:standalone', 'attachment:write'])] protected ?AttachmentContainingDBElement $element = null; #[ORM\Column(type: Types::BOOLEAN)] diff --git a/src/Entity/Parts/Category.php b/src/Entity/Parts/Category.php index b1714fe9..11731ab6 100644 --- a/src/Entity/Parts/Category.php +++ b/src/Entity/Parts/Category.php @@ -152,14 +152,14 @@ class Category extends AbstractPartsContainingDBElement * @var Collection */ #[Assert\Valid] - #[Groups(['full', 'category:read'])] + #[Groups(['full', 'category:read', 'category:write'])] #[ORM\OneToMany(targetEntity: CategoryAttachment::class, mappedBy: 'element', cascade: ['persist', 'remove'], orphanRemoval: true)] #[ORM\OrderBy(['name' => 'ASC'])] protected Collection $attachments; #[ORM\ManyToOne(targetEntity: CategoryAttachment::class)] #[ORM\JoinColumn(name: 'id_preview_attachment', onDelete: 'SET NULL')] - #[Groups(['category:read'])] + #[Groups(['category:read', 'category:write'])] protected ?Attachment $master_picture_attachment = null; /** @var Collection diff --git a/src/Entity/Parts/Footprint.php b/src/Entity/Parts/Footprint.php index c7148deb..edeb31cc 100644 --- a/src/Entity/Parts/Footprint.php +++ b/src/Entity/Parts/Footprint.php @@ -98,12 +98,12 @@ class Footprint extends AbstractPartsContainingDBElement #[Assert\Valid] #[ORM\OneToMany(targetEntity: FootprintAttachment::class, mappedBy: 'element', cascade: ['persist', 'remove'], orphanRemoval: true)] #[ORM\OrderBy(['name' => 'ASC'])] - #[Groups(['footprint:read'])] + #[Groups(['footprint:read', 'footprint:write'])] protected Collection $attachments; #[ORM\ManyToOne(targetEntity: FootprintAttachment::class)] #[ORM\JoinColumn(name: 'id_preview_attachment', onDelete: 'SET NULL')] - #[Groups(['footprint:read'])] + #[Groups(['footprint:read', 'footprint:write'])] protected ?Attachment $master_picture_attachment = null; /** @@ -111,7 +111,7 @@ class Footprint extends AbstractPartsContainingDBElement */ #[ORM\ManyToOne(targetEntity: FootprintAttachment::class)] #[ORM\JoinColumn(name: 'id_footprint_3d')] - #[Groups(['footprint:read'])] + #[Groups(['footprint:read', 'footprint:write'])] protected ?FootprintAttachment $footprint_3d = null; /** @var Collection diff --git a/src/Entity/Parts/MeasurementUnit.php b/src/Entity/Parts/MeasurementUnit.php index 18c6e2ed..d9437469 100644 --- a/src/Entity/Parts/MeasurementUnit.php +++ b/src/Entity/Parts/MeasurementUnit.php @@ -128,12 +128,12 @@ class MeasurementUnit extends AbstractPartsContainingDBElement #[Assert\Valid] #[ORM\OneToMany(targetEntity: MeasurementUnitAttachment::class, mappedBy: 'element', cascade: ['persist', 'remove'], orphanRemoval: true)] #[ORM\OrderBy(['name' => 'ASC'])] - #[Groups(['measurement_unit:read'])] + #[Groups(['measurement_unit:read', 'measurement_unit:write'])] protected Collection $attachments; #[ORM\ManyToOne(targetEntity: MeasurementUnitAttachment::class)] #[ORM\JoinColumn(name: 'id_preview_attachment', onDelete: 'SET NULL')] - #[Groups(['measurement_unit:read'])] + #[Groups(['measurement_unit:read', 'measurement_unit:write'])] protected ?Attachment $master_picture_attachment = null; /** @var Collection diff --git a/src/Entity/Parts/Part.php b/src/Entity/Parts/Part.php index 3f7f945a..1f7ace26 100644 --- a/src/Entity/Parts/Part.php +++ b/src/Entity/Parts/Part.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace App\Entity\Parts; use ApiPlatform\Metadata\ApiFilter; +use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\Delete; use ApiPlatform\Metadata\Get; @@ -71,7 +72,8 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface; #[ORM\Index(name: 'parts_idx_ipn', columns: ['ipn'])] #[ApiResource( operations: [ - new Get(normalizationContext: ['groups' => ['part:read', 'provider_reference:read', 'api:basic:read', 'part_lot:read', 'orderdetail:read', 'pricedetail:read']], security: 'is_granted("read", object)'), + new Get(normalizationContext: ['groups' => ['part:read', 'provider_reference:read', 'api:basic:read', 'part_lot:read', + 'orderdetail:read', 'pricedetail:read', 'attachment:read']], security: 'is_granted("read", object)'), new GetCollection(security: 'is_granted("@parts.read")'), new Post(securityPostDenormalize: 'is_granted("create", object)'), new Patch(security: 'is_granted("edit", object)'), @@ -115,7 +117,7 @@ class Part extends AttachmentContainingDBElement * @var Collection */ #[Assert\Valid] - #[Groups(['full'])] + #[Groups(['full', 'part:read', 'part:write'])] #[ORM\OneToMany(targetEntity: PartAttachment::class, mappedBy: 'element', cascade: ['persist', 'remove'], orphanRemoval: true)] #[ORM\OrderBy(['name' => 'ASC'])] protected Collection $attachments; @@ -126,6 +128,7 @@ class Part extends AttachmentContainingDBElement #[Assert\Expression('value == null or value.isPicture()', message: 'part.master_attachment.must_be_picture')] #[ORM\ManyToOne(targetEntity: PartAttachment::class)] #[ORM\JoinColumn(name: 'id_preview_attachment', onDelete: 'SET NULL')] + #[Groups(['part:read', 'part:write'])] protected ?Attachment $master_picture_attachment = null; public function __construct() diff --git a/src/Entity/UserSystem/User.php b/src/Entity/UserSystem/User.php index 31a8e6e3..6ce36d90 100644 --- a/src/Entity/UserSystem/User.php +++ b/src/Entity/UserSystem/User.php @@ -253,13 +253,12 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe */ #[ORM\OneToMany(mappedBy: 'element', targetEntity: UserAttachment::class, cascade: ['persist', 'remove'], orphanRemoval: true)] #[ORM\OrderBy(['name' => 'ASC'])] - #[Groups(['user:read'])] - #[ApiProperty(readableLink: false, writableLink: false)] + #[Groups(['user:read', 'user:write'])] protected Collection $attachments; #[ORM\ManyToOne(targetEntity: UserAttachment::class)] #[ORM\JoinColumn(name: 'id_preview_attachment', onDelete: 'SET NULL')] - #[Groups(['user:read'])] + #[Groups(['user:read', 'usser:write'])] protected ?Attachment $master_picture_attachment = null; /** @var \DateTimeInterface|null The time when the backup codes were generated