diff --git a/app/modules/api/Controllers/Tag/CreateController.php b/app/modules/api/Controllers/Tag/CreateController.php new file mode 100644 index 00000000..8566a324 --- /dev/null +++ b/app/modules/api/Controllers/Tag/CreateController.php @@ -0,0 +1,84 @@ +. + */ + +namespace SP\Modules\Api\Controllers\Tag; + + +use Exception; +use SP\Core\Acl\ActionsInterface; +use SP\Core\Events\Event; +use SP\Core\Events\EventMessage; +use SP\DataModel\TagData; +use SP\Domain\Api\Services\ApiResponse; + +/** + * Class CreateController + */ +final class CreateController extends TagBase +{ + /** + * createAction + */ + public function createAction(): void + { + try { + $this->setupApi(ActionsInterface::TAG_CREATE); + + $tagData = $this->buildTagData(); + + $id = $this->tagService->create($tagData); + + $tagData->setId($id); + + $this->eventDispatcher->notifyEvent( + 'create.tag', + new Event( + $this, + EventMessage::factory() + ->addDescription(__u('Tag added')) + ->addDetail(__u('Name'), $tagData->getName()) + ->addDetail('ID', $id) + ) + ); + + $this->returnResponse(ApiResponse::makeSuccess($tagData, $id, __('Tag added'))); + } catch (Exception $e) { + processException($e); + + $this->returnResponseException($e); + } + } + + /** + * @return \SP\DataModel\TagData + * @throws \SP\Domain\Common\Services\ServiceException + */ + private function buildTagData(): TagData + { + $tagData = new TagData(); + $tagData->setName($this->apiService->getParamString('name', true)); + + return $tagData; + } +} \ No newline at end of file diff --git a/app/modules/api/Controllers/Tag/DeleteController.php b/app/modules/api/Controllers/Tag/DeleteController.php new file mode 100644 index 00000000..f2078142 --- /dev/null +++ b/app/modules/api/Controllers/Tag/DeleteController.php @@ -0,0 +1,72 @@ +. + */ + +namespace SP\Modules\Api\Controllers\Tag; + + +use Exception; +use SP\Core\Acl\ActionsInterface; +use SP\Core\Events\Event; +use SP\Core\Events\EventMessage; +use SP\Domain\Api\Services\ApiResponse; + +/** + * Class DeleteController + */ +final class DeleteController extends TagBase +{ + /** + * deleteAction + */ + public function deleteAction(): void + { + try { + $this->setupApi(ActionsInterface::TAG_DELETE); + + $id = $this->apiService->getParamInt('id', true); + + $tagData = $this->tagService->getById($id); + + $this->tagService->delete($id); + + $this->eventDispatcher->notifyEvent( + 'delete.tag', + new Event( + $this, + EventMessage::factory() + ->addDescription(__u('Tag removed')) + ->addDetail(__u('Name'), $tagData->getName()) + ->addDetail('ID', $id) + ) + ); + + $this->returnResponse(ApiResponse::makeSuccess($tagData, $id, __('Tag removed'))); + } catch (Exception $e) { + processException($e); + + $this->returnResponseException($e); + } + } + +} \ No newline at end of file diff --git a/app/modules/api/Controllers/Tag/EditController.php b/app/modules/api/Controllers/Tag/EditController.php new file mode 100644 index 00000000..e37fafe0 --- /dev/null +++ b/app/modules/api/Controllers/Tag/EditController.php @@ -0,0 +1,83 @@ +. + */ + +namespace SP\Modules\Api\Controllers\Tag; + + +use Exception; +use SP\Core\Acl\ActionsInterface; +use SP\Core\Events\Event; +use SP\Core\Events\EventMessage; +use SP\DataModel\TagData; +use SP\Domain\Api\Services\ApiResponse; + +/** + * Class EditController + */ +final class EditController extends TagBase +{ + /** + * editAction + */ + public function editAction(): void + { + try { + $this->setupApi(ActionsInterface::TAG_EDIT); + + $tagData = $this->buildTagData(); + + $this->tagService->update($tagData); + + $this->eventDispatcher->notifyEvent( + 'edit.tag', + new Event( + $this, + EventMessage::factory() + ->addDescription(__u('Tag updated')) + ->addDetail(__u('Name'), $tagData->getName()) + ->addDetail('ID', $tagData->getId()) + ) + ); + + $this->returnResponse(ApiResponse::makeSuccess($tagData, $tagData->getId(), __('Tag updated'))); + } catch (Exception $e) { + processException($e); + + $this->returnResponseException($e); + } + } + + /** + * @return \SP\DataModel\TagData + * @throws \SP\Domain\Common\Services\ServiceException + */ + private function buildTagData(): TagData + { + $tagData = new TagData(); + $tagData->setId($this->apiService->getParamInt('id', true)); + $tagData->setName($this->apiService->getParamString('name', true)); + + return $tagData; + } +} \ No newline at end of file diff --git a/app/modules/api/Controllers/Tag/SearchController.php b/app/modules/api/Controllers/Tag/SearchController.php new file mode 100644 index 00000000..a3f47201 --- /dev/null +++ b/app/modules/api/Controllers/Tag/SearchController.php @@ -0,0 +1,74 @@ +. + */ + +namespace SP\Modules\Api\Controllers\Tag; + + +use Exception; +use SP\Core\Acl\ActionsInterface; +use SP\Core\Events\Event; +use SP\DataModel\ItemSearchData; +use SP\Domain\Api\Services\ApiResponse; + +/** + * Class SearchController + */ +final class SearchController extends TagBase +{ + /** + * searchAction + */ + public function searchAction(): void + { + try { + $this->setupApi(ActionsInterface::TAG_SEARCH); + + $itemSearchData = $this->buildSearchData(); + + $this->eventDispatcher->notifyEvent('search.tag', new Event($this)); + $this->returnResponse( + ApiResponse::makeSuccess( + $this->tagService->search($itemSearchData)->getDataAsArray() + ) + ); + } catch (Exception $e) { + processException($e); + + $this->returnResponseException($e); + } + } + + /** + * @return \SP\DataModel\ItemSearchData + * @throws \SP\Domain\Common\Services\ServiceException + */ + private function buildSearchData(): ItemSearchData + { + $itemSearchData = new ItemSearchData(); + $itemSearchData->setSeachString($this->apiService->getParamString('text')); + $itemSearchData->setLimitCount($this->apiService->getParamInt('count', false, self::SEARCH_COUNT_ITEMS)); + + return $itemSearchData; + } +} \ No newline at end of file diff --git a/app/modules/api/Controllers/Tag/TagBase.php b/app/modules/api/Controllers/Tag/TagBase.php new file mode 100644 index 00000000..ab9468f9 --- /dev/null +++ b/app/modules/api/Controllers/Tag/TagBase.php @@ -0,0 +1,59 @@ +. + */ + +namespace SP\Modules\Api\Controllers\Tag; + + +use Klein\Klein; +use SP\Core\Acl\Acl; +use SP\Core\Application; +use SP\Domain\Api\ApiServiceInterface; +use SP\Domain\Tag\TagServiceInterface; +use SP\Modules\Api\Controllers\ControllerBase; +use SP\Modules\Api\Controllers\Help\TagHelp; + +/** + * Class TagBase + */ +abstract class TagBase extends ControllerBase +{ + protected TagServiceInterface $tagService; + + /** + * @throws \SP\Core\Exceptions\InvalidClassException + */ + public function __construct( + Application $application, + Klein $router, + ApiServiceInterface $apiService, + Acl $acl, + TagServiceInterface $tagService + ) { + parent::__construct($application, $router, $apiService, $acl); + + $this->tagService = $tagService; + + $this->apiService->setHelpClass(TagHelp::class); + } +} \ No newline at end of file diff --git a/app/modules/api/Controllers/Tag/ViewController.php b/app/modules/api/Controllers/Tag/ViewController.php new file mode 100644 index 00000000..096c59bc --- /dev/null +++ b/app/modules/api/Controllers/Tag/ViewController.php @@ -0,0 +1,67 @@ +. + */ + +namespace SP\Modules\Api\Controllers\Tag; + +use Exception; +use SP\Core\Acl\ActionsInterface; +use SP\Core\Events\Event; +use SP\Core\Events\EventMessage; +use SP\Domain\Api\Services\ApiResponse; + +/** + * Class ViewController + * + * @package SP\Modules\Api\Controllers + */ +final class ViewController extends TagBase +{ + /** + * viewAction + */ + public function viewAction(): void + { + try { + $this->setupApi(ActionsInterface::TAG_VIEW); + + $id = $this->apiService->getParamInt('id', true); + $tagData = $this->tagService->getById($id); + + $this->eventDispatcher->notifyEvent( + 'show.tag', + new Event($this, EventMessage::factory() + ->addDescription(__u('Tag displayed')) + ->addDetail(__u('Name'), $tagData->getName()) + ->addDetail('ID', $id) + ) + ); + + $this->returnResponse(ApiResponse::makeSuccess($tagData, $id)); + } catch (Exception $e) { + processException($e); + + $this->returnResponseException($e); + } + } +} \ No newline at end of file diff --git a/app/modules/api/Controllers/TagController.php b/app/modules/api/Controllers/TagController.php deleted file mode 100644 index 2af702aa..00000000 --- a/app/modules/api/Controllers/TagController.php +++ /dev/null @@ -1,229 +0,0 @@ -. - */ - -namespace SP\Modules\Api\Controllers; - -use Exception; -use SP\Core\Acl\ActionsInterface; -use SP\Core\Events\Event; -use SP\Core\Events\EventMessage; -use SP\DataModel\ItemSearchData; -use SP\DataModel\TagData; -use SP\Domain\Api\Services\ApiResponse; -use SP\Domain\Tag\Services\TagService; -use SP\Modules\Api\Controllers\Help\TagHelp; - -/** - * Class TagController - * - * @package SP\Modules\Api\Controllers - */ -final class TagController extends ControllerBase -{ - private ?TagService $tagService = null; - - /** - * viewAction - */ - public function viewAction(): void - { - try { - $this->setupApi(ActionsInterface::TAG_VIEW); - - $id = $this->apiService->getParamInt('id', true); - $tagData = $this->tagService->getById($id); - - $this->eventDispatcher->notifyEvent( - 'show.tag', - new Event($this, EventMessage::factory() - ->addDescription(__u('Tag displayed')) - ->addDetail(__u('Name'), $tagData->getName()) - ->addDetail('ID', $id) - ) - ); - - $this->returnResponse(ApiResponse::makeSuccess($tagData, $id)); - } catch (Exception $e) { - processException($e); - - $this->returnResponseException($e); - } - } - - /** - * createAction - */ - public function createAction(): void - { - try { - $this->setupApi(ActionsInterface::TAG_CREATE); - - $tagData = new TagData(); - $tagData->setName($this->apiService->getParamString('name', true)); - - $id = $this->tagService->create($tagData); - - $tagData->setId($id); - - $this->eventDispatcher->notifyEvent( - 'create.tag', - new Event( - $this, - EventMessage::factory() - ->addDescription(__u('Tag added')) - ->addDetail(__u('Name'), $tagData->getName()) - ->addDetail('ID', $id) - ) - ); - - $this->returnResponse( - ApiResponse::makeSuccess( - $tagData, - $id, - __('Tag added') - ) - ); - } catch (Exception $e) { - processException($e); - - $this->returnResponseException($e); - } - } - - /** - * editAction - */ - public function editAction(): void - { - try { - $this->setupApi(ActionsInterface::TAG_EDIT); - - $tagData = new TagData(); - $tagData->setId($this->apiService->getParamInt('id', true)); - $tagData->setName($this->apiService->getParamString('name', true)); - - $this->tagService->update($tagData); - - $this->eventDispatcher->notifyEvent( - 'edit.tag', - new Event( - $this, - EventMessage::factory() - ->addDescription(__u('Tag updated')) - ->addDetail(__u('Name'), $tagData->getName()) - ->addDetail('ID', $tagData->getId()) - ) - ); - - $this->returnResponse( - ApiResponse::makeSuccess( - $tagData, - $tagData->getId(), - __('Tag updated') - ) - ); - } catch (Exception $e) { - processException($e); - - $this->returnResponseException($e); - } - } - - /** - * deleteAction - */ - public function deleteAction(): void - { - try { - $this->setupApi(ActionsInterface::TAG_DELETE); - - $id = $this->apiService->getParamInt('id', true); - - $tagData = $this->tagService->getById($id); - - $this->tagService->delete($id); - - $this->eventDispatcher->notifyEvent( - 'delete.tag', - new Event( - $this, - EventMessage::factory() - ->addDescription(__u('Tag removed')) - ->addDetail(__u('Name'), $tagData->getName()) - ->addDetail('ID', $id) - ) - ); - - $this->returnResponse( - ApiResponse::makeSuccess( - $tagData, - $id, - __('Tag removed') - ) - ); - } catch (Exception $e) { - processException($e); - - $this->returnResponseException($e); - } - } - - /** - * searchAction - */ - public function searchAction(): void - { - try { - $this->setupApi(ActionsInterface::TAG_SEARCH); - - $itemSearchData = new ItemSearchData(); - $itemSearchData->setSeachString($this->apiService->getParamString('text')); - $itemSearchData->setLimitCount($this->apiService->getParamInt('count', false, self::SEARCH_COUNT_ITEMS)); - - $this->eventDispatcher->notifyEvent( - 'search.tag', - new Event($this) - ); - - $this->returnResponse( - ApiResponse::makeSuccess( - $this->tagService->search($itemSearchData)->getDataAsArray() - ) - ); - } catch (Exception $e) { - processException($e); - - $this->returnResponseException($e); - } - } - - /** - * @throws \SP\Core\Exceptions\InvalidClassException - */ - protected function initialize(): void - { - $this->tagService = $this->dic->get(TagService::class); - $this->apiService->setHelpClass(TagHelp::class); - } -} \ No newline at end of file