Repository navigation
Add Attachment Admin API endpoints #295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
boo-code
wants to merge
4
commits into
PrestaShop:dev
Choose a base branch
from
boo-code:feat/attachment-endpoints
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9ba0506
Add Attachment Admin API endpoints
e92fa04
Add attachment list and bulk-delete endpoints
873e185
Skip the attachment POST tests on cores without the for-editing CQRS fix
7ba30d4
Address review: gate the item operations on 9.2.1, update through a m…
boo-code File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| <?php | ||
| /** | ||
| * Copyright since 2007 PrestaShop SA and Contributors | ||
| * PrestaShop is an International Registered Trademark & Property of PrestaShop SA | ||
| * | ||
| * NOTICE OF LICENSE | ||
| * | ||
| * This source file is subject to the Academic Free License version 3.0 | ||
| * that is bundled with this package in the file LICENSE.md. | ||
| * It is also available through the world-wide-web at this URL: | ||
| * https://opensource.org/licenses/AFL-3.0 | ||
| * If you did not receive a copy of the license and are unable to | ||
| * obtain it through the world-wide-web, please send an email | ||
| * to license@prestashop.com so we can send you a copy immediately. | ||
| * | ||
| * @author PrestaShop SA and Contributors <contact@prestashop.com> | ||
| * @copyright Since 2007 PrestaShop SA and Contributors | ||
| * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Attachment; | ||
|
|
||
| use ApiPlatform\Metadata\ApiProperty; | ||
| use ApiPlatform\Metadata\ApiResource; | ||
| use PrestaShop\PrestaShop\Core\Domain\Attachment\Command\AddAttachmentCommand; | ||
| use PrestaShop\PrestaShop\Core\Domain\Attachment\Command\DeleteAttachmentCommand; | ||
| use PrestaShop\PrestaShop\Core\Domain\Attachment\Command\EditAttachmentCommand; | ||
| use PrestaShop\PrestaShop\Core\Domain\Attachment\Exception\AttachmentConstraintException; | ||
| use PrestaShop\PrestaShop\Core\Domain\Attachment\Exception\AttachmentNotFoundException; | ||
| use PrestaShop\PrestaShop\Core\Domain\Attachment\Exception\EmptyFileException; | ||
| use PrestaShop\PrestaShop\Core\Domain\Attachment\Query\GetAttachmentForEditing; | ||
| use PrestaShopBundle\ApiPlatform\Metadata\CQRSCreate; | ||
| use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete; | ||
| use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet; | ||
| use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate; | ||
| use PrestaShopBundle\ApiPlatform\Metadata\LocalizedValue; | ||
| use Symfony\Component\HttpFoundation\File\File; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; | ||
| use Symfony\Component\Validator\Constraints as Assert; | ||
|
|
||
| #[ApiResource( | ||
| operations: [ | ||
| new CQRSGet( | ||
| uriTemplate: '/attachments/{attachmentId}', | ||
| requirements: ['attachmentId' => '\d+'], | ||
| CQRSQuery: GetAttachmentForEditing::class, | ||
| scopes: ['attachment_read'], | ||
| CQRSQueryMapping: self::QUERY_MAPPING, | ||
| extraProperties: [ | ||
| // GetAttachmentForEditing accepts the attachmentId the API builds it from only from PrestaShop 9.2.1 | ||
| // (PrestaShop/PrestaShop#42124), older cores answer 500 | ||
| 'minVersion' => '9.2.1', | ||
| ], | ||
| ), | ||
| new CQRSCreate( | ||
| uriTemplate: '/attachments', | ||
| validationContext: ['groups' => ['Default', 'Create']], | ||
| CQRSCommand: AddAttachmentCommand::class, | ||
| CQRSQuery: GetAttachmentForEditing::class, | ||
| scopes: ['attachment_write'], | ||
| inputFormats: ['multipart' => ['multipart/form-data']], | ||
| // Form data values are all strings so we disable type enforcement. | ||
| denormalizationContext: [ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true], | ||
| CQRSQueryMapping: self::QUERY_MAPPING, | ||
| CQRSCommandMapping: self::CREATE_COMMAND_MAPPING, | ||
| extraProperties: [ | ||
| // GetAttachmentForEditing accepts the attachmentId the API builds it from only from PrestaShop 9.2.1 | ||
| // (PrestaShop/PrestaShop#42124), older cores answer 500 | ||
| 'minVersion' => '9.2.1', | ||
| ], | ||
| ), | ||
| // A POST, since PHP only parses the multipart body of a POST request. EditAttachmentHandler writes the | ||
| // names and the descriptions whatever was sent, so both are required, the file is optional. | ||
| new CQRSUpdate( | ||
| method: CQRSUpdate::METHOD_POST, | ||
| uriTemplate: '/attachments/{attachmentId}', | ||
| requirements: ['attachmentId' => '\d+'], | ||
| status: Response::HTTP_OK, | ||
| read: false, | ||
| validationContext: ['groups' => ['Default', 'Update']], | ||
| CQRSCommand: EditAttachmentCommand::class, | ||
| CQRSQuery: GetAttachmentForEditing::class, | ||
| scopes: ['attachment_write'], | ||
| inputFormats: ['multipart' => ['multipart/form-data']], | ||
| denormalizationContext: [ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true], | ||
| CQRSQueryMapping: self::QUERY_MAPPING, | ||
| CQRSCommandMapping: self::UPDATE_COMMAND_MAPPING, | ||
| extraProperties: [ | ||
| // GetAttachmentForEditing accepts the attachmentId the API builds it from only from PrestaShop 9.2.1 | ||
| // (PrestaShop/PrestaShop#42124), older cores answer 500 | ||
| 'minVersion' => '9.2.1', | ||
| ], | ||
| ), | ||
| new CQRSDelete( | ||
| uriTemplate: '/attachments/{attachmentId}', | ||
| requirements: ['attachmentId' => '\d+'], | ||
| CQRSCommand: DeleteAttachmentCommand::class, | ||
| scopes: ['attachment_write'], | ||
| ), | ||
| ], | ||
| normalizationContext: ['skip_null_values' => false], | ||
| exceptionToStatus: [ | ||
| AttachmentNotFoundException::class => Response::HTTP_NOT_FOUND, | ||
| AttachmentConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY, | ||
| EmptyFileException::class => Response::HTTP_UNPROCESSABLE_ENTITY, | ||
| ], | ||
| )] | ||
| class Attachment | ||
| { | ||
| #[ApiProperty(identifier: true)] | ||
| public int $attachmentId; | ||
|
|
||
| #[LocalizedValue] | ||
| #[Assert\NotBlank(groups: ['Create', 'Update'])] | ||
| public array $names; | ||
|
|
||
| #[LocalizedValue] | ||
| #[Assert\NotNull(groups: ['Create', 'Update'])] | ||
| public array $descriptions; | ||
|
|
||
| /** | ||
| * Write-only: the file, sent as the `file` part of the multipart request. Required to add an attachment, | ||
| * optional on the update, which keeps the current file without it. The responses expose the stored file | ||
| * through fileName. Not nullable, like VirtualProductFile::$file: an initialized null is normalized into the | ||
| * response despite readable: false. | ||
| */ | ||
| #[ApiProperty(readable: false, writable: false)] | ||
| #[Assert\NotNull(groups: ['Create'])] | ||
| public File $file; | ||
|
|
||
| public string $fileName; | ||
|
|
||
| public const QUERY_MAPPING = [ | ||
| '[name]' => '[names]', | ||
| '[description]' => '[descriptions]', | ||
| ]; | ||
|
|
||
| // AddAttachmentCommand takes the localized names/descriptions as constructor arguments and the | ||
| // file information through the multi-parameter setter setFileInformation(pathName, fileSize, | ||
| // mimeType, originalName). The mapper fills that setter from a nested "fileInformation" object | ||
| // whose keys match the parameter names, reading them from the uploaded file. | ||
| public const CREATE_COMMAND_MAPPING = [ | ||
| '[names]' => '[localizedNames]', | ||
| '[descriptions]' => '[localizedDescriptions]', | ||
| '[file].pathName' => '[fileInformation][pathName]', | ||
| '[file].size' => '[fileInformation][fileSize]', | ||
| '[file].mimeType' => '[fileInformation][mimeType]', | ||
| '[file].clientOriginalName' => '[fileInformation][originalName]', | ||
| ]; | ||
|
|
||
| // EditAttachmentCommand exposes the file through setFileInfo(pathName, mimeType, originalFileName, | ||
| // fileSize) - a different setter name and parameter order than the add command. | ||
| public const UPDATE_COMMAND_MAPPING = [ | ||
| '[names]' => '[localizedNames]', | ||
| '[descriptions]' => '[localizedDescriptions]', | ||
| '[file].pathName' => '[fileInfo][pathName]', | ||
| '[file].mimeType' => '[fileInfo][mimeType]', | ||
| '[file].clientOriginalName' => '[fileInfo][originalFileName]', | ||
| '[file].size' => '[fileInfo][fileSize]', | ||
| ]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <?php | ||
| /** | ||
| * Copyright since 2007 PrestaShop SA and Contributors | ||
| * PrestaShop is an International Registered Trademark & Property of PrestaShop SA | ||
| * | ||
| * NOTICE OF LICENSE | ||
| * | ||
| * This source file is subject to the Academic Free License version 3.0 | ||
| * that is bundled with this package in the file LICENSE.md. | ||
| * It is also available through the world-wide-web at this URL: | ||
| * https://opensource.org/licenses/AFL-3.0 | ||
| * If you did not receive a copy of the license and are unable to | ||
| * obtain it through the world-wide-web, please send an email | ||
| * to license@prestashop.com so we can send you a copy immediately. | ||
| * | ||
| * @author PrestaShop SA and Contributors <contact@prestashop.com> | ||
| * @copyright Since 2007 PrestaShop SA and Contributors | ||
| * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Attachment; | ||
|
|
||
| use ApiPlatform\Metadata\ApiProperty; | ||
| use ApiPlatform\Metadata\ApiResource; | ||
| use PrestaShop\PrestaShop\Core\Search\Filters\AttachmentFilters; | ||
| use PrestaShopBundle\ApiPlatform\Metadata\PaginatedList; | ||
|
|
||
| #[ApiResource( | ||
| operations: [ | ||
| new PaginatedList( | ||
| uriTemplate: '/attachments', | ||
| scopes: [ | ||
| 'attachment_read', | ||
| ], | ||
| ApiResourceMapping: self::MAPPING, | ||
| // The undecorated factory: the decorator turns the size and the product count into display labels | ||
| gridDataFactory: 'prestashop.core.grid.data_factory.attachment', | ||
| filtersClass: AttachmentFilters::class, | ||
| filtersMapping: [ | ||
| '[attachmentId]' => '[id_attachment]', | ||
| ], | ||
| ), | ||
| ] | ||
| )] | ||
| class AttachmentList | ||
| { | ||
| #[ApiProperty(identifier: true)] | ||
| public int $attachmentId; | ||
|
|
||
| public string $name; | ||
|
|
||
| public string $file; | ||
|
|
||
| #[ApiProperty(openapiContext: ['description' => 'Size of the file, in bytes.'])] | ||
| public int $fileSize; | ||
|
|
||
| #[ApiProperty(openapiContext: ['description' => 'Number of products the attachment is associated with.'])] | ||
| public int $products; | ||
|
|
||
| public const MAPPING = [ | ||
| '[id_attachment]' => '[attachmentId]', | ||
| '[file_size]' => '[fileSize]', | ||
| ]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
| /** | ||
| * Copyright since 2007 PrestaShop SA and Contributors | ||
| * PrestaShop is an International Registered Trademark & Property of PrestaShop SA | ||
| * | ||
| * NOTICE OF LICENSE | ||
| * | ||
| * This source file is subject to the Academic Free License version 3.0 | ||
| * that is bundled with this package in the file LICENSE.md. | ||
| * It is also available through the world-wide-web at this URL: | ||
| * https://opensource.org/licenses/AFL-3.0 | ||
| * If you did not receive a copy of the license and are unable to | ||
| * obtain it through the world-wide-web, please send an email | ||
| * to license@prestashop.com so we can send you a copy immediately. | ||
| * | ||
| * @author PrestaShop SA and Contributors <contact@prestashop.com> | ||
| * @copyright Since 2007 PrestaShop SA and Contributors | ||
| * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Attachment; | ||
|
|
||
| use ApiPlatform\Metadata\ApiProperty; | ||
| use ApiPlatform\Metadata\ApiResource; | ||
| use PrestaShop\PrestaShop\Core\Domain\Attachment\Command\BulkDeleteAttachmentsCommand; | ||
| use PrestaShop\PrestaShop\Core\Domain\Attachment\Exception\AttachmentNotFoundException; | ||
| use PrestaShop\PrestaShop\Core\Domain\Attachment\Exception\BulkDeleteAttachmentsException; | ||
| use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\Validator\Constraints as Assert; | ||
|
|
||
| #[ApiResource( | ||
| operations: [ | ||
| new CQRSDelete( | ||
| uriTemplate: '/attachments/bulk-delete', | ||
| CQRSCommand: BulkDeleteAttachmentsCommand::class, | ||
| allowEmptyBody: false, | ||
| scopes: [ | ||
| 'attachment_write', | ||
| ], | ||
| ), | ||
| ], | ||
| exceptionToStatus: [ | ||
| AttachmentNotFoundException::class => Response::HTTP_NOT_FOUND, | ||
| // The handler deletes what it can and reports the failed ids together at the end | ||
| BulkDeleteAttachmentsException::class => Response::HTTP_UNPROCESSABLE_ENTITY, | ||
| ], | ||
| )] | ||
| class BulkAttachments | ||
| { | ||
| /** | ||
| * @var int[] | ||
| */ | ||
| #[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer'], 'example' => [1, 3]])] | ||
| #[Assert\NotBlank] | ||
| #[Assert\Count(min: 1)] | ||
| #[Assert\All([new Assert\Type('integer'), new Assert\Positive()])] | ||
| public array $attachmentIds = []; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BulkDeleteAttachmentsHandlercollects each failure and throwsBulkDeleteAttachmentsExceptionat the end, which is not mapped, so an unknown id answers 500 (and theAttachmentNotFoundExceptionentry never applies);[0]also ends in a 500 through theAttachmentConstraintExceptionofAttachmentId. Could you add#[Assert\All([new Assert\Type('integer'), new Assert\Positive()])]andallowEmptyBody: falseas inBulkCountries, mapBulkDeleteAttachmentsException(422 seems the closest fit) and test both cases?