Skip to content

Commit

Permalink
Add note controllers, service, hydrator, repo, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed May 21, 2024
1 parent f565e0d commit 256aae2
Show file tree
Hide file tree
Showing 23 changed files with 1,022 additions and 4 deletions.
25 changes: 25 additions & 0 deletions config/notes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
Pimcore\Bundle\StudioBackendBundle\Note\Controller\:
resource: '../src/Note/Controller'
public: true
tags: [ 'controller.service_arguments' ]

# Hydrators
Pimcore\Bundle\StudioBackendBundle\Note\Hydrator\NoteHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Note\Hydrator\NoteHydrator

Pimcore\Bundle\StudioBackendBundle\Note\Extractor\NoteDataExtractorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Note\Extractor\NoteDataExtractor

Pimcore\Bundle\StudioBackendBundle\Note\Repository\NoteRepositoryInterface:
class: Pimcore\Bundle\StudioBackendBundle\Note\Repository\NoteRepository

Pimcore\Bundle\StudioBackendBundle\Note\Service\NoteServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Note\Service\NoteService
1 change: 1 addition & 0 deletions src/DependencyInjection/PimcoreStudioBackendExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('factories.yaml');
$loader->load('filters.yaml');
$loader->load('icon.yaml');
$loader->load('notes.yaml');
$loader->load('open_api.yaml');
$loader->load('properties.yaml');
$loader->load('security.yaml');
Expand Down
34 changes: 34 additions & 0 deletions src/Note/Attributes/Request/CreateNoteRequestBody.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\Note\Attributes\Request;

use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioBackendBundle\Note\Schema\CreateNote;

#[Attribute(Attribute::TARGET_METHOD)]
final class CreateNoteRequestBody extends RequestBody
{
public function __construct()
{
parent::__construct(
required: true,
content: new JsonContent(ref: CreateNote::class)
);
}
}
90 changes: 90 additions & 0 deletions src/Note/Controller/Element/CollectionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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\Note\Controller\Element;

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteElement;
use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteParameters;
use Pimcore\Bundle\StudioBackendBundle\Note\Service\NoteServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\ElementTypeParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\FilterParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\PageParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\PageSizeParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\BadRequestResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\MethodNotAllowedResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnauthorizedResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnprocessableContentResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnsupportedMediaTypeResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\PaginatedResponseTrait;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class CollectionController extends AbstractApiController
{
use PaginatedResponseTrait;

public function __construct(
SerializerInterface $serializer,
private readonly NoteServiceInterface $noteService
)
{
parent::__construct($serializer);
}



#[Route('/notes/{elementType}/{id}', name: 'pimcore_studio_api_get_element_notes', methods: ['GET'])]
#[Get(
path: self::API_PATH . '/notes/{elementType}/{id}',
operationId: 'getNotesForElementByTypeAndId',
summary: 'Get notes for an element',
security: self::SECURITY_SCHEME,
tags: [Tags::Notes->name]
)]
#[ElementTypeParameter]
#[IdParameter(type: 'element')]
#[PageParameter]
#[PageSizeParameter(50)]
#[FilterParameter('notes')]
#[BadRequestResponse]
#[UnauthorizedResponse]
#[MethodNotAllowedResponse]
#[UnsupportedMediaTypeResponse]
#[UnprocessableContentResponse]
public function getNotes(
string $elementType,
int $id,
#[MapQueryString] NoteParameters $parameters = new NoteParameters()
): JsonResponse
{
$collection = $this->noteService->listNotes(new NoteElement($elementType, $id), $parameters);

return $this->getPaginatedCollection(
$this->serializer,
$collection->getItems(),
$collection->getTotalItems()
);
}
}
75 changes: 75 additions & 0 deletions src/Note/Controller/Element/CreateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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\Note\Controller\Element;

use OpenApi\Attributes\Post;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Note\Attributes\Request\CreateNoteRequestBody;
use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteElement;
use Pimcore\Bundle\StudioBackendBundle\Note\Schema\CreateNote;
use Pimcore\Bundle\StudioBackendBundle\Note\Service\NoteServiceInterface;
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\Error\BadRequestResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\MethodNotAllowedResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnauthorizedResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnprocessableContentResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnsupportedMediaTypeResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

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

#[Route('/notes/{elementType}/{id}', name: 'pimcore_studio_api_create_element_note', methods: ['POST'])]
#[Post(
path: self::API_PATH . '/notes/{elementType}/{id}',
operationId: 'createNoteForElement',
summary: 'Creating new note for element',
security: self::SECURITY_SCHEME,
tags: [Tags::Notes->name]
)]
#[ElementTypeParameter]
#[IdParameter(type: 'element')]
#[CreateNoteRequestBody]
#[BadRequestResponse]
#[UnauthorizedResponse]
#[MethodNotAllowedResponse]
#[UnsupportedMediaTypeResponse]
#[UnprocessableContentResponse]
public function createNote(
string $elementType,
int $id,
#[MapRequestPayload] CreateNote $createNote): JsonResponse
{
$note = $this->noteService->createNote(new NoteElement($elementType, $id), $createNote);
return $this->jsonResponse(['id' => $note->getId()]);
}
}
104 changes: 104 additions & 0 deletions src/Note/Extractor/NoteDataExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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\Note\Extractor;

use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface;
use Pimcore\Bundle\StaticResolverBundle\Models\User\UserResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Note\Schema\NoteUser;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\Element\Note as CoreNote;

/**
* @internal
*/
final readonly class NoteDataExtractor implements NoteDataExtractorInterface
{
public function __construct(
private ServiceResolverInterface $serviceResolver,
private UserResolverInterface $userResolver
)
{
}

public function extractCPath(CoreNote $note) : string
{
if (!$note->getCid() || !$note->getCtype()) {
return '';
}
$element = $this->serviceResolver->getElementById($note->getCtype(), $note->getCid());

if (!$element) {
return '';
}

return $element->getRealFullPath();
}

public function extractUserData(CoreNote $note) : NoteUser
{
$emptyUser = new NoteUser();
if(!$note->getUser()) {
return $emptyUser;
}

$user = $this->userResolver->getById($note->getUser());

if(!$user) {
return $emptyUser;
}

return new NoteUser(
$user->getId(),
$user->getName(),
);
}

public function extractData(CoreNote $note): array
{
// prepare key-values
$keyValues = [];
foreach ($note->getData() as $name => $d) {

$type = $d['type'];

$data = match($type) {
'document', 'object', 'asset' => $this->extractElementData($d['data']),
'date' => is_object($d['data']) ? $d['data']->getTimestamp() : $d['data'],
default => $d['data'],
};

$keyValue = [
'type' => $type,
'name' => $name,
'data' => $data,
];

$keyValues[] = $keyValue;
}

return $keyValues;
}

private function extractElementData(ElementInterface $element): array
{
return [
'id' => $element->getId(),
'path' => $element->getRealFullPath(),
'type' => $element->getType(),
];
}
}
32 changes: 32 additions & 0 deletions src/Note/Extractor/NoteDataExtractorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\Note\Extractor;

use Pimcore\Bundle\StudioBackendBundle\Note\Schema\NoteUser;
use Pimcore\Model\Element\Note as CoreNote;

/**
* @internal
*/
interface NoteDataExtractorInterface
{
public function extractUserData(CoreNote $note): NoteUser;

public function extractCPath(CoreNote $note): string;

public function extractData(CoreNote $note): array;
}
Loading

0 comments on commit 256aae2

Please sign in to comment.