Skip to content

Commit

Permalink
Create ActivityArea entity and relate it to Space.
Browse files Browse the repository at this point in the history
  • Loading branch information
Akellymourab committed Jan 31, 2025
1 parent a38f48e commit 591ec4e
Show file tree
Hide file tree
Showing 6 changed files with 443 additions and 1 deletion.
34 changes: 34 additions & 0 deletions migrations/Version20250131015424.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20250131015424 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create ActivityArea entity and its relation with Space';
}

public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE activity_area (id UUID NOT NULL, name VARCHAR(20) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE TABLE activity_area_spaces (activity_area_id UUID NOT NULL, space_id UUID NOT NULL, PRIMARY KEY(activity_area_id, space_id))');

$this->addSql('CREATE INDEX IDX_F15AB7E8BD5D367C ON activity_area_spaces (activity_area_id)');
$this->addSql('CREATE INDEX IDX_F15AB7E823575340 ON activity_area_spaces (space_id)');

$this->addSql('ALTER TABLE activity_area_spaces ADD CONSTRAINT FK_F15AB7E8BD5D367C FOREIGN KEY (activity_area_id) REFERENCES activity_area (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE activity_area_spaces ADD CONSTRAINT FK_F15AB7E823575340 FOREIGN KEY (space_id) REFERENCES space (id) ON DELETE CASCADE');
}

public function down(Schema $schema): void
{
$this->addSql('DROP TABLE activity_area_spaces');
$this->addSql('DROP TABLE activity_area');
}
}
152 changes: 152 additions & 0 deletions src/DataFixtures/Entity/ActivityAreaFixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

declare(strict_types=1);

namespace App\DataFixtures\Entity;

use App\Entity\ActivityArea;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Serializer\SerializerInterface;

final class ActivityAreaFixtures extends AbstractFixture implements DependentFixtureInterface
{
public const string ACTIVITY_AREA_ID_PREFIX = 'activity_area';
public const string ACTIVITY_AREA_ID_1 = 'a1b2c3d4-e5f6-7890-1234-56789abcdef0';
public const string ACTIVITY_AREA_ID_2 = 'b2c3d4e5-f678-9012-3456-789abcdef012';
public const string ACTIVITY_AREA_ID_3 = 'c3d4e5f6-7890-1234-5678-9abcdef01234';
public const string ACTIVITY_AREA_ID_4 = 'd4e5f678-9012-3456-789a-bcdef0123456';
public const string ACTIVITY_AREA_ID_5 = 'e5f67890-1234-5678-9abc-def012345678';
public const string ACTIVITY_AREA_ID_6 = 'f6789012-3456-789a-bcde-f01234567890';
public const string ACTIVITY_AREA_ID_7 = '67890123-4567-89ab-cdef-012345678901';
public const string ACTIVITY_AREA_ID_8 = '78901234-5678-9abc-def0-123456789012';
public const string ACTIVITY_AREA_ID_9 = '89012345-6789-abcd-ef01-234567890123';
public const string ACTIVITY_AREA_ID_10 = '90123456-789a-bcde-f012-345678901234';

public const array ACTIVITY_AREAS = [
[
'id' => self::ACTIVITY_AREA_ID_1,
'name' => 'Artes Visuais',
'spaces' => [
SpaceFixtures::SPACE_ID_1,
SpaceFixtures::SPACE_ID_2,
SpaceFixtures::SPACE_ID_3,
],
],
[
'id' => self::ACTIVITY_AREA_ID_2,
'name' => 'Música',
'spaces' => [
SpaceFixtures::SPACE_ID_4,
SpaceFixtures::SPACE_ID_5,
SpaceFixtures::SPACE_ID_6,
],
],
[
'id' => self::ACTIVITY_AREA_ID_3,
'name' => 'Teatro',
'spaces' => [
SpaceFixtures::SPACE_ID_7,
SpaceFixtures::SPACE_ID_8,
SpaceFixtures::SPACE_ID_9,
],
],
[
'id' => self::ACTIVITY_AREA_ID_4,
'name' => 'Dança',
'spaces' => [
SpaceFixtures::SPACE_ID_10,
SpaceFixtures::SPACE_ID_1,
SpaceFixtures::SPACE_ID_2,
],
],
[
'id' => self::ACTIVITY_AREA_ID_5,
'name' => 'Cinema',
'spaces' => [
SpaceFixtures::SPACE_ID_3,
SpaceFixtures::SPACE_ID_4,
SpaceFixtures::SPACE_ID_5,
],
],
[
'id' => self::ACTIVITY_AREA_ID_6,
'name' => 'Literatura',
'spaces' => [
SpaceFixtures::SPACE_ID_6,
SpaceFixtures::SPACE_ID_7,
SpaceFixtures::SPACE_ID_8,
],
],
[
'id' => self::ACTIVITY_AREA_ID_7,
'name' => 'Gastronomia',
'spaces' => [
SpaceFixtures::SPACE_ID_9,
SpaceFixtures::SPACE_ID_10,
SpaceFixtures::SPACE_ID_1,
],
],
[
'id' => self::ACTIVITY_AREA_ID_8,
'name' => 'Design',
'spaces' => [
SpaceFixtures::SPACE_ID_2,
SpaceFixtures::SPACE_ID_3,
SpaceFixtures::SPACE_ID_4,
],
],
[
'id' => self::ACTIVITY_AREA_ID_9,
'name' => 'Artesanato',
'spaces' => [
SpaceFixtures::SPACE_ID_5,
SpaceFixtures::SPACE_ID_6,
SpaceFixtures::SPACE_ID_7,
],
],
[
'id' => self::ACTIVITY_AREA_ID_10,
'name' => 'Fotografia',
'spaces' => [
SpaceFixtures::SPACE_ID_8,
SpaceFixtures::SPACE_ID_9,
SpaceFixtures::SPACE_ID_10,
],
],
];

public function __construct(
protected EntityManagerInterface $entityManager,
protected TokenStorageInterface $tokenStorage,
private readonly SerializerInterface $serializer,
) {
parent::__construct($entityManager, $tokenStorage);
}

public function getDependencies()
{
return [
SpaceFixtures::class,
];
}

public function load(ObjectManager $manager): void
{
$this->truncateTable(ActivityArea::class);
$this->createActivityAreas($manager);
}

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);
$manager->persist($activityArea);
}

