Skip to content

Commit

Permalink
Add delete controller and service methods, update descriptions of pro…
Browse files Browse the repository at this point in the history
…perty controllers
  • Loading branch information
mattamon committed May 22, 2024
1 parent 3749c81 commit bf5154b
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Exception/ElementNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/
final class ElementNotFoundException extends AbstractApiException
{
public function __construct(int $id)
public function __construct(int $id, string $type = 'Element')
{
parent::__construct(404, 'Element with ID ' . $id . ' not found');
parent::__construct(404, sprintf('%s with ID %d not found', $type, $id));
}
}
70 changes: 70 additions & 0 deletions src/Note/Controller/DeleteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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;

use OpenApi\Attributes\Delete;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Note\Service\NoteServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content\IdJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\UserPermissions;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;

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

#[Route('/notes/{id}', name: 'pimcore_studio_api_delete_note', methods: ['DELETE'])]
#[IsGranted(UserPermissions::NOTES_EVENTS->value)]
#[Delete(
path: self::API_PATH . '/notes/{id}',
operationId: 'deleteNote',
summary: 'Deleting note by id',
security: self::SECURITY_SCHEME,
tags: [Tags::Notes->name]
)]
#[IdParameter]
#[SuccessResponse(
description: 'Id of the note that got deleted',
content: new IdJson('ID of deleted note')
)]
#[DefaultResponses([
HttpResponseCodes::NOT_FOUND,
HttpResponseCodes::UNAUTHORIZED
])]
public function deleteNote(int $id): JsonResponse
{
$this->noteService->deleteNote($id);
return $this->jsonResponse(['id' => $id]);
}
}
15 changes: 14 additions & 1 deletion src/Note/Repository/NoteRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Pimcore\Bundle\StudioBackendBundle\Note\Repository;

use Pimcore\Bundle\StaticResolverBundle\Models\Element\NoteResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteElement;
use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteParameters;
use Pimcore\Bundle\StudioBackendBundle\Note\Schema\CreateNote;
Expand Down Expand Up @@ -87,4 +88,16 @@ public function listNotes(NoteElement $noteElement, NoteParameters $parameters):

return $list;
}
}

/**
* @throws ElementNotFoundException
*/
public function deleteNote(int $id): void
{
$note = $this->noteResolver->getById($id);
if(!$note) {
throw new ElementNotFoundException($id, 'Note');
}
$note->delete();
}
}
6 changes: 6 additions & 0 deletions src/Note/Repository/NoteRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace Pimcore\Bundle\StudioBackendBundle\Note\Repository;

use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteElement;
use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteParameters;
use Pimcore\Bundle\StudioBackendBundle\Note\Schema\CreateNote;
Expand All @@ -30,4 +31,9 @@ interface NoteRepositoryInterface
public function createNote(NoteElement $noteElement, CreateNote $createNote): Note;

public function listNotes(NoteElement $noteElement, NoteParameters $parameters): NoteListing;

/**
* @throws ElementNotFoundException
*/
public function deleteNote(int $id): void;
}
9 changes: 9 additions & 0 deletions src/Note/Service/NoteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace Pimcore\Bundle\StudioBackendBundle\Note\Service;

use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Note\Hydrator\NoteHydratorInterface;
use Pimcore\Bundle\StudioBackendBundle\Note\Repository\NoteRepositoryInterface;
use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteElement;
Expand Down Expand Up @@ -60,6 +61,14 @@ public function listNotes(NoteElement $noteElement, NoteParameters $parameters):
);
}

/**
* @throws ElementNotFoundException
*/
public function deleteNote(int $id): void
{
$this->noteRepository->deleteNote($id);
}

private function getNote(int $id): Note
{
return $this->noteHydrator->hydrate($this->noteRepository->getNote($id));

Check failure on line 74 in src/Note/Service/NoteService.php

View workflow job for this annotation

GitHub Actions / static-analysis / Static Analysis with PHPStan (8.2, highest, 11.x-dev as 11.99.9, true)

Call to an undefined method Pimcore\Bundle\StudioBackendBundle\Note\Repository\NoteRepositoryInterface::getNote().

Check failure on line 74 in src/Note/Service/NoteService.php

View workflow job for this annotation

GitHub Actions / static-analysis / Static Analysis with PHPStan (8.2, highest, false)

Call to an undefined method Pimcore\Bundle\StudioBackendBundle\Note\Repository\NoteRepositoryInterface::getNote().
Expand Down
6 changes: 6 additions & 0 deletions src/Note/Service/NoteServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace Pimcore\Bundle\StudioBackendBundle\Note\Service;

use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteElement;
use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteParameters;
use Pimcore\Bundle\StudioBackendBundle\Note\Response\Collection;
Expand All @@ -30,4 +31,9 @@ interface NoteServiceInterface
public function createNote(NoteElement $noteElement, CreateNote $createNote): Note;

public function listNotes(NoteElement $noteElement, NoteParameters $parameters): Collection;

/**
* @throws ElementNotFoundException
*/
public function deleteNote(int $id): void;
}
2 changes: 1 addition & 1 deletion src/Property/Controller/CreateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(
tags: [Tags::Properties->name]
)]
#[SuccessResponse(
description: 'Element Properties data as json',
description: 'Created predefined property',
content: new JsonContent(ref: PredefinedProperty::class, type: 'object')
)]
#[BadRequestResponse]
Expand Down
2 changes: 1 addition & 1 deletion src/Property/Controller/DeleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(
)]
#[IdParameter(type: 'property', schema: new Schema(type: 'string', example: 'alpha-numerical'))]
#[SuccessResponse(
description: 'Element Properties data as json',
description: 'Id of deleted property',
content: new IdJson('ID of deleted property')
)]
#[UnauthorizedResponse]
Expand Down
2 changes: 1 addition & 1 deletion src/Property/Controller/Element/UpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct(
#[IdParameter(type: 'element')]
#[ElementPropertyRequestBody]
#[SuccessResponse(
description: 'Element Properties data as json',
description: 'Updated Element Properties data as json',
content: new ItemsJson(ElementProperty::class)
)]
#[UnauthorizedResponse]
Expand Down
2 changes: 1 addition & 1 deletion src/Property/Controller/UpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(
#[IdParameter(type: 'property', schema: new Schema(type: 'string', example: 'alpha-numerical'))]
#[PredefinedPropertyRequestBody]
#[SuccessResponse(
description: 'Updated property',
description: 'Updated predefined property',
content: new JsonContent(ref: PredefinedProperty::class, type: 'object')
)]
#[BadRequestResponse]
Expand Down

0 comments on commit bf5154b

Please sign in to comment.