From 2f5fba6e2e3fd693ebec456bbe363e6739da5961 Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Mon, 10 Jun 2024 15:53:03 +0200 Subject: [PATCH] refs #427. Added Hardware profile MVP --- config/api_platform/Client.yaml | 8 ++ config/api_platform/Hardware.yaml | 2 +- config/api_platform/HardwareProfile.yaml | 30 ++++++++ config/services.yaml | 5 ++ config/services/api_platform.yaml | 16 +++- migrations/.gitignore | 0 src/Controller/.gitignore | 0 .../ChangeOrganizationalUnitAction.php | 42 +++++++++++ .../Input/ChangeOrganizationalUnitInput.php | 22 ++++++ src/Dto/Input/HardwareProfileInput.php | 51 +++++++++++++ src/Dto/Output/HardwareOutput.php | 4 +- src/Dto/Output/HardwareProfileOutput.php | 39 ++++++++++ src/Repository/HardwareProfileRepository.php | 27 +------ src/State/Processor/HardwareProcessor.php | 3 +- .../Processor/HardwareProfileProcessor.php | 69 +++++++++++++++++ .../Provider/HardwareProfileProvider.php | 75 +++++++++++++++++++ src/State/Provider/HardwareProvider.php | 2 +- 17 files changed, 362 insertions(+), 33 deletions(-) delete mode 100644 migrations/.gitignore delete mode 100644 src/Controller/.gitignore create mode 100644 src/Controller/ChangeOrganizationalUnitAction.php create mode 100644 src/Dto/Input/ChangeOrganizationalUnitInput.php create mode 100644 src/Dto/Input/HardwareProfileInput.php create mode 100644 src/Dto/Output/HardwareProfileOutput.php create mode 100644 src/State/Processor/HardwareProfileProcessor.php create mode 100644 src/State/Provider/HardwareProfileProvider.php diff --git a/config/api_platform/Client.yaml b/config/api_platform/Client.yaml index 196c658..3dc25d5 100644 --- a/config/api_platform/Client.yaml +++ b/config/api_platform/Client.yaml @@ -25,6 +25,14 @@ resources: ApiPlatform\Metadata\Post: ~ ApiPlatform\Metadata\Delete: ~ + change_organizational_units: + provider: App\State\Provider\ClientProvider + class: ApiPlatform\Metadata\Post + method: POST + input: App\Dto\Input\ChangeOrganizationalUnitInput + uriTemplate: /clients/change-organizational-units + controller: App\Controller\ChangeOrganizationalUnitAction + properties: App\Entity\Client: id: diff --git a/config/api_platform/Hardware.yaml b/config/api_platform/Hardware.yaml index 3e81cc6..dd3ef97 100644 --- a/config/api_platform/Hardware.yaml +++ b/config/api_platform/Hardware.yaml @@ -13,7 +13,7 @@ resources: filters: - 'api_platform.filter.hardware.order' - 'api_platform.filter.hardware.search' - - 'api_platform.filter.hardware.boolean' + ApiPlatform\Metadata\Get: provider: App\State\Provider\HardwareProvider ApiPlatform\Metadata\Put: diff --git a/config/api_platform/HardwareProfile.yaml b/config/api_platform/HardwareProfile.yaml index e69de29..5dd6c67 100644 --- a/config/api_platform/HardwareProfile.yaml +++ b/config/api_platform/HardwareProfile.yaml @@ -0,0 +1,30 @@ +resources: + App\Entity\HardwareProfile: + processor: App\State\Processor\HardwareProfileProcessor + input: App\Dto\Input\HardwareProfileInput + output: App\Dto\Output\HardwareProfileOutput + normalization_context: + groups: ['default', 'hardware-profile:read'] + denormalization_context: + groups: ['hardware-profile:write'] + operations: + ApiPlatform\Metadata\GetCollection: + provider: App\State\Provider\HardwareProfileProvider + filters: + - 'api_platform.filter.hardware.order' + - 'api_platform.filter.hardware.search' + ApiPlatform\Metadata\Get: + provider: App\State\Provider\HardwareProfileProvider + ApiPlatform\Metadata\Put: + provider: App\State\Provider\HardwareProfileProvider + ApiPlatform\Metadata\Patch: + provider: App\State\Provider\HardwareProfileProvider + ApiPlatform\Metadata\Post: ~ + ApiPlatform\Metadata\Delete: ~ + +properties: + App\Entity\HardwareProfile: + id: + identifier: false + uuid: + identifier: true \ No newline at end of file diff --git a/config/services.yaml b/config/services.yaml index d9649c3..124187d 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -46,6 +46,11 @@ services: $itemProvider: '@api_platform.doctrine.orm.state.item_provider' App\State\Provider\HardwareProvider: + bind: + $collectionProvider: '@api_platform.doctrine.orm.state.collection_provider' + $itemProvider: '@api_platform.doctrine.orm.state.item_provider' + + App\State\Provider\HardwareProfileProvider: bind: $collectionProvider: '@api_platform.doctrine.orm.state.collection_provider' $itemProvider: '@api_platform.doctrine.orm.state.item_provider' \ No newline at end of file diff --git a/config/services/api_platform.yaml b/config/services/api_platform.yaml index 8049096..1541e80 100644 --- a/config/services/api_platform.yaml +++ b/config/services/api_platform.yaml @@ -37,4 +37,18 @@ services: parent: 'api_platform.doctrine.orm.boolean_filter' arguments: [ { 'enabled': ~ } ] tags: - - [ 'api_platform.filter' ] \ No newline at end of file + - [ 'api_platform.filter' ] + + api_platform.filter.hardware.order: + parent: 'api_platform.doctrine.orm.order_filter' + arguments: + $properties: { 'id': ~, 'name': ~ } + $orderParameterName: 'order' + tags: + - [ 'api_platform.filter' ] + + api_platform.filter.hardware.search: + parent: 'api_platform.doctrine.orm.search_filter' + arguments: [ { 'id': 'exact', 'name': 'partial' } ] + tags: + - [ 'api_platform.filter' ] diff --git a/migrations/.gitignore b/migrations/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/src/Controller/.gitignore b/src/Controller/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/src/Controller/ChangeOrganizationalUnitAction.php b/src/Controller/ChangeOrganizationalUnitAction.php new file mode 100644 index 0000000..4d0609b --- /dev/null +++ b/src/Controller/ChangeOrganizationalUnitAction.php @@ -0,0 +1,42 @@ +clients as $client) { + /** @var Client $client */ + $clientEntity = $this->clientRepository->find($client->getEntity()->getId()); + if (!$clientEntity) { + throw new NotFoundHttpException('Client not found'); + } + + $organizationalUnit = $input->organizationalUnit->getEntity(); + $clientEntity->setOrganizationalUnit($organizationalUnit); + + $this->entityManager->persist($clientEntity); + } + + $this->entityManager->flush(); + + return new JsonResponse( data: 'Clients updated successfully', status: Response::HTTP_OK); + } +} \ No newline at end of file diff --git a/src/Dto/Input/ChangeOrganizationalUnitInput.php b/src/Dto/Input/ChangeOrganizationalUnitInput.php new file mode 100644 index 0000000..71f8cec --- /dev/null +++ b/src/Dto/Input/ChangeOrganizationalUnitInput.php @@ -0,0 +1,22 @@ +description = $hardwareProfile->getDescription(); + $this->comments = $hardwareProfile->getComments(); + if($hardwareProfile->getOrganizationalUnit()) { + $this->organizationalUnit = new OrganizationalUnitOutput($hardwareProfile->getOrganizationalUnit()); + } + } + + public function createOrUpdateEntity(?HardwareProfile $hardwareProfile = null): HardwareProfile + { + if (!$hardwareProfile) { + $hardwareProfile = new HardwareProfile(); + } + + $hardwareProfile->setDescription($this->description); + $hardwareProfile->setComments($this->comments); + if ($this->organizationalUnit) { + $hardwareProfile->setOrganizationalUnit($this->organizationalUnit->getEntity()); + } + + return $hardwareProfile; + } +} \ No newline at end of file diff --git a/src/Dto/Output/HardwareOutput.php b/src/Dto/Output/HardwareOutput.php index a0ebf9d..262cd6d 100644 --- a/src/Dto/Output/HardwareOutput.php +++ b/src/Dto/Output/HardwareOutput.php @@ -13,10 +13,10 @@ final class HardwareOutput extends AbstractOutput public string $name; #[Groups(['hardware:read'])] - public string $description; + public ?string $description = ''; #[Groups(['hardware:read'])] - public string $type; + public ?string $type = ''; #[Groups(['hardware:read'])] public \DateTime $createAt; diff --git a/src/Dto/Output/HardwareProfileOutput.php b/src/Dto/Output/HardwareProfileOutput.php new file mode 100644 index 0000000..ed2315d --- /dev/null +++ b/src/Dto/Output/HardwareProfileOutput.php @@ -0,0 +1,39 @@ +description = $hardwareProfile->getDescription(); + $this->comments = $hardwareProfile->getComments(); + if($hardwareProfile->getOrganizationalUnit()) { + $this->organizationalUnit = new OrganizationalUnitOutput($hardwareProfile->getOrganizationalUnit()); + } + $this->createAt = $hardwareProfile->getCreatedAt(); + $this->createBy = $hardwareProfile->getCreatedBy(); + } +} \ No newline at end of file diff --git a/src/Repository/HardwareProfileRepository.php b/src/Repository/HardwareProfileRepository.php index 0e4ff1a..256a73e 100644 --- a/src/Repository/HardwareProfileRepository.php +++ b/src/Repository/HardwareProfileRepository.php @@ -9,35 +9,10 @@ use Doctrine\Persistence\ManagerRegistry; /** * @extends ServiceEntityRepository */ -class HardwareProfileRepository extends ServiceEntityRepository +class HardwareProfileRepository extends AbstractRepository { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, HardwareProfile::class); } - - // /** - // * @return HardwareProfile[] Returns an array of HardwareProfile objects - // */ - // public function findByExampleField($value): array - // { - // return $this->createQueryBuilder('h') - // ->andWhere('h.exampleField = :val') - // ->setParameter('val', $value) - // ->orderBy('h.id', 'ASC') - // ->setMaxResults(10) - // ->getQuery() - // ->getResult() - // ; - // } - - // public function findOneBySomeField($value): ?HardwareProfile - // { - // return $this->createQueryBuilder('h') - // ->andWhere('h.exampleField = :val') - // ->setParameter('val', $value) - // ->getQuery() - // ->getOneOrNullResult() - // ; - // } } diff --git a/src/State/Processor/HardwareProcessor.php b/src/State/Processor/HardwareProcessor.php index c396d3e..25781a8 100644 --- a/src/State/Processor/HardwareProcessor.php +++ b/src/State/Processor/HardwareProcessor.php @@ -11,7 +11,6 @@ use ApiPlatform\State\ProcessorInterface; use ApiPlatform\Validator\ValidatorInterface; use App\Dto\Input\ClientInput; use App\Dto\Input\HardwareInput; -use App\Dto\Output\ClientOutput; use App\Dto\Output\HardwareOutput; use App\Repository\HardwareRepository; @@ -44,7 +43,7 @@ class HardwareProcessor implements ProcessorInterface */ private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): HardwareOutput { - if (!($data instanceof ClientInput)) { + if (!($data instanceof HardwareInput)) { throw new \Exception(sprintf('data is not instance of %s', HardwareInput::class)); } diff --git a/src/State/Processor/HardwareProfileProcessor.php b/src/State/Processor/HardwareProfileProcessor.php new file mode 100644 index 0000000..aaf2bdd --- /dev/null +++ b/src/State/Processor/HardwareProfileProcessor.php @@ -0,0 +1,69 @@ +processCreateOrUpdate($data, $operation, $uriVariables, $context); + case $operation instanceof Delete: + return $this->processDelete($data, $operation, $uriVariables, $context); + } + } + + /** + * @throws \Exception + */ + private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): HardwareProfileOutput + { + if (!($data instanceof HardwareProfileInput)) { + throw new \Exception(sprintf('data is not instance of %s', HardwareInput::class)); + } + + $entity = null; + if (isset($uriVariables['uuid'])) { + $entity = $this->hardwareProfileRepository->findOneByUuid($uriVariables['uuid']); + } + + $userGroup = $data->createOrUpdateEntity($entity); + $this->validator->validate($userGroup); + $this->hardwareProfileRepository->save($userGroup); + + return new HardwareProfileOutput($userGroup); + } + + private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null + { + $user = $this->hardwareProfileRepository->findOneByUuid($uriVariables['uuid']); + $this->hardwareProfileRepository->delete($user); + + return null; + } +} diff --git a/src/State/Provider/HardwareProfileProvider.php b/src/State/Provider/HardwareProfileProvider.php new file mode 100644 index 0000000..241fda1 --- /dev/null +++ b/src/State/Provider/HardwareProfileProvider.php @@ -0,0 +1,75 @@ +provideCollection($operation, $uriVariables, $context); + case $operation instanceof Patch: + case $operation instanceof Put: + return $this->provideInput($operation, $uriVariables, $context); + case $operation instanceof Get: + return $this->provideItem($operation, $uriVariables, $context); + } + } + + private function provideCollection(Operation $operation, array $uriVariables = [], array $context = []): object + { + $paginator = $this->collectionProvider->provide($operation, $uriVariables, $context); + + $items = new \ArrayObject(); + foreach ($paginator->getIterator() as $item){ + $items[] = new HardwareProfileOutput($item); + } + + return new TraversablePaginator($items, $paginator->getCurrentPage(), $paginator->getItemsPerPage(), $paginator->getTotalItems()); + } + + public function provideItem(Operation $operation, array $uriVariables = [], array $context = []): object|array|null + { + $item = $this->itemProvider->provide($operation, $uriVariables, $context); + + if (!$item) { + throw new NotFoundHttpException('Hardware profile not found'); + } + + return new HardwareProfileOutput($item); + } + + public function provideInput(Operation $operation, array $uriVariables = [], array $context = []): object|array|null + { + if (isset($uriVariables['uuid'])) { + $item = $this->itemProvider->provide($operation, $uriVariables, $context); + + return $item !== null ? new HardwareProfileInput($item) : null; + } + + return new HardwareProfileInput(); + } +} diff --git a/src/State/Provider/HardwareProvider.php b/src/State/Provider/HardwareProvider.php index d62dd29..0342d64 100644 --- a/src/State/Provider/HardwareProvider.php +++ b/src/State/Provider/HardwareProvider.php @@ -65,7 +65,7 @@ class HardwareProvider implements ProviderInterface if (isset($uriVariables['uuid'])) { $item = $this->itemProvider->provide($operation, $uriVariables, $context); - return $item !== null ? new ClientInput($item) : null; + return $item !== null ? new HardwareInput($item) : null; } return new HardwareInput();