diff --git a/app/modules/api/Controllers/Client/ClientBase.php b/app/modules/api/Controllers/Client/ClientBase.php new file mode 100644 index 00000000..40296a7d --- /dev/null +++ b/app/modules/api/Controllers/Client/ClientBase.php @@ -0,0 +1,56 @@ +. + */ + +namespace SP\Modules\Api\Controllers\Client; + + +use Klein\Klein; +use SP\Core\Acl\Acl; +use SP\Core\Application; +use SP\Domain\Api\ApiServiceInterface; +use SP\Domain\Client\ClientServiceInterface; +use SP\Modules\Api\Controllers\ControllerBase; +use SP\Modules\Api\Controllers\Help\ClientHelp; + +/** + * Class ClientBase + */ +abstract class ClientBase extends ControllerBase +{ + protected ClientServiceInterface $clientService; + + public function __construct( + Application $application, + Klein $router, + ApiServiceInterface $apiService, + Acl $acl, + ClientServiceInterface $clientService + ) { + parent::__construct($application, $router, $apiService, $acl); + + $this->clientService = $clientService; + + $this->apiService->setHelpClass(ClientHelp::class); + } +} \ No newline at end of file diff --git a/app/modules/api/Controllers/Client/CreateController.php b/app/modules/api/Controllers/Client/CreateController.php new file mode 100644 index 00000000..2e01e5fa --- /dev/null +++ b/app/modules/api/Controllers/Client/CreateController.php @@ -0,0 +1,86 @@ +. + */ + +namespace SP\Modules\Api\Controllers\Client; + + +use Exception; +use SP\Core\Acl\ActionsInterface; +use SP\Core\Events\Event; +use SP\Core\Events\EventMessage; +use SP\DataModel\ClientData; +use SP\Domain\Api\Services\ApiResponse; + +/** + * Class CreateController + */ +final class CreateController extends ClientBase +{ + /** + * createAction + */ + public function createAction(): void + { + try { + $this->setupApi(ActionsInterface::CLIENT_CREATE); + + $clientData = $this->buildClientData(); + + $id = $this->clientService->create($clientData); + + $clientData->setId($id); + + $this->eventDispatcher->notifyEvent( + 'create.client', + new Event( + $this, + EventMessage::factory() + ->addDescription(__u('Client added')) + ->addDetail(__u('Name'), $clientData->getName()) + ->addDetail('ID', $id) + ) + ); + + $this->returnResponse(ApiResponse::makeSuccess($clientData, $id, __('Client added'))); + } catch (Exception $e) { + processException($e); + + $this->returnResponseException($e); + } + } + + /** + * @return \SP\DataModel\ClientData + * @throws \SP\Domain\Common\Services\ServiceException + */ + private function buildClientData(): ClientData + { + $clientData = new ClientData(); + $clientData->setName($this->apiService->getParamString('name', true)); + $clientData->setDescription($this->apiService->getParamString('description')); + $clientData->setIsGlobal($this->apiService->getParamInt('global')); + + return $clientData; + } +} \ No newline at end of file diff --git a/app/modules/api/Controllers/Client/DeleteController.php b/app/modules/api/Controllers/Client/DeleteController.php new file mode 100644 index 00000000..ab14b0c2 --- /dev/null +++ b/app/modules/api/Controllers/Client/DeleteController.php @@ -0,0 +1,71 @@ +. + */ + +namespace SP\Modules\Api\Controllers\Client; + + +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 ClientBase +{ + /** + * deleteAction + */ + public function deleteAction(): void + { + try { + $this->setupApi(ActionsInterface::CLIENT_DELETE); + + $id = $this->apiService->getParamInt('id', true); + + $clientData = $this->clientService->getById($id); + + $this->clientService->delete($id); + + $this->eventDispatcher->notifyEvent( + 'delete.client', + new Event( + $this, + EventMessage::factory() + ->addDescription(__u('Client deleted')) + ->addDetail(__u('Name'), $clientData->getName()) + ->addDetail('ID', $id) + ) + ); + + $this->returnResponse(ApiResponse::makeSuccess($clientData, $id, __('Client deleted'))); + } catch (Exception $e) { + $this->returnResponseException($e); + + processException($e); + } + } +} \ No newline at end of file diff --git a/app/modules/api/Controllers/Client/EditController.php b/app/modules/api/Controllers/Client/EditController.php new file mode 100644 index 00000000..0d90287b --- /dev/null +++ b/app/modules/api/Controllers/Client/EditController.php @@ -0,0 +1,85 @@ +. + */ + +namespace SP\Modules\Api\Controllers\Client; + + +use Exception; +use SP\Core\Acl\ActionsInterface; +use SP\Core\Events\Event; +use SP\Core\Events\EventMessage; +use SP\DataModel\ClientData; +use SP\Domain\Api\Services\ApiResponse; + +/** + * Class EditController + */ +final class EditController extends ClientBase +{ + /** + * editAction + */ + public function editAction(): void + { + try { + $this->setupApi(ActionsInterface::CLIENT_EDIT); + + $clientData = $this->buildClientData(); + + $this->clientService->update($clientData); + + $this->eventDispatcher->notifyEvent( + 'edit.client', + new Event( + $this, + EventMessage::factory() + ->addDescription(__u('Client updated')) + ->addDetail(__u('Name'), $clientData->getName()) + ->addDetail('ID', $clientData->getId()) + ) + ); + + $this->returnResponse(ApiResponse::makeSuccess($clientData, $clientData->getId(), __('Client updated'))); + } catch (Exception $e) { + processException($e); + + $this->returnResponseException($e); + } + } + + /** + * @return \SP\DataModel\ClientData + * @throws \SP\Domain\Common\Services\ServiceException + */ + private function buildClientData(): ClientData + { + $clientData = new ClientData(); + $clientData->setId($this->apiService->getParamInt('id', true)); + $clientData->setName($this->apiService->getParamString('name', true)); + $clientData->setDescription($this->apiService->getParamString('description')); + $clientData->setIsGlobal($this->apiService->getParamInt('global')); + + return $clientData; + } +} \ No newline at end of file diff --git a/app/modules/api/Controllers/Client/SearchController.php b/app/modules/api/Controllers/Client/SearchController.php new file mode 100644 index 00000000..43e0a9d5 --- /dev/null +++ b/app/modules/api/Controllers/Client/SearchController.php @@ -0,0 +1,77 @@ +. + */ + +namespace SP\Modules\Api\Controllers\Client; + + +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 ClientBase +{ + /** + * searchAction + */ + public function searchAction(): void + { + try { + $this->setupApi(ActionsInterface::CLIENT_SEARCH); + + $itemSearchData = $this->buildSearchData(); + + $this->eventDispatcher->notifyEvent('search.client', new Event($this)); + + $this->returnResponse( + ApiResponse::makeSuccess( + $this->clientService->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/Client/ViewController.php b/app/modules/api/Controllers/Client/ViewController.php new file mode 100644 index 00000000..3a83b7a3 --- /dev/null +++ b/app/modules/api/Controllers/Client/ViewController.php @@ -0,0 +1,96 @@ +. + */ + +namespace SP\Modules\Api\Controllers\Client; + +use Exception; +use League\Fractal\Resource\Item; +use SP\Core\Acl\ActionsInterface; +use SP\Core\Events\Event; +use SP\Core\Events\EventMessage; +use SP\Domain\Api\Services\ApiResponse; +use SP\Domain\Client\Out\ClientAdapter; +use SP\Util\Util; + +/** + * Class ViewController + */ +final class ViewController extends ClientBase +{ + /** + * viewAction + */ + public function viewAction(): void + { + try { + $this->setupApi(ActionsInterface::CLIENT_VIEW); + + $id = $this->apiService->getParamInt('id', true); + + $customFields = Util::boolval($this->apiService->getParamString('customFields')); + + $clientData = $this->clientService->getById($id); + + $this->eventDispatcher->notifyEvent( + 'show.client', + new Event($this) + ); + + $this->eventDispatcher->notifyEvent( + 'show.client', + new Event( + $this, + EventMessage::factory() + ->addDescription(__u('Client displayed')) + ->addDetail(__u('Name'), $clientData->getName()) + ->addDetail('ID', $id) + ) + ); + + if ($customFields) { + $this->apiService->requireMasterPass(); + } + + $out = $this->fractal + ->createData( + new Item( + $clientData, + new ClientAdapter($this->configData) + )); + + if ($customFields) { + $this->apiService->requireMasterPass(); + $this->fractal->parseIncludes(['customFields']); + } + + $this->returnResponse( + ApiResponse::makeSuccess($out->toArray(), $id) + ); + } catch (Exception $e) { + processException($e); + + $this->returnResponseException($e); + } + } +} \ No newline at end of file diff --git a/app/modules/api/Controllers/ClientController.php b/app/modules/api/Controllers/ClientController.php deleted file mode 100644 index 08f77e2d..00000000 --- a/app/modules/api/Controllers/ClientController.php +++ /dev/null @@ -1,267 +0,0 @@ -. - */ - -namespace SP\Modules\Api\Controllers; - -use Exception; -use League\Fractal\Resource\Item; -use SP\Core\Acl\ActionsInterface; -use SP\Core\Events\Event; -use SP\Core\Events\EventMessage; -use SP\DataModel\ClientData; -use SP\DataModel\ItemSearchData; -use SP\Domain\Api\Services\ApiResponse; -use SP\Domain\Client\Out\ClientAdapter; -use SP\Domain\Client\Services\ClientService; -use SP\Modules\Api\Controllers\Help\ClientHelp; -use SP\Mvc\Controller\ItemTrait; -use SP\Util\Util; - -/** - * Class ClientController - * - * @package SP\Modules\Api\Controllers - */ -final class ClientController extends ControllerBase -{ - use ItemTrait; - - private ?ClientService $clientService = null; - - /** - * viewAction - */ - public function viewAction(): void - { - try { - $this->setupApi(ActionsInterface::CLIENT_VIEW); - - $id = $this->apiService->getParamInt('id', true); - - $customFields = Util::boolval($this->apiService->getParamString('customFields')); - - $clientData = $this->clientService->getById($id); - - $this->eventDispatcher->notifyEvent( - 'show.client', - new Event($this) - ); - - $this->eventDispatcher->notifyEvent( - 'show.client', - new Event( - $this, - EventMessage::factory() - ->addDescription(__u('Client displayed')) - ->addDetail(__u('Name'), $clientData->getName()) - ->addDetail('ID', $id) - ) - ); - - if ($customFields) { - $this->apiService->requireMasterPass(); - } - - $out = $this->fractal - ->createData( - new Item( - $clientData, - new ClientAdapter($this->configData) - )); - - if ($customFields) { - $this->apiService->requireMasterPass(); - $this->fractal->parseIncludes(['customFields']); - } - - $this->returnResponse( - ApiResponse::makeSuccess($out->toArray(), $id) - ); - } catch (Exception $e) { - processException($e); - - $this->returnResponseException($e); - } - } - - /** - * createAction - */ - public function createAction(): void - { - try { - $this->setupApi(ActionsInterface::CLIENT_CREATE); - - $clientData = new ClientData(); - $clientData->setName($this->apiService->getParamString('name', true)); - $clientData->setDescription($this->apiService->getParamString('description')); - $clientData->setIsGlobal($this->apiService->getParamInt('global')); - - $id = $this->clientService->create($clientData); - - $clientData->setId($id); - - $this->eventDispatcher->notifyEvent( - 'create.client', - new Event( - $this, - EventMessage::factory() - ->addDescription(__u('Client added')) - ->addDetail(__u('Name'), $clientData->getName()) - ->addDetail('ID', $id) - ) - ); - - $this->returnResponse( - ApiResponse::makeSuccess( - $clientData, - $id, - __('Client added') - ) - ); - } catch (Exception $e) { - processException($e); - - $this->returnResponseException($e); - } - } - - /** - * editAction - */ - public function editAction(): void - { - try { - $this->setupApi(ActionsInterface::CLIENT_EDIT); - - $clientData = new ClientData(); - $clientData->setId($this->apiService->getParamInt('id', true)); - $clientData->setName($this->apiService->getParamString('name', true)); - $clientData->setDescription($this->apiService->getParamString('description')); - $clientData->setIsGlobal($this->apiService->getParamInt('global')); - - $this->clientService->update($clientData); - - $this->eventDispatcher->notifyEvent( - 'edit.client', - new Event( - $this, - EventMessage::factory() - ->addDescription(__u('Client updated')) - ->addDetail(__u('Name'), $clientData->getName()) - ->addDetail('ID', $clientData->getId()) - ) - ); - - $this->returnResponse( - ApiResponse::makeSuccess( - $clientData, - $clientData->getId(), - __('Client updated') - ) - ); - } catch (Exception $e) { - processException($e); - - $this->returnResponseException($e); - } - } - - /** - * deleteAction - */ - public function deleteAction(): void - { - try { - $this->setupApi(ActionsInterface::CLIENT_DELETE); - - $id = $this->apiService->getParamInt('id', true); - - $clientData = $this->clientService->getById($id); - - $this->clientService->delete($id); - - $this->eventDispatcher->notifyEvent( - 'delete.client', - new Event( - $this, - EventMessage::factory() - ->addDescription(__u('Client deleted')) - ->addDetail(__u('Name'), $clientData->getName()) - ->addDetail('ID', $id) - ) - ); - - $this->returnResponse( - ApiResponse::makeSuccess( - $clientData, - $id, - __('Client deleted') - ) - ); - } catch (Exception $e) { - $this->returnResponseException($e); - - processException($e); - } - } - - /** - * searchAction - */ - public function searchAction(): void - { - try { - $this->setupApi(ActionsInterface::CLIENT_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.client', - new Event($this) - ); - - $this->returnResponse( - ApiResponse::makeSuccess( - $this->clientService->search($itemSearchData)->getDataAsArray() - ) - ); - } catch (Exception $e) { - processException($e); - - $this->returnResponseException($e); - } - } - - /** - * @throws \SP\Core\Exceptions\InvalidClassException - */ - protected function initialize(): void - { - $this->clientService = $this->dic->get(ClientService::class); - $this->apiService->setHelpClass(ClientHelp::class); - } -} \ No newline at end of file