From c8a2513b87774ee101b225be4315b0f0f39151fe Mon Sep 17 00:00:00 2001 From: Flo Date: Sat, 24 Feb 2024 21:03:10 +0100 Subject: [PATCH] tagList first version --- bin/createApi.php | 18 +++---- bin/createPipeline.php | 53 ++++++++++++++++++- composer.development.json | 4 ++ config/config.php | 2 + .../External/TagList/config/routes.php | 16 ++++++ .../TagList/config/service_manager.php | 17 ++++++ .../External/TagList/src/ConfigProvider.php | 16 ++++++ .../TagList/src/Handler/ReadListHandler.php | 38 +++++++++++++ .../ReadListResponseFormatter.php | 32 +++++++++++ .../VideoList/src/Handler/ReadListHandler.php | 2 +- .../Business/src/Repository/TagRepository.php | 22 ++++++++ .../TagList/config/service_manager.php | 17 ++++++ .../TagList/src/ConfigProvider.php | 15 ++++++ .../Handler/Query/ReadList/ReadListQuery.php | 30 +++++++++++ .../Query/ReadList/ReadListQueryBuilder.php | 20 +++++++ .../Query/ReadList/ReadListQueryHandler.php | 33 ++++++++++++ .../Query/ReadList/ReadListQueryResult.php | 20 +++++++ 17 files changed, 343 insertions(+), 12 deletions(-) create mode 100644 src/ApiDomain/External/TagList/config/routes.php create mode 100644 src/ApiDomain/External/TagList/config/service_manager.php create mode 100644 src/ApiDomain/External/TagList/src/ConfigProvider.php create mode 100644 src/ApiDomain/External/TagList/src/Handler/ReadListHandler.php create mode 100644 src/ApiDomain/External/TagList/src/ResponseFormatter/ReadListResponseFormatter.php create mode 100644 src/HandlingDomain/TagList/config/service_manager.php create mode 100644 src/HandlingDomain/TagList/src/ConfigProvider.php create mode 100644 src/HandlingDomain/TagList/src/Handler/Query/ReadList/ReadListQuery.php create mode 100644 src/HandlingDomain/TagList/src/Handler/Query/ReadList/ReadListQueryBuilder.php create mode 100644 src/HandlingDomain/TagList/src/Handler/Query/ReadList/ReadListQueryHandler.php create mode 100644 src/HandlingDomain/TagList/src/Handler/Query/ReadList/ReadListQueryResult.php diff --git a/bin/createApi.php b/bin/createApi.php index d39e6c4..fb6c4ad 100644 --- a/bin/createApi.php +++ b/bin/createApi.php @@ -103,7 +103,7 @@ declare(strict_types=1); use {$apiHandlerUsingNamespace}; use {$apiResponseFormatterUsingNamespace}; -use Reinfi\DependencyInjection\Factory\AutoWiringFactory; +use Reinfi\\DependencyInjection\\Factory\\AutoWiringFactory; return [ 'factories' => [ @@ -122,7 +122,7 @@ return [ declare(strict_types=1); -namespace MyTube\API\{$apiType}\{$apiNamespace}; +namespace MyTube\\API\\{$apiType}\\{$apiNamespace}; class ConfigProvider { @@ -146,8 +146,8 @@ declare(strict_types=1); use {$cqrsHandlerUsingNamespace}; use {$cqrsBuilderUsingNamespace}; -use Reinfi\DependencyInjection\Factory\AutoWiringFactory; -use Reinfi\DependencyInjection\Factory\InjectionFactory; +use Reinfi\\DependencyInjection\\Factory\\AutoWiringFactory; +use Reinfi\\DependencyInjection\\Factory\\InjectionFactory; return [ 'factories' => [ @@ -165,7 +165,7 @@ return [ declare(strict_types=1); -namespace MyTube\Handling\{$originalCqrsNamespace}; +namespace MyTube\\Handling\\{$originalCqrsNamespace}; class ConfigProvider { @@ -191,10 +191,11 @@ namespace {$apiHandlerNamespace}; use {$cqrsHandlerUsingNamespace}; use {$cqrsBuilderUsingNamespace}; use {$apiResponseFormatterUsingNamespace}; +use {$projectNamespace}\\Infrastructure\\Request\\Middleware\\AnalyzeBodyMiddleware; +use {$projectNamespace}\\Infrastructure\\Response\\SuccessResponse; use Psr\\Http\\Message\\ResponseInterface; use Psr\\Http\\Message\\ServerRequestInterface; use Psr\\Http\\Server\\RequestHandlerInterface; -use {$projectNamespace}\\Infrastructure\\Response\\SuccessResponse; class {$apiHandlerName} implements RequestHandlerInterface { @@ -207,10 +208,7 @@ class {$apiHandlerName} implements RequestHandlerInterface public function handle(ServerRequestInterface \$request): ResponseInterface { - \$data = json_decode( - \$request->getBody()->getContents(), - true - ); + \$data = \$request->getAttribute(AnalyzeBodyMiddleware::JSON_DATA); \${$cqrsVariableName} = \$this->{$cqrsBuilderVariableName}->build( \$data diff --git a/bin/createPipeline.php b/bin/createPipeline.php index 7eb9d32..cd2eed6 100644 --- a/bin/createPipeline.php +++ b/bin/createPipeline.php @@ -16,7 +16,8 @@ $stepNames = array_slice($argv, 3); # Pipeline $pipelineClassName = $pipelineName . 'Pipeline'; $pipelineVariableName = lcfirst($pipelineClassName); -$pipelineFilePath = $projectSourceDirectory . 'HandlingDomain/' . $pipelineNamespace . '/src/Pipeline/' . $pipelineName . '/' . $pipelineClassName . '.php'; +$pipelineDirectoryPath = $projectSourceDirectory . 'HandlingDomain/' . $pipelineNamespace . '/'; +$pipelineFilePath = $pipelineDirectoryPath . 'src/Pipeline/' . $pipelineName . '/' . $pipelineClassName . '.php'; $pipelineFullNamespace = $projectNamespace . '\\Handling\\' . $pipelineNamespace . '\\Pipeline\\' . $pipelineName; $pipelineUsingNamespace = $pipelineFullNamespace . '\\' . $pipelineClassName; @@ -57,8 +58,11 @@ function writeToFile($path, $content) { file_put_contents($path, $content); } +$createNamespaceFiles = !file_exists($pipelineDirectoryPath); + $stepsUsingNamespaces = []; $stepsDeclarations = []; +$stepsAutoWirings = []; $stepsReferences = []; foreach ($steps as $step) { @@ -69,6 +73,7 @@ foreach ($steps as $step) { $stepsUsingNamespaces[] = $stepUsingNamespace . ';'; $stepsDeclarations[] = 'private readonly ' . $stepClassName . ' $' . $stepVariableName . ','; + $stepsAutoWirings[] = $stepClassName . '::class => AutoWiringFactory::class,'; $stepsReferences[] = '$this->' . $stepVariableName . ','; $stepFileContent = " [ + /// Pipeline + // {$pipelineName} + {$pipelineName}::class => AutoWiringFactory::class, + {$stepsAutoWirings} + ], +]; +"; + $serviceManagerFilePath = $createNamespaceFiles . 'config/service_manager.php'; + writeToFile($serviceManagerFilePath, $serviceManagerFileContent); + + $configProviderFileContent = " require __DIR__ . './../config/service_manager.php', + ]; + } +} +"; + $configProviderFilePath = $createNamespaceFiles . 'src/ConfigProvider.php'; + writeToFile($configProviderFilePath, $configProviderFileContent); + +} diff --git a/composer.development.json b/composer.development.json index 22376ae..8799c42 100644 --- a/composer.development.json +++ b/composer.development.json @@ -28,6 +28,7 @@ "MyTube\\API\\External\\Authentication\\": "src\/ApiDomain\/External\/Authentication\/src", "MyTube\\API\\External\\Health\\": "src\/ApiDomain\/External\/Health\/src", "MyTube\\API\\External\\Tag\\": "src\/ApiDomain\/External\/Tag\/src", + "MyTube\\API\\External\\TagList\\": "src\/ApiDomain\/External\/TagList\/src", "MyTube\\API\\External\\User\\": "src\/ApiDomain\/External\/User\/src", "MyTube\\API\\External\\Video\\": "src\/ApiDomain\/External\/Video\/src", "MyTube\\API\\External\\VideoList\\": "src\/ApiDomain\/External\/VideoList\/src", @@ -36,6 +37,7 @@ "MyTube\\Handling\\Registration\\": "src\/HandlingDomain\/Registration\/src", "MyTube\\Handling\\Role\\": "src\/HandlingDomain\/Role\/src", "MyTube\\Handling\\Tag\\": "src\/HandlingDomain\/Tag\/src", + "MyTube\\Handling\\TagList\\": "src\/HandlingDomain\/TagList\/src", "MyTube\\Handling\\User\\": "src\/HandlingDomain\/User\/src", "MyTube\\Handling\\UserSession\\": "src\/HandlingDomain\/UserSession\/src", "MyTube\\Handling\\Video\\": "src\/HandlingDomain\/Video\/src", @@ -98,6 +100,7 @@ "MyTube\\API\\External\\Authentication\\": "src\/ApiDomain\/External\/Authentication\/src", "MyTube\\API\\External\\Health\\": "src\/ApiDomain\/External\/Health\/src", "MyTube\\API\\External\\Tag\\": "src\/ApiDomain\/External\/Tag\/src", + "MyTube\\API\\External\\TagList\\": "src\/ApiDomain\/External\/TagList\/src", "MyTube\\API\\External\\User\\": "src\/ApiDomain\/External\/User\/src", "MyTube\\API\\External\\Video\\": "src\/ApiDomain\/External\/Video\/src", "MyTube\\API\\External\\VideoList\\": "src\/ApiDomain\/External\/VideoList\/src", @@ -106,6 +109,7 @@ "MyTube\\Handling\\Registration\\": "src\/HandlingDomain\/Registration\/src", "MyTube\\Handling\\Role\\": "src\/HandlingDomain\/Role\/src", "MyTube\\Handling\\Tag\\": "src\/HandlingDomain\/Tag\/src", + "MyTube\\Handling\\TagList\\": "src\/HandlingDomain\/TagList\/src", "MyTube\\Handling\\User\\": "src\/HandlingDomain\/User\/src", "MyTube\\Handling\\UserSession\\": "src\/HandlingDomain\/UserSession\/src", "MyTube\\Handling\\Video\\": "src\/HandlingDomain\/Video\/src", diff --git a/config/config.php b/config/config.php index 80c5026..b8264f6 100644 --- a/config/config.php +++ b/config/config.php @@ -60,6 +60,7 @@ $aggregator = new ConfigAggregator([ \MyTube\Handling\Video\ConfigProvider::class, \MyTube\Handling\VideoList\ConfigProvider::class, \MyTube\Handling\Tag\ConfigProvider::class, + \MyTube\Handling\TagList\ConfigProvider::class, // API /// Command @@ -72,6 +73,7 @@ $aggregator = new ConfigAggregator([ \MyTube\API\External\Video\ConfigProvider::class, \MyTube\API\External\VideoList\ConfigProvider::class, \MyTube\API\External\Tag\ConfigProvider::class, + \MyTube\API\External\TagList\ConfigProvider::class, /// Internal diff --git a/src/ApiDomain/External/TagList/config/routes.php b/src/ApiDomain/External/TagList/config/routes.php new file mode 100644 index 0000000..a095ae2 --- /dev/null +++ b/src/ApiDomain/External/TagList/config/routes.php @@ -0,0 +1,16 @@ + 'tag-list.read-list', + 'path' => '/api/tag-list/read-list[/]', + 'allowed_methods' => ['POST'], + 'middleware' => [ + ReadListHandler::class, + ], + ], +]; diff --git a/src/ApiDomain/External/TagList/config/service_manager.php b/src/ApiDomain/External/TagList/config/service_manager.php new file mode 100644 index 0000000..2919fb9 --- /dev/null +++ b/src/ApiDomain/External/TagList/config/service_manager.php @@ -0,0 +1,17 @@ + [ + // Handler + ReadListHandler::class => AutoWiringFactory::class, + + // Response Formatter + ReadListResponseFormatter::class => AutoWiringFactory::class, + ], +]; diff --git a/src/ApiDomain/External/TagList/src/ConfigProvider.php b/src/ApiDomain/External/TagList/src/ConfigProvider.php new file mode 100644 index 0000000..4e42d1d --- /dev/null +++ b/src/ApiDomain/External/TagList/src/ConfigProvider.php @@ -0,0 +1,16 @@ + require __DIR__ . './../config/service_manager.php', + 'routes' => require __DIR__ . '/./../config/routes.php', + ]; + } +} diff --git a/src/ApiDomain/External/TagList/src/Handler/ReadListHandler.php b/src/ApiDomain/External/TagList/src/Handler/ReadListHandler.php new file mode 100644 index 0000000..030e155 --- /dev/null +++ b/src/ApiDomain/External/TagList/src/Handler/ReadListHandler.php @@ -0,0 +1,38 @@ +getAttribute(AnalyzeBodyMiddleware::JSON_DATA); + + $readListQuery = $this->readListQueryBuilder->build( + $data['query'] ?? null, + $data['page'], + $data['perPage'], + ); + $result = $this->readListQueryHandler->execute($readListQuery); + + return new SuccessResponse($this->responseFormatter->format($result)); + } +} diff --git a/src/ApiDomain/External/TagList/src/ResponseFormatter/ReadListResponseFormatter.php b/src/ApiDomain/External/TagList/src/ResponseFormatter/ReadListResponseFormatter.php new file mode 100644 index 0000000..55c3a5c --- /dev/null +++ b/src/ApiDomain/External/TagList/src/ResponseFormatter/ReadListResponseFormatter.php @@ -0,0 +1,32 @@ +getPaginator(); + $tags = $paginator->getIterator(); + + $items = []; + + /** @var Tag $tag */ + foreach ($tags as $tag) { + $items[] = [ + 'id' => $tag->getId()->toString(), + 'description' => $tag->getDescription(), + ]; + } + + return [ + 'total' => $paginator->count(), + 'items' => $items, + ]; + } +} diff --git a/src/ApiDomain/External/VideoList/src/Handler/ReadListHandler.php b/src/ApiDomain/External/VideoList/src/Handler/ReadListHandler.php index 278a1cc..e061542 100644 --- a/src/ApiDomain/External/VideoList/src/Handler/ReadListHandler.php +++ b/src/ApiDomain/External/VideoList/src/Handler/ReadListHandler.php @@ -5,8 +5,8 @@ declare(strict_types=1); namespace MyTube\API\External\VideoList\Handler; use MyTube\API\External\VideoList\ResponseFormatter\ReadListResponseFormatter; -use MyTube\Handling\VideoList\Handler\Query\ReadList\ReadListQueryHandler; use MyTube\Handling\VideoList\Handler\Query\ReadList\ReadListQueryBuilder; +use MyTube\Handling\VideoList\Handler\Query\ReadList\ReadListQueryHandler; use MyTube\Infrastructure\Request\Middleware\AnalyzeBodyMiddleware; use MyTube\Infrastructure\Response\SuccessResponse; use Psr\Http\Message\ResponseInterface; diff --git a/src/DataDomain/Business/src/Repository/TagRepository.php b/src/DataDomain/Business/src/Repository/TagRepository.php index 69adcde..d3212aa 100644 --- a/src/DataDomain/Business/src/Repository/TagRepository.php +++ b/src/DataDomain/Business/src/Repository/TagRepository.php @@ -3,6 +3,28 @@ namespace MyTube\Data\Business\Repository; use Doctrine\ORM\EntityRepository; +use Doctrine\ORM\Tools\Pagination\Paginator; class TagRepository extends EntityRepository { + public function findByFilter( + ?string $query, + int $page, + int $perPage, + ): Paginator + { + $queryBuilder = $this->createQueryBuilder('t'); + + if ($query !== null) { + $query = '%'.$query.'%'; + + $queryBuilder + ->where('t.description like :query') + ->setParameter('query', $query); + } + + $queryBuilder->setFirstResult($perPage * ($page - 1)); + $queryBuilder->setMaxResults($perPage); + + return new Paginator($queryBuilder->getQuery()); + } } \ No newline at end of file diff --git a/src/HandlingDomain/TagList/config/service_manager.php b/src/HandlingDomain/TagList/config/service_manager.php new file mode 100644 index 0000000..c1f936e --- /dev/null +++ b/src/HandlingDomain/TagList/config/service_manager.php @@ -0,0 +1,17 @@ + [ + /// CQRS + // ReadList + ReadListQueryBuilder::class => AutoWiringFactory::class, + ReadListQueryHandler::class => AutoWiringFactory::class, + ], +]; diff --git a/src/HandlingDomain/TagList/src/ConfigProvider.php b/src/HandlingDomain/TagList/src/ConfigProvider.php new file mode 100644 index 0000000..1c922eb --- /dev/null +++ b/src/HandlingDomain/TagList/src/ConfigProvider.php @@ -0,0 +1,15 @@ + require __DIR__ . './../config/service_manager.php', + ]; + } +} diff --git a/src/HandlingDomain/TagList/src/Handler/Query/ReadList/ReadListQuery.php b/src/HandlingDomain/TagList/src/Handler/Query/ReadList/ReadListQuery.php new file mode 100644 index 0000000..10c97de --- /dev/null +++ b/src/HandlingDomain/TagList/src/Handler/Query/ReadList/ReadListQuery.php @@ -0,0 +1,30 @@ +query; + } + + public function getPage(): int + { + return $this->page; + } + + public function getPerPage(): int + { + return $this->perPage; + } +} diff --git a/src/HandlingDomain/TagList/src/Handler/Query/ReadList/ReadListQueryBuilder.php b/src/HandlingDomain/TagList/src/Handler/Query/ReadList/ReadListQueryBuilder.php new file mode 100644 index 0000000..fe7e4a9 --- /dev/null +++ b/src/HandlingDomain/TagList/src/Handler/Query/ReadList/ReadListQueryBuilder.php @@ -0,0 +1,20 @@ +tagRepository->findByFilter( + $readListQuery->getQuery(), + $readListQuery->getPage(), + $readListQuery->getPerPage() + ) + ); + } +} diff --git a/src/HandlingDomain/TagList/src/Handler/Query/ReadList/ReadListQueryResult.php b/src/HandlingDomain/TagList/src/Handler/Query/ReadList/ReadListQueryResult.php new file mode 100644 index 0000000..446e9d6 --- /dev/null +++ b/src/HandlingDomain/TagList/src/Handler/Query/ReadList/ReadListQueryResult.php @@ -0,0 +1,20 @@ +paginator; + } +}