Skip to content

Commit

Permalink
Add asign tag methods including batch
Browse files Browse the repository at this point in the history
  • Loading branch information
alexz707 committed May 29, 2024
1 parent bd11992 commit e0f24c5
Show file tree
Hide file tree
Showing 13 changed files with 619 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Tag/Attributes/Request/ElementTagRequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Tag\Attributes\Request;

use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioBackendBundle\Tag\Schema\ElementTag;

#[Attribute(Attribute::TARGET_METHOD)]
final class ElementTagRequestBody extends RequestBody
{
public function __construct()
{
parent::__construct(
required: true,
content: new JsonContent(ref: ElementTag::class)
);
}
}
34 changes: 34 additions & 0 deletions src/Tag/Attributes/Request/ElementsTagsCollectionRequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Tag\Attributes\Request;

use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioBackendBundle\Tag\Schema\ElementTagIdCollection;

#[Attribute(Attribute::TARGET_METHOD)]
final class ElementsTagsCollectionRequestBody extends RequestBody
{
public function __construct()
{
parent::__construct(
required: true,
content: new JsonContent(ref: ElementTagIdCollection::class)
);
}
}
79 changes: 79 additions & 0 deletions src/Tag/Controller/Element/AssignController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Tag\Controller\Element;

use OpenApi\Attributes\Post;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\ElementSavingFailedException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\ElementTypeParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Tag\Attributes\Request\ElementTagRequestBody;
use Pimcore\Bundle\StudioBackendBundle\Tag\Request\TagElement;
use Pimcore\Bundle\StudioBackendBundle\Tag\Schema\ElementTag;
use Pimcore\Bundle\StudioBackendBundle\Tag\Service\TagServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\UserPermissions;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class AssignController extends AbstractApiController
{
public function __construct(
SerializerInterface $serializer,
private readonly TagServiceInterface $tagService,
)
{
parent::__construct($serializer);
}

/**
* @throws ElementSavingFailedException|ElementNotFoundException
*/
#[Route('/tags/{elementType}/{id}', name: 'pimcore_studio_api_assign_element_tag', methods: ['POST'])]
//#[IsGranted(UserPermissions::TAGS_ASSIGNMENT->value)]
#[Post(
path: self::API_PATH . '/tags/{elementType}/{id}',
operationId: 'assignTagForElement',
summary: 'Assign tag for element',
security: self::SECURITY_SCHEME,
tags: [Tags::TagsForElement->value]
)]
#[ElementTypeParameter]
#[IdParameter(type: 'element')]
#[ElementTagRequestBody]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED
])]
public function assignTag(
string $elementType,
int $id,
#[MapRequestPayload] ElementTag $assignTag
): JsonResponse
{
$this->tagService->assignTagToElement(new TagElement($elementType, $id), $assignTag->getTagId());
return $this->jsonResponse(['id' => $id]);
}
}
82 changes: 82 additions & 0 deletions src/Tag/Controller/Element/BatchAssignController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Tag\Controller\Element;

use OpenApi\Attributes\Post;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\ElementSavingFailedException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\ElementTypeParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Tag\Attributes\Request\ElementsTagsCollectionRequestBody;
use Pimcore\Bundle\StudioBackendBundle\Tag\Request\BatchCollection;
use Pimcore\Bundle\StudioBackendBundle\Tag\Schema\ElementTagIdCollection;
use Pimcore\Bundle\StudioBackendBundle\Tag\Service\TagServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\UserPermissions;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class BatchAssignController extends AbstractApiController
{
public function __construct(
SerializerInterface $serializer,
private readonly TagServiceInterface $tagService,
)
{
parent::__construct($serializer);
}

/**
* @throws ElementSavingFailedException|ElementNotFoundException
*/
#[Route('/tags/batch/assign/{elementType}', name: 'pimcore_studio_api_batch_assign_elements_tags', methods: ['POST'])]
//#[IsGranted(UserPermissions::TAGS_ASSIGNMENT->value)]
#[Post(
path: self::API_PATH . '/tags/batch/assign/{elementType}',
operationId: 'batchAssignTagsForElements',
summary: 'Batch assign tags for elements',
security: self::SECURITY_SCHEME,
tags: [Tags::TagsForElement->value]
)]
#[ElementTypeParameter]
#[ElementsTagsCollectionRequestBody]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED
])]
public function assignTag(
string $elementType,
#[MapRequestPayload] ElementTagIdCollection $elementTagCollection
): JsonResponse
{
$this->tagService->batchAssignTagsToElements(
new BatchCollection(
$elementType,
$elementTagCollection->getElementIds(),
$elementTagCollection->getTagsIds()
)
);
return $this->jsonResponse(['id' => 0]);
}
}
82 changes: 82 additions & 0 deletions src/Tag/Controller/Element/BatchReplaceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Tag\Controller\Element;

