refs #427. Added Hardware profile MVP
parent
ccc33d7615
commit
2f5fba6e2e
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
|
@ -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'
|
|
@ -37,4 +37,18 @@ services:
|
|||
parent: 'api_platform.doctrine.orm.boolean_filter'
|
||||
arguments: [ { 'enabled': ~ } ]
|
||||
tags:
|
||||
- [ 'api_platform.filter' ]
|
||||
- [ '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' ]
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Dto\Input\ChangeOrganizationalUnitInput;
|
||||
use App\Entity\Client;
|
||||
use App\Repository\ClientRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class ChangeOrganizationalUnitAction extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ClientRepository $clientRepository,
|
||||
private readonly EntityManagerInterface $entityManager
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function __invoke(ChangeOrganizationalUnitInput $input): JsonResponse
|
||||
{
|
||||
foreach ($input->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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace App\Dto\Input;
|
||||
|
||||
use App\Dto\Output\OrganizationalUnitOutput;
|
||||
use App\Entity\Client;
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
final class ChangeOrganizationalUnitInput
|
||||
{
|
||||
/**
|
||||
* @var Client[]
|
||||
*/
|
||||
#[Assert\GreaterThan(1)]
|
||||
#[Groups(['user-group:write'])]
|
||||
public ?array $clients = [];
|
||||
|
||||
#[Assert\NotNull]
|
||||
#[Groups(['user-group:write'])]
|
||||
public ?OrganizationalUnitOutput $organizationalUnit = null;
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
namespace App\Dto\Input;
|
||||
|
||||
use ApiPlatform\Metadata\ApiProperty;
|
||||
use App\Dto\Output\OrganizationalUnitOutput;
|
||||
use App\Entity\HardwareProfile;
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
|
||||
final class HardwareProfileInput
|
||||
{
|
||||
#[Groups(['hardware-profile:write'])]
|
||||
#[ApiProperty(description: 'The description of the hardware profile', example: "Hardware 1 description")]
|
||||
public ?string $description = null;
|
||||
|
||||
#[Groups(['hardware-profile:write'])]
|
||||
#[ApiProperty(description: 'Comments of the hardware profile', example: "Hardware 1")]
|
||||
public ?string $comments = null;
|
||||
|
||||
#[Groups(['hardware-profile:write'])]
|
||||
#[ApiProperty(description: 'The organizational unit of the hardware profile')]
|
||||
public ?OrganizationalUnitOutput $organizationalUnit = null;
|
||||
|
||||
public function __construct(?HardwareProfile $hardwareProfile = null)
|
||||
{
|
||||
if (!$hardwareProfile) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace App\Dto\Output;
|
||||
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use App\Entity\HardwareProfile;
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
|
||||
#[Get(shortName: 'HardwareProfile')]
|
||||
final class HardwareProfileOutput extends AbstractOutput
|
||||
{
|
||||
#[Groups(['hardware-profile:read'])]
|
||||
public ?string $description = '';
|
||||
|
||||
#[Groups(['hardware-profile:read'])]
|
||||
public ?string $comments = '';
|
||||
|
||||
#[Groups(['hardware-profile:read'])]
|
||||
public ?OrganizationalUnitOutput $organizationalUnit = null;
|
||||
|
||||
#[Groups(['hardware-profile:read'])]
|
||||
public \DateTime $createAt;
|
||||
|
||||
#[Groups(['hardware-profile:read'])]
|
||||
public ?string $createBy = null;
|
||||
|
||||
public function __construct(HardwareProfile $hardwareProfile)
|
||||
{
|
||||
parent::__construct($hardwareProfile);
|
||||
|
||||
$this->description = $hardwareProfile->getDescription();
|
||||
$this->comments = $hardwareProfile->getComments();
|
||||
if($hardwareProfile->getOrganizationalUnit()) {
|
||||
$this->organizationalUnit = new OrganizationalUnitOutput($hardwareProfile->getOrganizationalUnit());
|
||||
}
|
||||
$this->createAt = $hardwareProfile->getCreatedAt();
|
||||
$this->createBy = $hardwareProfile->getCreatedBy();
|
||||
}
|
||||
}
|
|
@ -9,35 +9,10 @@ use Doctrine\Persistence\ManagerRegistry;
|
|||
/**
|
||||
* @extends ServiceEntityRepository<HardwareProfile>
|
||||
*/
|
||||
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()
|
||||
// ;
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace App\State\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Delete;
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\Metadata\Patch;
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use ApiPlatform\Metadata\Put;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use ApiPlatform\Validator\ValidatorInterface;
|
||||
use App\Dto\Input\HardwareInput;
|
||||
use App\Dto\Input\HardwareProfileInput;
|
||||
use App\Dto\Output\HardwareProfileOutput;
|
||||
use App\Repository\HardwareProfileRepository;
|
||||
|
||||
readonly class HardwareProfileProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private HardwareProfileRepository $hardwareProfileRepository,
|
||||
private ValidatorInterface $validator
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): HardwareProfileOutput
|
||||
{
|
||||
switch ($operation){
|
||||
case $operation instanceof Post:
|
||||
case $operation instanceof Put:
|
||||
case $operation instanceof Patch:
|
||||
return $this->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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
namespace App\State\Provider;
|
||||
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\Metadata\Patch;
|
||||
use ApiPlatform\Metadata\Put;
|
||||
use ApiPlatform\State\Pagination\TraversablePaginator;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Dto\Input\ClientInput;
|
||||
use App\Dto\Input\HardwareInput;
|
||||
use App\Dto\Input\HardwareProfileInput;
|
||||
use App\Dto\Output\ClientOutput;
|
||||
use App\Dto\Output\HardwareOutput;
|
||||
use App\Dto\Output\HardwareProfileOutput;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class HardwareProfileProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ProviderInterface $collectionProvider,
|
||||
private readonly ProviderInterface $itemProvider
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
||||
{
|
||||
switch ($operation){
|
||||
case $operation instanceof GetCollection:
|
||||
return $this->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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue