From bf5154b955cfdd40fe587859f2cd3922498b0dac Mon Sep 17 00:00:00 2001 From: mattamon Date: Wed, 22 May 2024 10:08:55 +0200 Subject: [PATCH] Add delete controller and service methods, update descriptions of property controllers --- src/Exception/ElementNotFoundException.php | 4 +- src/Note/Controller/DeleteController.php | 70 +++++++++++++++++++ src/Note/Repository/NoteRepository.php | 15 +++- .../Repository/NoteRepositoryInterface.php | 6 ++ src/Note/Service/NoteService.php | 9 +++ src/Note/Service/NoteServiceInterface.php | 6 ++ src/Property/Controller/CreateController.php | 2 +- src/Property/Controller/DeleteController.php | 2 +- .../Controller/Element/UpdateController.php | 2 +- src/Property/Controller/UpdateController.php | 2 +- 10 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 src/Note/Controller/DeleteController.php diff --git a/src/Exception/ElementNotFoundException.php b/src/Exception/ElementNotFoundException.php index 5beb6fa31..5cd915bb1 100644 --- a/src/Exception/ElementNotFoundException.php +++ b/src/Exception/ElementNotFoundException.php @@ -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)); } } diff --git a/src/Note/Controller/DeleteController.php b/src/Note/Controller/DeleteController.php new file mode 100644 index 000000000..8fdbd437b --- /dev/null +++ b/src/Note/Controller/DeleteController.php @@ -0,0 +1,70 @@ +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]); + } +} diff --git a/src/Note/Repository/NoteRepository.php b/src/Note/Repository/NoteRepository.php index 87631d9a9..7509c6a25 100644 --- a/src/Note/Repository/NoteRepository.php +++ b/src/Note/Repository/NoteRepository.php @@ -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; @@ -87,4 +88,16 @@ public function listNotes(NoteElement $noteElement, NoteParameters $parameters): return $list; } -} \ No newline at end of file + + /** + * @throws ElementNotFoundException + */ + public function deleteNote(int $id): void + { + $note = $this->noteResolver->getById($id); + if(!$note) { + throw new ElementNotFoundException($id, 'Note'); + } + $note->delete(); + } +} diff --git a/src/Note/Repository/NoteRepositoryInterface.php b/src/Note/Repository/NoteRepositoryInterface.php index 3668f7996..9e26811da 100644 --- a/src/Note/Repository/NoteRepositoryInterface.php +++ b/src/Note/Repository/NoteRepositoryInterface.php @@ -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; @@ -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; } diff --git a/src/Note/Service/NoteService.php b/src/Note/Service/NoteService.php index eeb163988..9f6f7ae21 100644 --- a/src/Note/Service/NoteService.php +++ b/src/Note/Service/NoteService.php @@ -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; @@ -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)); diff --git a/src/Note/Service/NoteServiceInterface.php b/src/Note/Service/NoteServiceInterface.php index 18a17aeef..65f555c76 100644 --- a/src/Note/Service/NoteServiceInterface.php +++ b/src/Note/Service/NoteServiceInterface.php @@ -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; @@ -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; } diff --git a/src/Property/Controller/CreateController.php b/src/Property/Controller/CreateController.php index c44cc9ed4..de00431ab 100644 --- a/src/Property/Controller/CreateController.php +++ b/src/Property/Controller/CreateController.php @@ -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] diff --git a/src/Property/Controller/DeleteController.php b/src/Property/Controller/DeleteController.php index d56682570..da3a777ea 100644 --- a/src/Property/Controller/DeleteController.php +++ b/src/Property/Controller/DeleteController.php @@ -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] diff --git a/src/Property/Controller/Element/UpdateController.php b/src/Property/Controller/Element/UpdateController.php index e066fef11..2f3af7d25 100644 --- a/src/Property/Controller/Element/UpdateController.php +++ b/src/Property/Controller/Element/UpdateController.php @@ -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] diff --git a/src/Property/Controller/UpdateController.php b/src/Property/Controller/UpdateController.php index d5809d184..3a4ae6354 100644 --- a/src/Property/Controller/UpdateController.php +++ b/src/Property/Controller/UpdateController.php @@ -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]