Compare commits

...

2 Commits

Author SHA1 Message Date
Manuel Aranda Rosales edee27994b refs #1081. API ImageRepository. Edit Image API with this
testing/ogcore-api/pipeline/head There was a failure building this commit Details
2024-10-29 16:57:30 +01:00
Manuel Aranda Rosales 5dc8ffae8c refs #1083. New entity ImageRepository 2024-10-29 16:56:23 +01:00
17 changed files with 475 additions and 6 deletions

View File

@ -0,0 +1,30 @@
resources:
App\Entity\ImageRepository:
processor: App\State\Processor\ImageRepositoryProcessor
input: App\Dto\Input\ImageRepositoryInput
output: App\Dto\Output\ImageRepositoryOutput
normalizationContext:
groups: ['default', 'repository:read']
denormalizationContext:
groups: ['repository:write']
operations:
ApiPlatform\Metadata\GetCollection:
provider: App\State\Provider\ImageRepositoryProvider
filters:
- 'api_platform.filter.repository.order'
- 'api_platform.filter.repository.search'
ApiPlatform\Metadata\Get:
provider: App\State\Provider\ImageRepositoryProvider
ApiPlatform\Metadata\Put:
provider: App\State\Provider\ImageRepositoryProvider
ApiPlatform\Metadata\Patch:
provider: App\State\Provider\ImageRepositoryProvider
ApiPlatform\Metadata\Post: ~
ApiPlatform\Metadata\Delete: ~
properties:
App\Entity\ImageRepository:
id:
identifier: false
uuid:
identifier: true

View File

@ -3,6 +3,8 @@ resources:
processor: App\State\Processor\PartitionProcessor
input: App\Dto\Input\PartitionInput
output: App\Dto\Output\PartitionOutput
orderBy:
partitionNumber: 'ASC'
normalizationContext:
groups: ['default', 'partition:read']
denormalizationContext:

View File

@ -17,7 +17,7 @@ resources:
ApiPlatform\Metadata\Get:
provider: App\State\Provider\SoftwareProfileProvider
normalizationContext:
groups: ['software-profile:item:get', 'software-profile:read:collection:short']
groups: ['software-profile:read', 'software-profile:item:get', 'software-profile:read:collection:short']
ApiPlatform\Metadata\Put:
provider: App\State\Provider\SoftwareProfileProvider
ApiPlatform\Metadata\Patch:

View File

@ -152,3 +152,8 @@ services:
bind:
$collectionProvider: '@api_platform.doctrine.orm.state.collection_provider'
$itemProvider: '@api_platform.doctrine.orm.state.item_provider'
App\State\Provider\ImageRepositoryProvider:
bind:
$collectionProvider: '@api_platform.doctrine.orm.state.collection_provider'
$itemProvider: '@api_platform.doctrine.orm.state.item_provider'

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241029112630 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE image_repository (id INT AUTO_INCREMENT NOT NULL, uuid CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', migration_id VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, created_by VARCHAR(255) DEFAULT NULL, updated_by VARCHAR(255) DEFAULT NULL, ip VARCHAR(255) NOT NULL, comments VARCHAR(255) DEFAULT NULL, name VARCHAR(255) NOT NULL, UNIQUE INDEX UNIQ_302040FBD17F50A6 (uuid), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE image_repository');
}
}

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241029145213 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE image ADD repository_id INT NOT NULL');
$this->addSql('ALTER TABLE image ADD CONSTRAINT FK_C53D045F50C9D4F7 FOREIGN KEY (repository_id) REFERENCES image_repository (id)');
$this->addSql('CREATE INDEX IDX_C53D045F50C9D4F7 ON image (repository_id)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE image DROP FOREIGN KEY FK_C53D045F50C9D4F7');
$this->addSql('DROP INDEX IDX_C53D045F50C9D4F7 ON image');
$this->addSql('ALTER TABLE image DROP repository_id');
}
}

View File

