Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds endpoints for listing, retrieving, and deleting ActivityArea. #658

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/routes/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ api_example:
prefix: /api
name_prefix: api_example_

api_activity_area:
resource: 'api/activity-area.yaml'
prefix: /api/activity-areas
name_prefix: api_activity_area_
trailing_slash_on_root: false

api_agents:
resource: 'api/agent.yaml'
prefix: /api/agents
Expand Down
14 changes: 14 additions & 0 deletions config/routes/api/activity-area.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
get:
path: /{id}
controller: App\Controller\Api\ActivityAreaApiController::get
methods: ['GET']

list:
path: /
controller: App\Controller\Api\ActivityAreaApiController::list
methods: ['GET']

remove:
path: /{id}
controller: App\Controller\Api\ActivityAreaApiController::remove
methods: ['DELETE']
14 changes: 14 additions & 0 deletions public/docs/components/paths/activity-area/Collection.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
get:
tags:
- Áreas de Atividade
summary: Recupera uma lista de áreas de atividade
responses:
'200':
description: Lista de áreas de atividade
content:
application/json:
schema:
type: array
items:
allOf:
- $ref: '../../responses/activity-area/ActivityAreaGetCollectionResponse.yaml'
37 changes: 37 additions & 0 deletions public/docs/components/paths/activity-area/Item.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
get:
parameters:
- $ref: '../../parameters/path/id.yaml'
tags:
- Áreas de Atividade
summary: Recupera uma única área de atividade
responses:
'200':
description: Área de atividade encontrada
content:
application/json:
schema:
$ref: '../../responses/activity-area/ActivityAreaGetItemResponse.yaml'
'404':
description: Área de atividade não encontrada
content:
application/json:
schema:
$ref: '../../responses/activity-area/ActivityAreaNotFound.yaml'

delete:
parameters:
- $ref: '../../parameters/path/id.yaml'
tags:
- Áreas de Atividade
summary: Remove uma única área de atividade
responses:
'204':
description: Não possui conteúdo de resposta
content:
application/json: {}
'404':
description: Área de atividade não encontrada
content:
application/json:
schema:
$ref: '../../responses/activity-area/ActivityAreaNotFound.yaml'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type: object
allOf:
- $ref: './CommonDefinitions.yaml#/ActivityAreaBase'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type: object
allOf:
- $ref: './CommonDefinitions.yaml#/ActivityAreaBase'
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type: object
properties:
error_message:
type: string
default: "not_found"
error_details:
type: object
properties:
description:
type: string
default: "The requested ActivityArea was not found."
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ActivityAreaBase:
type: object
properties:
id:
type: string
format: uuid
description: "Identificador único da área de atividade."
example: "123e4567-e89b-12d3-a456-426614174000"
name:
type: string
maxLength: 20
description: "Nome da área de atividade."
example: "Teatro"
12 changes: 12 additions & 0 deletions public/docs/components/schemas/activity-area.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type: object
properties:
id:
type: string
format: uuid
description: "Identificador único da área de atividade."
example: "123e4567-e89b-12d3-a456-426614174000"
name:
type: string
maxLength: 20
description: "Nome da área de atividade."
example: "Teatro"
9 changes: 9 additions & 0 deletions public/docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ info:
description: |-
API responsável por praticamente todas as funcionalidades de gerenciamento:
- Agentes
- Áreas de atividade
- Espaços
- Eventos
- Faqs
Expand All @@ -30,6 +31,8 @@ tags:
description: Endpoints relacionados a autorização
- name: Agentes
description: Endpoints relacionados aos agentes
- name: Áreas de Atividade
description: Endpoints relacionados às áreas de atividade
- name: Espaços
description: Endpoints relacionados aos espaços
- name: Eventos
Expand All @@ -53,6 +56,10 @@ tags:
- name: Usuários
description: Endpoints relacionados aos usuários
paths:
/activity-areas:
$ref: './components/paths/activity-area/Collection.yaml'
/activity-areas/{id}:
$ref: './components/paths/activity-area/Item.yaml'
/agents:
$ref: './components/paths/agent/Collection.yaml'
/agents/{id}:
Expand Down Expand Up @@ -117,6 +124,8 @@ paths:
$ref: './components/paths/user/Item.yaml'
components:
schemas:
ActivityArea:
$ref: './components/schemas/activity-area.yaml'
Agent:
$ref: './components/schemas/agent.yaml'
Event:
Expand Down
44 changes: 44 additions & 0 deletions src/Controller/Api/ActivityAreaApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace App\Controller\Api;

use App\Helper\EntityIdNormalizerHelper;
use App\Service\Interface\ActivityAreaServiceInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Uid\Uuid;

