diff --git a/config/autoload/authorization.global.php b/config/autoload/authorization.global.php index 5190d65..71bdf22 100644 --- a/config/autoload/authorization.global.php +++ b/config/autoload/authorization.global.php @@ -4,12 +4,25 @@ return [ 'bee-rbac' => [ 'roles' => [ 'admin', + 'beekeeper', 'user', ], 'permissions' => [ 'user' => [ ], + 'beekeeper' => [ + 'colony.create', + 'colony.delete', + 'colony.update', + 'colony.read-details', + 'colony.read-list', + ], 'admin' => [ + 'colony.create', + 'colony.delete', + 'colony.update', + 'colony.read-details', + 'colony.read-list', 'user.create-user', ], ] diff --git a/config/config.php b/config/config.php index 1031b20..d460cf9 100644 --- a/config/config.php +++ b/config/config.php @@ -58,6 +58,7 @@ $aggregator = new ConfigAggregator([ \Bee\Handling\User\ConfigProvider::class, \Bee\Handling\UserSession\ConfigProvider::class, \Bee\Handling\Registration\ConfigProvider::class, + \Bee\Handling\Colony\ConfigProvider::class, // API /// Command @@ -67,6 +68,7 @@ $aggregator = new ConfigAggregator([ \Bee\API\External\Health\ConfigProvider::class, \Bee\API\External\User\ConfigProvider::class, \Bee\API\External\Authentication\ConfigProvider::class, + \Bee\API\External\Colony\ConfigProvider::class, /// Internal diff --git a/data/migrations/business/Version20240915085327.php b/data/migrations/business/Version20240915085327.php new file mode 100644 index 0000000..804f56f --- /dev/null +++ b/data/migrations/business/Version20240915085327.php @@ -0,0 +1,38 @@ +addSql($sql); + } + + public function down(Schema $schema): void + { + $this->addSql("DROP TABLE colony;"); + } +} diff --git a/src/ApiDomain/Console/src/Command/InitializeDataCommand.php b/src/ApiDomain/Console/src/Command/InitializeDataCommand.php index a724a42..3fe5454 100644 --- a/src/ApiDomain/Console/src/Command/InitializeDataCommand.php +++ b/src/ApiDomain/Console/src/Command/InitializeDataCommand.php @@ -66,8 +66,6 @@ class InitializeDataCommand extends Command $this->entityManager->persist($adminUser); $this->entityManager->flush(); } - - $io->success('OK!'); } catch (\Throwable $e) { $io->error($e->getMessage()); $io->error($e->getTraceAsString()); diff --git a/src/ApiDomain/Console/src/Command/RbacUpdateCommand.php b/src/ApiDomain/Console/src/Command/RbacUpdateCommand.php index c5089df..536d572 100644 --- a/src/ApiDomain/Console/src/Command/RbacUpdateCommand.php +++ b/src/ApiDomain/Console/src/Command/RbacUpdateCommand.php @@ -84,8 +84,6 @@ class RbacUpdateCommand extends Command $this->entityManager->flush(); } } - - $io->success('OK!'); } catch (\Throwable $e) { $io->error($e->getMessage()); $io->error($e->getTraceAsString()); diff --git a/src/ApiDomain/External/Colony/config/routes.php b/src/ApiDomain/External/Colony/config/routes.php new file mode 100644 index 0000000..b944514 --- /dev/null +++ b/src/ApiDomain/External/Colony/config/routes.php @@ -0,0 +1,62 @@ + 'colony.create', + 'path' => '/api/colony/create', + 'allowed_methods' => ['POST'], + 'middleware' => [ + LoggedInUserMiddleware::class, + EnsureAuthorizationMiddleware::class, + CreateHandler::class, + ], + ], + [ + 'name' => 'colony.delete', + 'path' => '/api/colony/delete', + 'allowed_methods' => ['POST'], + 'middleware' => [ + LoggedInUserMiddleware::class, + EnsureAuthorizationMiddleware::class, + DeleteHandler::class, + ], + ], + [ + 'name' => 'colony.update', + 'path' => '/api/colony/update', + 'allowed_methods' => ['POST'], + 'middleware' => [ + LoggedInUserMiddleware::class, + EnsureAuthorizationMiddleware::class, + UpdateHandler::class, + ], + ], + [ + 'name' => 'colony.read-list', + 'path' => '/api/colony/read-list', + 'allowed_methods' => ['POST'], + 'middleware' => [ + LoggedInUserMiddleware::class, + EnsureAuthorizationMiddleware::class, + ReadListHandler::class, + ], + ], + [ + 'name' => 'colony.read-details', + 'path' => '/api/colony/read-details', + 'allowed_methods' => ['POST'], + 'middleware' => [ + LoggedInUserMiddleware::class, + EnsureAuthorizationMiddleware::class, + ReadDetailsHandler::class, + ], + ], +]; diff --git a/src/ApiDomain/External/Colony/config/service_manager.php b/src/ApiDomain/External/Colony/config/service_manager.php new file mode 100644 index 0000000..6433376 --- /dev/null +++ b/src/ApiDomain/External/Colony/config/service_manager.php @@ -0,0 +1,31 @@ + [ + // Handler + CreateHandler::class => AutoWiringFactory::class, + DeleteHandler::class => AutoWiringFactory::class, + UpdateHandler::class => AutoWiringFactory::class, + ReadListHandler::class => AutoWiringFactory::class, + ReadDetailsHandler::class => AutoWiringFactory::class, + + // Response Formatter + CreateResponseFormatter::class => AutoWiringFactory::class, + DeleteResponseFormatter::class => AutoWiringFactory::class, + UpdateResponseFormatter::class => AutoWiringFactory::class, + ReadListResponseFormatter::class => AutoWiringFactory::class, + ReadDetailsResponseFormatter::class => AutoWiringFactory::class, + ], +]; diff --git a/src/ApiDomain/External/Colony/src/ConfigProvider.php b/src/ApiDomain/External/Colony/src/ConfigProvider.php new file mode 100644 index 0000000..896d869 --- /dev/null +++ b/src/ApiDomain/External/Colony/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/Colony/src/Handler/CreateHandler.php b/src/ApiDomain/External/Colony/src/Handler/CreateHandler.php new file mode 100644 index 0000000..2f88f4c --- /dev/null +++ b/src/ApiDomain/External/Colony/src/Handler/CreateHandler.php @@ -0,0 +1,39 @@ +getAttribute(AnalyzeBodyMiddleware::JSON_DATA); + $user = $request->getAttribute(LoggedInUserMiddleware::USER_KEY); + + $createCommand = $this->builder->build( + $user, + $data['name'] + ); + $result = $this->handler->execute($createCommand); + + return new SuccessResponse($this->responseFormatter->format($result)); + } +} diff --git a/src/ApiDomain/External/Colony/src/Handler/DeleteHandler.php b/src/ApiDomain/External/Colony/src/Handler/DeleteHandler.php new file mode 100644 index 0000000..d8453e5 --- /dev/null +++ b/src/ApiDomain/External/Colony/src/Handler/DeleteHandler.php @@ -0,0 +1,46 @@ +getAttribute(AnalyzeBodyMiddleware::JSON_DATA); + $user = $request->getAttribute(LoggedInUserMiddleware::USER_KEY); + + $deleteCommand = $this->builder->build( + $user, + Uuid::fromString($data['id']) + ); + $result = $this->handler->execute($deleteCommand); + + return new SuccessResponse($this->responseFormatter->format($result)); + } +} diff --git a/src/ApiDomain/External/Colony/src/Handler/ReadDetailsHandler.php b/src/ApiDomain/External/Colony/src/Handler/ReadDetailsHandler.php new file mode 100644 index 0000000..6471a22 --- /dev/null +++ b/src/ApiDomain/External/Colony/src/Handler/ReadDetailsHandler.php @@ -0,0 +1,40 @@ +getAttribute(AnalyzeBodyMiddleware::JSON_DATA); + $user = $request->getAttribute(LoggedInUserMiddleware::USER_KEY); + + $readDetailsQuery = $this->builder->build( + $user, + Uuid::fromString($data['id']) + ); + $result = $this->handler->execute($readDetailsQuery); + + return new SuccessResponse($this->responseFormatter->format($result)); + } +} diff --git a/src/ApiDomain/External/Colony/src/Handler/ReadListHandler.php b/src/ApiDomain/External/Colony/src/Handler/ReadListHandler.php new file mode 100644 index 0000000..7670d11 --- /dev/null +++ b/src/ApiDomain/External/Colony/src/Handler/ReadListHandler.php @@ -0,0 +1,43 @@ +getAttribute(AnalyzeBodyMiddleware::JSON_DATA); + $user = $request->getAttribute(LoggedInUserMiddleware::USER_KEY); + + $readListQuery = $this->builder->build( + $user, + $data['query'], + $data['page'], + $data['perPage'], + $data['orderBy'], + $data['orderDirection'], + ); + $result = $this->handler->execute($readListQuery); + + return new SuccessResponse($this->responseFormatter->format($result)); + } +} diff --git a/src/ApiDomain/External/Colony/src/Handler/UpdateHandler.php b/src/ApiDomain/External/Colony/src/Handler/UpdateHandler.php new file mode 100644 index 0000000..3e9c1b4 --- /dev/null +++ b/src/ApiDomain/External/Colony/src/Handler/UpdateHandler.php @@ -0,0 +1,41 @@ +getAttribute(AnalyzeBodyMiddleware::JSON_DATA); + $user = $request->getAttribute(LoggedInUserMiddleware::USER_KEY); + + $updateCommand = $this->builder->build( + $user, + Uuid::fromString($data['id']), + $data['name'] ?? null, + ); + $result = $this->handler->execute($updateCommand); + + return new SuccessResponse($this->responseFormatter->format($result)); + } +} diff --git a/src/ApiDomain/External/Colony/src/ResponseFormatter/CreateResponseFormatter.php b/src/ApiDomain/External/Colony/src/ResponseFormatter/CreateResponseFormatter.php new file mode 100644 index 0000000..93e5d8b --- /dev/null +++ b/src/ApiDomain/External/Colony/src/ResponseFormatter/CreateResponseFormatter.php @@ -0,0 +1,19 @@ +getColony(); + return [ + 'id' => $colony->getId(), + 'name' => $colony->getName(), + ]; + } +} diff --git a/src/ApiDomain/External/Colony/src/ResponseFormatter/DeleteResponseFormatter.php b/src/ApiDomain/External/Colony/src/ResponseFormatter/DeleteResponseFormatter.php new file mode 100644 index 0000000..b849cd0 --- /dev/null +++ b/src/ApiDomain/External/Colony/src/ResponseFormatter/DeleteResponseFormatter.php @@ -0,0 +1,17 @@ +getColony(); + + return [ + 'id' => $colony->getId(), + 'name' => $colony->getName(), + ]; + } +} diff --git a/src/ApiDomain/External/Colony/src/ResponseFormatter/ReadListResponseFormatter.php b/src/ApiDomain/External/Colony/src/ResponseFormatter/ReadListResponseFormatter.php new file mode 100644 index 0000000..70eec36 --- /dev/null +++ b/src/ApiDomain/External/Colony/src/ResponseFormatter/ReadListResponseFormatter.php @@ -0,0 +1,29 @@ +getColonies() as $colony) { + $items[] = [ + 'id' => $colony->getId(), + 'name' => $colony->getName() + ]; + } + + return [ + "total" => $readListQueryResult->getTotal(), + "items" => $items + ]; + } +} diff --git a/src/ApiDomain/External/Colony/src/ResponseFormatter/UpdateResponseFormatter.php b/src/ApiDomain/External/Colony/src/ResponseFormatter/UpdateResponseFormatter.php new file mode 100644 index 0000000..0e25510 --- /dev/null +++ b/src/ApiDomain/External/Colony/src/ResponseFormatter/UpdateResponseFormatter.php @@ -0,0 +1,20 @@ +getColony(); + + return [ + 'id' => $colony->getId(), + 'name' => $colony->getName() + ]; + } +} diff --git a/src/DataDomain/Business/src/Entity/Colony.php b/src/DataDomain/Business/src/Entity/Colony.php new file mode 100644 index 0000000..f137ad6 --- /dev/null +++ b/src/DataDomain/Business/src/Entity/Colony.php @@ -0,0 +1,99 @@ +id = UuidGenerator::generate(); + + $now = new DateTime(); + $this->setCreatedAt($now); + $this->setUpdatedAt($now); + } + + + /** + * @ORM\PrePersist + * @ORM\PreUpdate + */ + public function updateTimestamps(): void { + $now = new DateTime(); + $this->setUpdatedAt($now); + } + + + public function getId(): UuidInterface { + return $this->id; + } + + public function getUserId(): UuidInterface { + return $this->userId; + } + public function setUserId(UuidInterface $userId): void { + $this->userId = $userId; + } + + public function getUser(): User { + return $this->user; + } + public function setUser(User $user): void { + $this->user = $user; + } + + public function getName(): string + { + return $this->name; + } + public function setName(string $name): void + { + $this->name = $name; + } + + public function getCreatedAt(): DateTime { + return $this->createdAt; + } + public function setCreatedAt(DateTime $createdAt): void { + $this->createdAt = $createdAt; + } + + public function getUpdatedAt(): DateTime { + return $this->updatedAt; + } + public function setUpdatedAt(DateTime $updatedAt): void { + $this->updatedAt = $updatedAt; + } +} \ No newline at end of file diff --git a/src/DataDomain/Business/src/Repository/ColonyRepository.php b/src/DataDomain/Business/src/Repository/ColonyRepository.php new file mode 100644 index 0000000..fe2d67c --- /dev/null +++ b/src/DataDomain/Business/src/Repository/ColonyRepository.php @@ -0,0 +1,8 @@ + [ + /// Builder + ColonyBuilder::class => AutoWiringFactory::class, + + /// Repository + ColonyRepository::class => AutoWiringFactory::class, + + /// Rule + ColonyIsAssociatedWithUserRule::class => AutoWiringFactory::class, + + /// CQRS + // Create + CreateCommandBuilder::class => AutoWiringFactory::class, + CreateCommandHandler::class => AutoWiringFactory::class, + // Delete + DeleteCommandBuilder::class => AutoWiringFactory::class, + DeleteCommandHandler::class => InjectionFactory::class, + // Update + UpdateCommandBuilder::class => AutoWiringFactory::class, + UpdateCommandHandler::class => InjectionFactory::class, + // Read List + ReadListQueryBuilder::class => AutoWiringFactory::class, + ReadListQueryHandler::class => AutoWiringFactory::class, + // Read Details + ReadDetailsQueryBuilder::class => AutoWiringFactory::class, + ReadDetailsQueryHandler::class => InjectionFactory::class, + ], +]; diff --git a/src/HandlingDomain/Colony/src/Builder/ColonyBuilder.php b/src/HandlingDomain/Colony/src/Builder/ColonyBuilder.php new file mode 100644 index 0000000..8c26ed7 --- /dev/null +++ b/src/HandlingDomain/Colony/src/Builder/ColonyBuilder.php @@ -0,0 +1,20 @@ +setUser($user); + $colony->setName($name); + return $colony; + } +} diff --git a/src/HandlingDomain/Colony/src/ConfigProvider.php b/src/HandlingDomain/Colony/src/ConfigProvider.php new file mode 100644 index 0000000..8698818 --- /dev/null +++ b/src/HandlingDomain/Colony/src/ConfigProvider.php @@ -0,0 +1,15 @@ + require __DIR__ . '/./../config/service_manager.php', + ]; + } +} diff --git a/src/HandlingDomain/Colony/src/Exception/ColonyNotFoundByIdException.php b/src/HandlingDomain/Colony/src/Exception/ColonyNotFoundByIdException.php new file mode 100644 index 0000000..ebd370e --- /dev/null +++ b/src/HandlingDomain/Colony/src/Exception/ColonyNotFoundByIdException.php @@ -0,0 +1,27 @@ +toString() + ), + ErrorDomain::Colony, + ErrorCode::NotFound + ); + } +} \ No newline at end of file diff --git a/src/HandlingDomain/Colony/src/Exception/ColonyUserInvalidAssociationException.php b/src/HandlingDomain/Colony/src/Exception/ColonyUserInvalidAssociationException.php new file mode 100644 index 0000000..bf6c4a2 --- /dev/null +++ b/src/HandlingDomain/Colony/src/Exception/ColonyUserInvalidAssociationException.php @@ -0,0 +1,29 @@ +toString(), + $userId->toString() + ), + ErrorDomain::Colony, + ErrorCode::UserInvalidAssociation + ); + } +} \ No newline at end of file diff --git a/src/HandlingDomain/Colony/src/Handler/Command/Create/CreateCommand.php b/src/HandlingDomain/Colony/src/Handler/Command/Create/CreateCommand.php new file mode 100644 index 0000000..4d6e43a --- /dev/null +++ b/src/HandlingDomain/Colony/src/Handler/Command/Create/CreateCommand.php @@ -0,0 +1,26 @@ +user; + } + + public function getName(): string + { + return $this->name; + } +} diff --git a/src/HandlingDomain/Colony/src/Handler/Command/Create/CreateCommandBuilder.php b/src/HandlingDomain/Colony/src/Handler/Command/Create/CreateCommandBuilder.php new file mode 100644 index 0000000..ed77fad --- /dev/null +++ b/src/HandlingDomain/Colony/src/Handler/Command/Create/CreateCommandBuilder.php @@ -0,0 +1,20 @@ +builder->build( + $createCommand->getUser(), + $createCommand->getName(), + ); + + $this->entityManager->persist($colony); + $this->entityManager->flush(); + + return new CreateCommandResult($colony); + } +} diff --git a/src/HandlingDomain/Colony/src/Handler/Command/Create/CreateCommandResult.php b/src/HandlingDomain/Colony/src/Handler/Command/Create/CreateCommandResult.php new file mode 100644 index 0000000..f278acc --- /dev/null +++ b/src/HandlingDomain/Colony/src/Handler/Command/Create/CreateCommandResult.php @@ -0,0 +1,20 @@ +colony; + } +} diff --git a/src/HandlingDomain/Colony/src/Handler/Command/Delete/DeleteCommand.php b/src/HandlingDomain/Colony/src/Handler/Command/Delete/DeleteCommand.php new file mode 100644 index 0000000..03af8bb --- /dev/null +++ b/src/HandlingDomain/Colony/src/Handler/Command/Delete/DeleteCommand.php @@ -0,0 +1,27 @@ +user; + } + + public function getColonyId(): UuidInterface + { + return $this->colonyId; + } +} diff --git a/src/HandlingDomain/Colony/src/Handler/Command/Delete/DeleteCommandBuilder.php b/src/HandlingDomain/Colony/src/Handler/Command/Delete/DeleteCommandBuilder.php new file mode 100644 index 0000000..ee139f1 --- /dev/null +++ b/src/HandlingDomain/Colony/src/Handler/Command/Delete/DeleteCommandBuilder.php @@ -0,0 +1,21 @@ +colonyRepository->findOneBy(['id' => $deleteCommand->getColonyId()]); + + if ($colony === null) { + throw new ColonyNotFoundByIdException($deleteCommand->getColonyId()); + } + + if (!$this->colonyIsAssociatedWithUserRule->appliesTo($colony, $deleteCommand->getUser())) { + throw new ColonyUserInvalidAssociationException($colony->getId(), $deleteCommand->getUser()->getId()); + } + + $this->entityManager->remove($colony); + $this->entityManager->flush(); + + return new DeleteCommandResult(); + } +} diff --git a/src/HandlingDomain/Colony/src/Handler/Command/Delete/DeleteCommandResult.php b/src/HandlingDomain/Colony/src/Handler/Command/Delete/DeleteCommandResult.php new file mode 100644 index 0000000..a16680b --- /dev/null +++ b/src/HandlingDomain/Colony/src/Handler/Command/Delete/DeleteCommandResult.php @@ -0,0 +1,13 @@ +user; + } + + public function getColonyId(): UuidInterface + { + return $this->colonyId; + } + + public function getName(): ?string + { + return $this->name; + } +} diff --git a/src/HandlingDomain/Colony/src/Handler/Command/Update/UpdateCommandBuilder.php b/src/HandlingDomain/Colony/src/Handler/Command/Update/UpdateCommandBuilder.php new file mode 100644 index 0000000..663045f --- /dev/null +++ b/src/HandlingDomain/Colony/src/Handler/Command/Update/UpdateCommandBuilder.php @@ -0,0 +1,23 @@ +colonyRepository->findOneBy(['id' => $updateCommand->getColonyId()]); + + if ($colony === null) { + throw new ColonyNotFoundByIdException($updateCommand->getColonyId()); + } + + if (!$this->colonyIsAssociatedWithUserRule->appliesTo($colony, $updateCommand->getUser())) { + throw new ColonyUserInvalidAssociationException($colony->getId(), $updateCommand->getUser()->getId()); + } + + if ($updateCommand->getName() !== null) { + $colony->setName($updateCommand->getName()); + } + + $this->entityManager->persist($colony); + $this->entityManager->flush(); + + return new UpdateCommandResult($colony); + } +} diff --git a/src/HandlingDomain/Colony/src/Handler/Command/Update/UpdateCommandResult.php b/src/HandlingDomain/Colony/src/Handler/Command/Update/UpdateCommandResult.php new file mode 100644 index 0000000..75cc4e1 --- /dev/null +++ b/src/HandlingDomain/Colony/src/Handler/Command/Update/UpdateCommandResult.php @@ -0,0 +1,20 @@ +colony; + } +} diff --git a/src/HandlingDomain/Colony/src/Handler/Query/ReadDetails/ReadDetailsQuery.php b/src/HandlingDomain/Colony/src/Handler/Query/ReadDetails/ReadDetailsQuery.php new file mode 100644 index 0000000..6dfa297 --- /dev/null +++ b/src/HandlingDomain/Colony/src/Handler/Query/ReadDetails/ReadDetailsQuery.php @@ -0,0 +1,27 @@ +user; + } + + public function getColonyId(): UuidInterface + { + return $this->colonyId; + } +} diff --git a/src/HandlingDomain/Colony/src/Handler/Query/ReadDetails/ReadDetailsQueryBuilder.php b/src/HandlingDomain/Colony/src/Handler/Query/ReadDetails/ReadDetailsQueryBuilder.php new file mode 100644 index 0000000..469f147 --- /dev/null +++ b/src/HandlingDomain/Colony/src/Handler/Query/ReadDetails/ReadDetailsQueryBuilder.php @@ -0,0 +1,21 @@ +colonyRepository->findOneBy(['id' => $readDetailsQuery->getColonyId()]); + + if ($colony === null) { + throw new ColonyNotFoundByIdException($readDetailsQuery->getColonyId()); + } + + if (!$this->colonyIsAssociatedWithUserRule->appliesTo($colony, $readDetailsQuery->getUser())) { + throw new ColonyUserInvalidAssociationException($colony->getId(), $readDetailsQuery->getUser()->getId()); + } + + return new ReadDetailsQueryResult($colony); + } +} diff --git a/src/HandlingDomain/Colony/src/Handler/Query/ReadDetails/ReadDetailsQueryResult.php b/src/HandlingDomain/Colony/src/Handler/Query/ReadDetails/ReadDetailsQueryResult.php new file mode 100644 index 0000000..5f0c677 --- /dev/null +++ b/src/HandlingDomain/Colony/src/Handler/Query/ReadDetails/ReadDetailsQueryResult.php @@ -0,0 +1,20 @@ +colony; + } +} diff --git a/src/HandlingDomain/Colony/src/Handler/Query/ReadList/ReadListQuery.php b/src/HandlingDomain/Colony/src/Handler/Query/ReadList/ReadListQuery.php new file mode 100644 index 0000000..bf5105d --- /dev/null +++ b/src/HandlingDomain/Colony/src/Handler/Query/ReadList/ReadListQuery.php @@ -0,0 +1,50 @@ +user; + } + + public function getQuery(): ?string + { + return $this->query; + } + + public function getPage(): int + { + return $this->page; + } + + public function getPerPage(): int + { + return $this->perPage; + } + + public function getOrderBy(): ?string + { + return $this->orderBy; + } + + public function getOrderDirection(): ?string + { + return $this->orderDirection; + } +} diff --git a/src/HandlingDomain/Colony/src/Handler/Query/ReadList/ReadListQueryBuilder.php b/src/HandlingDomain/Colony/src/Handler/Query/ReadList/ReadListQueryBuilder.php new file mode 100644 index 0000000..d64d40a --- /dev/null +++ b/src/HandlingDomain/Colony/src/Handler/Query/ReadList/ReadListQueryBuilder.php @@ -0,0 +1,28 @@ +repository->readColonyList( + $readListQuery->getUser(), + $readListQuery->getQuery(), + $readListQuery->getPage(), + $readListQuery->getPerPage(), + $readListQuery->getOrderBy(), + $readListQuery->getOrderDirection() + ); + + return new ReadListQueryResult( + $coloniesPaginator->count(), + iterator_to_array($coloniesPaginator->getIterator()) + ); + } +} diff --git a/src/HandlingDomain/Colony/src/Handler/Query/ReadList/ReadListQueryResult.php b/src/HandlingDomain/Colony/src/Handler/Query/ReadList/ReadListQueryResult.php new file mode 100644 index 0000000..0f4d922 --- /dev/null +++ b/src/HandlingDomain/Colony/src/Handler/Query/ReadList/ReadListQueryResult.php @@ -0,0 +1,24 @@ +total; + } + + public function getColonies(): array + { + return $this->colonies; + } +} diff --git a/src/HandlingDomain/Colony/src/Repository/ColonyRepository.php b/src/HandlingDomain/Colony/src/Repository/ColonyRepository.php new file mode 100644 index 0000000..5f35be6 --- /dev/null +++ b/src/HandlingDomain/Colony/src/Repository/ColonyRepository.php @@ -0,0 +1,54 @@ + "c.name" + ]; + + public function __construct( + private readonly EntityManager $entityManager + ) { + } + + public function readColonyList( + User $user, + ?string $query, + int $page, + int $perPage, + ?string $orderBy, + ?string $orderDirection, + ): Paginator + { + $orderBy = self::FIELD_MAPPING[$orderBy] ?? self::FIELD_MAPPING['name']; + $orderDirection = $orderDirection ?? 'asc'; + + $qb = $this->entityManager->createQueryBuilder(); + $qb->select('c') + ->from(Colony::class, 'c') + ->where('c.userId = :userId') + ->setParameter('userId', $user->getId(), UuidBinaryOrderedTimeType::NAME); + + if ($query !== null) { + $query = "%" . $query . "%"; + $qb->andWhere('c.name like :query') + ->setParameter('query', $query); + } + + $qb->orderBy($orderBy, $orderDirection); + $qb->setMaxResults($perPage); + $qb->setFirstResult($perPage * ($page-1)); + + return new Paginator($qb->getQuery()); + } +} \ No newline at end of file diff --git a/src/HandlingDomain/Colony/src/Rule/ColonyIsAssociatedWithUserRule.php b/src/HandlingDomain/Colony/src/Rule/ColonyIsAssociatedWithUserRule.php new file mode 100644 index 0000000..544b887 --- /dev/null +++ b/src/HandlingDomain/Colony/src/Rule/ColonyIsAssociatedWithUserRule.php @@ -0,0 +1,15 @@ +getUser() === $user; + } +} \ No newline at end of file diff --git a/src/Infrastructure/Exception/src/ErrorCode.php b/src/Infrastructure/Exception/src/ErrorCode.php index 456cc08..741bb44 100644 --- a/src/Infrastructure/Exception/src/ErrorCode.php +++ b/src/Infrastructure/Exception/src/ErrorCode.php @@ -6,6 +6,7 @@ enum ErrorCode : string { case SomethingWentWrong = 'SomethingWentWrong'; case NotFound = 'NotFound'; + case UserInvalidAssociation = 'UserInvalidAssociation'; case AlreadyExists = 'AlreadyExists'; case WrongCredentials = 'WrongCredentials'; case Mismatch = 'Mismatch'; diff --git a/src/Infrastructure/Exception/src/ErrorDomain.php b/src/Infrastructure/Exception/src/ErrorDomain.php index 574d2ea..7e6a67b 100644 --- a/src/Infrastructure/Exception/src/ErrorDomain.php +++ b/src/Infrastructure/Exception/src/ErrorDomain.php @@ -4,6 +4,7 @@ namespace Bee\Infrastructure\Exception; enum ErrorDomain : string { case Generic = 'Generic'; + case Colony = 'Colony'; case Role = 'Role'; case User = 'User';