use OpenApi\Attributes\Post;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\ElementSavingFailedException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\ElementTypeParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Tag\Attributes\Request\ElementsTagsCollectionRequestBody;
use Pimcore\Bundle\StudioBackendBundle\Tag\Request\BatchCollection;
use Pimcore\Bundle\StudioBackendBundle\Tag\Schema\ElementTagIdCollection;
use Pimcore\Bundle\StudioBackendBundle\Tag\Service\TagServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\UserPermissions;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class BatchReplaceController extends AbstractApiController
{
public function __construct(
SerializerInterface $serializer,
private readonly TagServiceInterface $tagService,
)
{
parent::__construct($serializer);
}

/**
* @throws ElementSavingFailedException|ElementNotFoundException
*/
#[Route('/tags/batch/replace/{elementType}', name: 'pimcore_studio_api_batch_replace_elements_tags', methods: ['POST'])]
//#[IsGranted(UserPermissions::TAGS_ASSIGNMENT->value)]
#[Post(
path: self::API_PATH . '/tags/batch/replace/{elementType}',
operationId: 'batchReplaceTagsForElements',
summary: 'Batch replace tags for elements',
security: self::SECURITY_SCHEME,
tags: [Tags::TagsForElement->value]
)]
#[ElementTypeParameter]
#[ElementsTagsCollectionRequestBody]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED
])]
public function assignTag(
string $elementType,
#[MapRequestPayload] ElementTagIdCollection $elementTagCollection
): JsonResponse
{
$this->tagService->batchReplaceTagsToElements(
new BatchCollection(
$elementType,
$elementTagCollection->getElementIds(),
$elementTagCollection->getTagsIds()
)
);
return $this->jsonResponse(['id' => 0]);
}
}
79 changes: 79 additions & 0 deletions src/Tag/Controller/Element/UnassignController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Tag\Controller\Element;

use OpenApi\Attributes\Delete;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\ElementSavingFailedException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\ElementTypeParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Tag\Attributes\Request\ElementTagRequestBody;
use Pimcore\Bundle\StudioBackendBundle\Tag\Request\TagElement;
use Pimcore\Bundle\StudioBackendBundle\Tag\Schema\ElementTag;
use Pimcore\Bundle\StudioBackendBundle\Tag\Service\TagServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\UserPermissions;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class UnassignController extends AbstractApiController
{
public function __construct(
SerializerInterface $serializer,
private readonly TagServiceInterface $tagService,
)
{
parent::__construct($serializer);
}

/**
* @throws ElementSavingFailedException|ElementNotFoundException
*/
#[Route('/tags/{elementType}/{id}', name: 'pimcore_studio_api_unassign_element_tag', methods: ['DELETE'])]
//#[IsGranted(UserPermissions::TAGS_ASSIGNMENT->value)]
#[Delete(
path: self::API_PATH . '/tags/{elementType}/{id}',
operationId: 'unassignTagFromElement',
summary: 'Unassign tag from element',
security: self::SECURITY_SCHEME,
tags: [Tags::TagsForElement->value]
)]
#[ElementTypeParameter]
#[IdParameter(type: 'element')]
#[ElementTagRequestBody]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED
])]
public function unassignTag(
string $elementType,
int $id,
#[MapRequestPayload] ElementTag $unassignTag
): JsonResponse
{
$this->tagService->unassignTagFromElement(new TagElement($elementType, $id), $unassignTag->getTagId());
return $this->jsonResponse(['id' => $id]);
}
}
Loading

0 comments on commit e0f24c5

Please sign in to comment.