@ -4,6 +4,7 @@ namespace App\Dto\Input;
use ApiPlatform\Metadata\ApiProperty;
use App\Dto\Output\ClientOutput;
use App\Dto\Output\ImageRepositoryOutput;
use App\Dto\Output\OrganizationalUnitOutput;
use App\Dto\Output\SoftwareProfileOutput;
use App\Entity\Image;
@ -50,6 +51,10 @@ final class ImageInput
#[ApiProperty(description: 'The software profile of the image')]
public ?SoftwareProfileOutput $softwareProfile = null;
#[Groups(['image:write'])]
#[ApiProperty(description: 'The image repository of the image')]
public ?ImageRepositoryOutput $imageRepository = null;
#[Groups(['image:write'])]
public ?bool $remotePc = false;
@ -72,6 +77,10 @@ final class ImageInput
if ($image->getSoftwareProfile()) {
$this->softwareProfile = new SoftwareProfileOutput($image->getSoftwareProfile());
}
if ($image->getRepository()) {
$this->imageRepository = new ImageRepositoryOutput($image->getRepository());
}
}
public function createOrUpdateEntity(?Image $image = null): Image
@ -89,6 +98,7 @@ final class ImageInput
$image->setInfo($this->info);
$image->setSize($this->size);
$image->setSoftwareProfile($this->softwareProfile->getEntity());
$image->setRepository($this->imageRepository->getEntity());
$image->setRemotePc($this->remotePc);
return $image;

View File

@ -0,0 +1,56 @@
<?php
namespace App\Dto\Input;
use ApiPlatform\Metadata\ApiProperty;
use App\Dto\Output\ClientOutput;
use App\Dto\Output\ImageOutput;
use App\Dto\Output\OperativeSystemOutput;
use App\Dto\Output\OrganizationalUnitOutput;
use App\Entity\HardwareProfile;
use App\Entity\ImageRepository;
use App\Entity\Menu;
use App\Entity\Partition;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
final class ImageRepositoryInput
{
#[Groups(['repository:write'])]
#[ApiProperty(description: 'The name of the repository', example: "Repository 1")]
public ?string $name = null;
#[Groups(['repository:write'])]
#[ApiProperty(description: 'The IP of the repository', example: "")]
public ?string $ip = null;
#[Groups(['repository:write'])]
#[ApiProperty(description: 'The comments of the repository', example: "Repository 1 comments")]
public ?string $comments = null;
public function __construct(?ImageRepository $repository = null)
{
if (!$repository) {
return;
}
$this->name = $repository->getName();
$this->ip = $repository->getIp();
$this->comments = $repository->getComments();
}
public function createOrUpdateEntity(?ImageRepository $repository = null): ImageRepository
{
if (!$repository) {
$repository = new ImageRepository();
}
$repository->setName($this->name);
$repository->setIp($this->ip);
$repository->setComments($this->comments);
return $repository;
}
}

View File

