Explorar o código

Adds event category API resource

Introduces an API resource for retrieving event categories.

This change implements a new API endpoint to fetch event categories,
including their family, subfamily, and gender labels. It also updates
the product configuration to include the new resource.

Relates to feature/V8-6884-demande-dessai
Olivier Massot hai 4 meses
pai
achega
fafde0bea6

+ 1 - 0
config/opentalent/products.yaml

@@ -22,6 +22,7 @@ parameters:
           - DolibarrDocDownload
           - Download
           - Upload
+          - EventCategory
         roles:
           - ROLE_IMPORT
           - ROLE_TAGG

+ 97 - 0
src/ApiResources/Core/EventCategory.php

@@ -0,0 +1,97 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\ApiResources\Core;
+
+use ApiPlatform\Metadata\ApiProperty;
+use ApiPlatform\Metadata\ApiResource;
+use ApiPlatform\Metadata\GetCollection;
+use App\ApiResources\ApiResourcesInterface;
+use App\State\Provider\Core\EventCategoryProvider;
+
+/**
+ * Classe resource qui contient les champs disponibles lors d'un appel à event-category.
+ */
+#[ApiResource(
+    operations: [
+        new GetCollection(
+            uriTemplate: '/event-categories',
+            provider: EventCategoryProvider::class,
+            paginationEnabled: false
+        ),
+    ]
+)]
+class EventCategory implements ApiResourcesInterface
+{
+    #[ApiProperty(identifier: true)]
+    private ?int $id = null;
+
+    private ?string $label = null;
+
+    private ?string $famillyLabel = null;
+
+    private ?string $subfamillyLabel = null;
+
+    private ?string $genderLabel = null;
+
+    public function getId(): ?int
+    {
+        return $this->id;
+    }
+
+    public function setId(?int $id): self
+    {
+        $this->id = $id;
+
+        return $this;
+    }
+
+    public function getLabel(): ?string
+    {
+        return $this->label;
+    }
+
+    public function setLabel(?string $label): self
+    {
+        $this->label = $label;
+
+        return $this;
+    }
+
+    public function getFamillyLabel(): ?string
+    {
+        return $this->famillyLabel;
+    }
+
+    public function setFamillyLabel(?string $famillyLabel): self
+    {
+        $this->famillyLabel = $famillyLabel;
+
+        return $this;
+    }
+
+    public function getSubfamillyLabel(): ?string
+    {
+        return $this->subfamillyLabel;
+    }
+
+    public function setSubfamillyLabel(?string $subfamillyLabel): self
+    {
+        $this->subfamillyLabel = $subfamillyLabel;
+
+        return $this;
+    }
+
+    public function getGenderLabel(): ?string
+    {
+        return $this->genderLabel;
+    }
+
+    public function setGenderLabel(?string $genderLabel): self
+    {
+        $this->genderLabel = $genderLabel;
+
+        return $this;
+    }
+}

+ 1 - 1
src/Entity/Core/Categories.php

@@ -17,7 +17,7 @@ use Doctrine\ORM\Mapping as ORM;
 class Categories
 {
     #[ORM\Id]
-    #[ORM\Column(type: 'mediumint', options: ['unsigned' => true])]
+    #[ORM\Column]
     #[ORM\GeneratedValue]
     private ?int $id = null;
 

+ 15 - 0
src/Entity/Core/Familly.php

@@ -21,8 +21,23 @@ class Familly
     #[ORM\GeneratedValue]
     private ?int $id = null;
 
+    #[ORM\Column(type: 'string')]
+    private string $name;
+
     public function getId(): ?int
     {
         return $this->id;
     }
+
+    public function getName(): string
+    {
+        return $this->name;
+    }
+
+    public function setName(string $name): self
+    {
+        $this->name = $name;
+
+        return $this;
+    }
 }

+ 15 - 0
src/Entity/Core/Gender.php

@@ -21,8 +21,23 @@ class Gender
     #[ORM\GeneratedValue]
     private ?int $id = null;
 
+    #[ORM\Column(type: 'string')]
+    private string $name;
+
     public function getId(): ?int
     {
         return $this->id;
     }
+
+    public function getName(): string
+    {
+        return $this->name;
+    }
+
+    public function setName(string $name): self
+    {
+        $this->name = $name;
+
+        return $this;
+    }
 }

+ 15 - 0
src/Entity/Core/Subfamilly.php

@@ -21,8 +21,23 @@ class Subfamilly
     #[ORM\GeneratedValue]
     private ?int $id = null;
 
+    #[ORM\Column(type: 'string')]
+    private string $name;
+
     public function getId(): ?int
     {
         return $this->id;
     }
+
+    public function getName(): string
+    {
+        return $this->name;
+    }
+
+    public function setName(string $name): self
+    {
+        $this->name = $name;
+
+        return $this;
+    }
 }

+ 68 - 0
src/State/Provider/Core/EventCategoryProvider.php

@@ -0,0 +1,68 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\State\Provider\Core;
+
+use ApiPlatform\Metadata\GetCollection;
+use ApiPlatform\Metadata\Operation;
+use ApiPlatform\State\ProviderInterface;
+use App\ApiResources\Core\EventCategory;
+use App\Entity\Core\Categories;
+use Doctrine\ORM\EntityManagerInterface;
+use Symfony\Component\HttpFoundation\Response;
+
+/**
+ * Class EventCategoryProvider : custom provider pour assurer l'alimentation de la ressource EventCategory.
+ */
+final class EventCategoryProvider implements ProviderInterface
+{
+    public function __construct(
+        private readonly EntityManagerInterface $entityManager,
+    ) {
+    }
+
+    /**
+     * @param mixed[] $uriVariables
+     * @param mixed[] $context
+     *
+     * @return EventCategory[]|EventCategory
+     */
+    public function provide(Operation $operation, array $uriVariables = [], array $context = []): array|EventCategory
+    {
+        if (!$operation instanceof GetCollection) {
+            throw new \RuntimeException('Only GetCollection operation is supported', Response::HTTP_METHOD_NOT_ALLOWED);
+        }
+
+        return $this->getCollection();
+    }
+
+    /**
+     * @return EventCategory[]
+     */
+    private function getCollection(): array
+    {
+        $categoriesRepository = $this->entityManager->getRepository(Categories::class);
+        $categories = $categoriesRepository->findAll();
+
+        $result = [];
+        foreach ($categories as $category) {
+            $eventCategory = new EventCategory();
+            $eventCategory->setId($category->getId());
+
+            // Get the related entities
+            $familly = $category->getFamilly();
+            $subfamilly = $category->getSubfamilly();
+            $gender = $category->getGender();
+
+            // Generate labels for the related entities based on their IDs
+            $eventCategory->setFamillyLabel($familly->getName());
+            $eventCategory->setSubfamillyLabel($subfamilly->getName());
+            $eventCategory->setGenderLabel($gender->getName());
+
+            $result[] = $eventCategory;
+        }
+
+        return $result;
+    }
+}