$manager->flush();
}
}
87 changes: 87 additions & 0 deletions src/Entity/ActivityArea.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace App\Entity;

use App\Repository\ActivityAreaRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: ActivityAreaRepository::class)]
class ActivityArea
{
#[ORM\Id]
#[ORM\Column(type: UuidType::NAME)]
#[Groups(['activity_area.get'])]
private ?Uuid $id = null;

#[ORM\Column(length: 20)]
private ?string $name = null;

#[ORM\JoinTable(name: 'activity_area_spaces')]
#[ORM\JoinColumn(name: 'activity_area_id', referencedColumnName: 'id')]
#[ORM\InverseJoinColumn(name: 'space_id', referencedColumnName: 'id')]
#[ORM\ManyToMany(targetEntity: Space::class, inversedBy: 'ActivityArea', cascade: ['persist'])]
private Collection $spaces;

public function __construct()
{
$this->spaces = new ArrayCollection();
}

public function getId(): ?Uuid
{
return $this->id;
}

public function setId(Uuid $id): void
{
$this->id = $id;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): void
{
$this->name = $name;
}

public function getSpaces(): Collection
{
return $this->spaces;
}

public function setSpaces(Collection $spaces): void
{
$this->spaces = $spaces;
}

public function addSpace(Space $space): void
{
if (!$this->spaces->contains($space)) {
$this->spaces->add($space);
}
}

public function removeSpace(Space $space): void
{
$this->spaces->removeElement($space);
}

public function toArray(): array
{
return [
'id' => $this->id?->toRfc4122(),
'name' => $this->name,
'spaces' => $this->spaces->map(fn (Space $space) => $space->getId())->toArray(),
];
}
}
30 changes: 29 additions & 1 deletion src/Entity/Space.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use App\Repository\SpaceRepository;
use DateTime;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Types\UuidType;
Expand All @@ -20,7 +22,7 @@ class Space extends AbstractEntity
{
#[ORM\Id]
#[ORM\Column(type: UuidType::NAME)]
#[Groups(['event.get', 'initiative.get', 'opportunity.get', 'space.get'])]
#[Groups(['event.get', 'initiative.get', 'opportunity.get', 'space.get', 'activity_area.get'])]
private ?Uuid $id = null;

#[ORM\Column(length: 100)]
Expand Down Expand Up @@ -50,6 +52,9 @@ class Space extends AbstractEntity
#[Groups(['space.get.item'])]
private ?array $extraFields = null;

#[ORM\ManyToMany(targetEntity: ActivityArea::class, mappedBy: 'spaces')]
private Collection $ActivityAreas;

#[ORM\Column]
#[Groups('space.get')]
private DateTimeImmutable $createdAt;
Expand All @@ -65,6 +70,7 @@ class Space extends AbstractEntity
public function __construct()
{
$this->createdAt = new DateTimeImmutable();
$this->ActivityAreas = new ArrayCollection();
}

public function getId(): ?Uuid
Expand Down Expand Up @@ -137,6 +143,26 @@ public function setAddress(SpaceAddress $address): void
$this->address = $address;
}

public function getActivityAreas(): Collection
{
return $this->ActivityAreas;
}

public function addActivityAreas(ActivityArea $activityAreas): void
{
if (!$this->ActivityAreas->contains($activityAreas)) {
$this->ActivityAreas->add($activityAreas);
$activityAreas->addSpace($this);
}
}

public function removeActivityArea(ActivityArea $activityAreas): void
{
if ($this->ActivityAreas->removeElement($activityAreas)) {
$activityAreas->removeSpace($this);
}
}

public function getCreatedAt(): ?DateTimeImmutable
{
return $this->createdAt;
Expand Down Expand Up @@ -175,6 +201,8 @@ public function toArray(): array
'createdBy' => $this->createdBy->getId()->toRfc4122(),
'parent' => $this->parent?->getId()->toRfc4122(),
'address' => $this->address?->toArray(),
'extraFields' => $this->extraFields,
'ActivityAreas' => $this->ActivityAreas->map(fn (ActivityArea $activityArea) => $activityArea->getId()->toRfc4122())->toArray(),
'createdAt' => $this->createdAt->format(DateFormatHelper::DEFAULT_FORMAT),
'updatedAt' => $this->updatedAt?->format(DateFormatHelper::DEFAULT_FORMAT),
'deletedAt' => $this->deletedAt?->format(DateFormatHelper::DEFAULT_FORMAT),
Expand Down
Loading

0 comments on commit 591ec4e

Please sign in to comment.