class ActivityAreaApiController extends AbstractApiController
{
public function __construct(
private readonly ActivityAreaServiceInterface $service,
) {
}

public function get(?Uuid $id): JsonResponse
{
$activityArea = $this->service->get($id);

return $this->json($activityArea, context: ['groups' => ['activity-area.get', 'activity-area.get.item']]);
}

public function list(): JsonResponse
{
return $this->json($this->service->list(), context: [
'groups' => 'activity-area.get',
AbstractNormalizer::CALLBACKS => [
'parent' => [EntityIdNormalizerHelper::class, 'normalizeEntityId'],
],
]);
}

public function remove(?Uuid $id): JsonResponse
{
$this->service->remove($id);

return $this->json(data: [], status: Response::HTTP_NO_CONTENT);
}
}
4 changes: 3 additions & 1 deletion src/DataFixtures/Entity/ActivityAreaFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ private function createActivityAreas(ObjectManager $manager): void
{
foreach (self::ACTIVITY_AREAS as $activityAreaData) {
$activityArea = $this->serializer->denormalize($activityAreaData, ActivityArea::class);
$this->setReference(self::ACTIVITY_AREA_ID_PREFIX.$activityAreaData['id'], $activityArea);

$this->setReference(sprintf('%s-%s', self::ACTIVITY_AREA_ID_PREFIX, $activityAreaData['id']), $activityArea);

$manager->persist($activityArea);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Entity/ActivityArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class ActivityArea
{
#[ORM\Id]
#[ORM\Column(type: UuidType::NAME)]
#[Groups(['space.get', 'space.get.item', 'activity_area.get'])]
#[Groups(['space.get', 'space.get.item', 'activity-area.get', 'activity-area.get.item'])]
private ?Uuid $id = null;

#[ORM\Column(length: 20)]
#[Groups(['space.get', 'space.get.item', 'activity_area.get'])]
#[Groups(['space.get', 'space.get.item', 'activity-area.get', 'activity-area.get.item'])]
private ?string $name = null;

public function getId(): ?Uuid
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Space.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Space extends AbstractEntity
{
#[ORM\Id]
#[ORM\Column(type: UuidType::NAME)]
#[Groups(['event.get', 'initiative.get', 'opportunity.get', 'space.get', 'activity_area.get'])]
#[Groups(['event.get', 'initiative.get', 'opportunity.get', 'space.get', 'activity-area.get'])]
private ?Uuid $id = null;

#[ORM\Column(length: 100)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Exception\ActivityArea;

use App\Exception\ResourceNotFoundException;

class ActivityAreaResourceNotFoundException extends ResourceNotFoundException
{
protected const string RESOURCE = 'ActivityArea';
}
6 changes: 6 additions & 0 deletions src/Repository/ActivityAreaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ public function save(ActivityArea $activityArea): ActivityArea

return $activityArea;
}

public function remove(ActivityArea $activityArea): void
{
$this->getEntityManager()->remove($activityArea);
$this->getEntityManager()->flush();
}
}
2 changes: 2 additions & 0 deletions src/Repository/Interface/ActivityAreaRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
interface ActivityAreaRepositoryInterface
{
public function save(ActivityArea $activityArea): ActivityArea;

public function remove(ActivityArea $activityArea);
}
58 changes: 58 additions & 0 deletions src/Service/ActivityAreaService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace App\Service;

use App\Entity\ActivityArea;
use App\Exception\ActivityArea\ActivityAreaResourceNotFoundException;
use App\Repository\Interface\ActivityAreaRepositoryInterface;
use App\Service\Interface\ActivityAreaServiceInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Uid\Uuid;

readonly class ActivityAreaService extends AbstractEntityService implements ActivityAreaServiceInterface
{
public function __construct(
private ActivityAreaRepositoryInterface $repository,
private Security $security,
) {
parent::__construct($this->security);
}

public function get(Uuid $id): ActivityArea
{
$activityArea = $this->findOneBy(['id' => $id]);

if (null === $activityArea) {
throw new ActivityAreaResourceNotFoundException();
}

return $activityArea;
}

public function list(int $limit = 50, array $params = []): array
{
return $this->repository->findBy(
$params,
['name' => 'ASC'],
$limit
);
}

public function remove(Uuid $id): void
{
$activityArea = $this->findOneBy(['id' => $id]);

if (null === $activityArea) {
throw new ActivityAreaResourceNotFoundException();
}

$this->repository->remove($activityArea);
}

private function findOneBy(array $array): ?ActivityArea
{
return $this->repository->findOneBy($array);
}
}
17 changes: 17 additions & 0 deletions src/Service/Interface/ActivityAreaServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Service\Interface;

use App\Entity\ActivityArea;
use Symfony\Component\Uid\Uuid;

interface ActivityAreaServiceInterface
{
public function get(Uuid $id): ActivityArea;

public function list(int $limit = 50): array;

public function remove(Uuid $id): void;
}
Loading