@ -101,10 +101,7 @@ final class PartitionInput
$partition->setOperativeSystem($this->operativeSystem->getEntity());
}
$partition->setClient($this->client->getEntity());
if ($this->memoryUsage) {
$partition->setMemoryUsage($this->memoryUsage * 100);
}
$partition->setMemoryUsage($this->memoryUsage * 100);
if ($this->image) {
$partition->setImage($this->image->getEntity());

View File

@ -39,6 +39,9 @@ final class ImageOutput extends AbstractOutput
#[Groups(['image:read'])]
public ?SoftwareProfileOutput $softwareProfile = null;
#[Groups(['image:read'])]
public ?ImageRepositoryOutput $imageRepository = null;
#[Groups(['image:read'])]
public \DateTime $createdAt;
@ -58,6 +61,7 @@ final class ImageOutput extends AbstractOutput
$this->info = $image->getInfo();
$this->size = $image->getSize();
$this->softwareProfile = $image->getSoftwareProfile() ? new SoftwareProfileOutput($image->getSoftwareProfile()) : null;
$this->imageRepository = $image->getRepository() ? new ImageRepositoryOutput($image->getRepository()) : null;
$this->remotePc = $image->isRemotePc();
$this->createdAt = $image->getCreatedAt();
$this->createdBy = $image->getCreatedBy();

View File

@ -0,0 +1,38 @@
<?php
namespace App\Dto\Output;
use ApiPlatform\Metadata\Get;
use App\Entity\ImageRepository;
use Symfony\Component\Serializer\Annotation\Groups;
#[Get(shortName: 'ImageRepository')]
class ImageRepositoryOutput extends AbstractOutput
{
#[Groups(['repository:read', 'image:read'])]
public ?string $name = '';
#[Groups(['repository:read'])]
public ?string $ip = '';
#[Groups(['repository:read'])]
public ?string $comments = '';
#[Groups(['repository:read'])]
public \DateTime $createdAt;
#[Groups(['repository:read'])]
public ?string $createdBy = null;
public function __construct(ImageRepository $imageRepository)
{
parent::__construct($imageRepository);
$this->name = $imageRepository->getName();
$this->ip = $imageRepository->getIp();
$this->comments = $imageRepository->getComments();
$this->createdAt = $imageRepository->getCreatedAt();
$this->createdBy = $imageRepository->getCreatedBy();
}
}

View File

@ -11,7 +11,7 @@ use Symfony\Component\Serializer\Annotation\Groups;
#[Get(shortName: 'OrganizationalUnit')]
final class OrganizationalUnitOutput extends AbstractOutput
{
#[Groups(['organizational-unit:read', "client:read", "user:read", 'organizational-unit:read:collection:short'])]
#[Groups(['organizational-unit:read', "client:read", "user:read", 'organizational-unit:read:collection:short', 'software-profile:read'])]
public string $name;
#[Groups(['organizational-unit:read'])]

View File

@ -46,6 +46,10 @@ class Image extends AbstractEntity
#[ORM\Column]
private ?bool $remotePc = null;
#[ORM\ManyToOne(inversedBy: 'images')]
#[ORM\JoinColumn(nullable: false)]
private ?\App\Entity\ImageRepository $repository = null;
public function __construct()
{
parent::__construct();
@ -189,4 +193,16 @@ class Image extends AbstractEntity
return $this;
}
public function getRepository(): ?\App\Entity\ImageRepository
{
return $this->repository;
}
public function setRepository(?\App\Entity\ImageRepository $repository): static
{
$this->repository = $repository;
return $this;
}
}

View File

@ -0,0 +1,86 @@
<?php
namespace App\Entity;
use App\Repository\ImageRepositoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ImageRepositoryRepository::class)]
class ImageRepository extends AbstractEntity
{
use NameableTrait;
#[ORM\Column(length: 255)]
private ?string $ip = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $comments = null;
/**
* @var Collection<int, Image>
*/
#[ORM\OneToMany(mappedBy: 'repository', targetEntity: Image::class)]
private Collection $images;
public function __construct()
{
parent::__construct();
$this->images = new ArrayCollection();
}
public function getIp(): ?string
{
return $this->ip;
}
public function setIp(string $ip): static
{
$this->ip = $ip;
return $this;
}
public function getComments(): ?string
{
return $this->comments;
}
public function setComments(?string $comments): static
{
$this->comments = $comments;
return $this;
}
/**
* @return Collection<int, Image>
*/
public function getImages(): Collection
{
return $this->images;
}
public function addImage(Image $image): static
{
if (!$this->images->contains($image)) {
$this->images->add($image);
$image->setRepository($this);
}
return $this;
}
public function removeImage(Image $image): static
{
if ($this->images->removeElement($image)) {
// set the owning side to null (unless already changed)
if ($image->getRepository() === $this) {
$image->setRepository(null);
}
}
return $this;
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace App\Repository;
use App\Entity\ImageRepository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<ImageRepository>
*/
class ImageRepositoryRepository extends AbstractRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, ImageRepository::class);
}
}

View File

@ -0,0 +1,68 @@
<?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\ImageRepositoryInput;
use App\Dto\Output\ImageRepositoryOutput;
use App\Repository\ImageRepositoryRepository;
readonly class ImageRepositoryProcessor implements ProcessorInterface
{
public function __construct(
private ImageRepositoryRepository $imageRepository,
private ValidatorInterface $validator
)
{
}
/**
* @throws \Exception
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): ImageRepositoryOutput|null
{
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 = []): ImageRepositoryOutput
{
if (!($data instanceof ImageRepositoryInput)) {
throw new \Exception(sprintf('data is not instance of %s', ImageRepositoryInput::class));
}
$entity = null;
if (isset($uriVariables['uuid'])) {
$entity = $this->imageRepository->findOneByUuid($uriVariables['uuid']);
}
$repository = $data->createOrUpdateEntity($entity);
$this->validator->validate($repository);
$this->imageRepository->save($repository);
return new ImageRepositoryOutput($repository);
}
private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null
{
$user = $this->imageRepository->findOneByUuid($uriVariables['uuid']);
$this->imageRepository->delete($user);
return null;
}
}

View File

@ -0,0 +1,73 @@
<?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\ImageInput;
use App\Dto\Input\ImageRepositoryInput;
use App\Dto\Output\ImageOutput;
use App\Dto\Output\ImageRepositoryOutput;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
readonly class ImageRepositoryProvider implements ProviderInterface
{
public function __construct(
private ProviderInterface $collectionProvider,
private 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 ImageRepositoryOutput($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('Image not found');
}
return new ImageRepositoryOutput($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 ImageRepositoryInput($item) : null;
}
return new ImageRepositoryInput();
}
}