Skip to content

Commit

Permalink
Create entity Organization (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrofeitoza authored Aug 29, 2024
1 parent b9ba154 commit bc700ad
Show file tree
Hide file tree
Showing 5 changed files with 440 additions and 0 deletions.
61 changes: 61 additions & 0 deletions migrations/Version20240829143857.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20240829143857 extends AbstractMigration
{
public function getDescription(): string
{
return 'Creates/Drops organization table';
}

public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE organization (
id UUID NOT NULL,
owner_id UUID NOT NULL,
created_by UUID NOT NULL,
name VARCHAR(100) NOT NULL,
description VARCHAR(255) DEFAULT NULL,
created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL,
deleted_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL,
PRIMARY KEY(id)
)');
$this->addSql('CREATE INDEX IDX_C1EE637C7E3C61F9 ON organization (owner_id)');
$this->addSql('CREATE INDEX IDX_C1EE637CDE12AB56 ON organization (created_by)');
$this->addSql('COMMENT ON COLUMN organization.id IS \'(DC2Type:uuid)\'');
$this->addSql('COMMENT ON COLUMN organization.owner_id IS \'(DC2Type:uuid)\'');
$this->addSql('COMMENT ON COLUMN organization.created_by IS \'(DC2Type:uuid)\'');
$this->addSql('COMMENT ON COLUMN organization.created_at IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('CREATE TABLE organizations_agents (
organization_id UUID NOT NULL,
agent_id UUID NOT NULL,
PRIMARY KEY(organization_id, agent_id)
)');
$this->addSql('CREATE INDEX IDX_FAEB3B732C8A3DE ON organizations_agents (organization_id)');
$this->addSql('CREATE INDEX IDX_FAEB3B73414710B ON organizations_agents (agent_id)');
$this->addSql('COMMENT ON COLUMN organizations_agents.organization_id IS \'(DC2Type:uuid)\'');
$this->addSql('COMMENT ON COLUMN organizations_agents.agent_id IS \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE organization ADD CONSTRAINT fk_organization_owner_id_agent FOREIGN KEY (owner_id) REFERENCES agent (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE organization ADD CONSTRAINT fk_organization_agent_id_agent FOREIGN KEY (created_by) REFERENCES agent (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE organizations_agents ADD CONSTRAINT fk_organizations_agents_organization_id_organization FOREIGN KEY (organization_id) REFERENCES organization (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE organizations_agents ADD CONSTRAINT fk_organizations_agents_agent_id_agent FOREIGN KEY (agent_id) REFERENCES agent (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
}

public function down(Schema $schema): void
{
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE organization DROP CONSTRAINT fk_organization_owner_id_agent');
$this->addSql('ALTER TABLE organization DROP CONSTRAINT fk_organization_agent_id_agent');
$this->addSql('ALTER TABLE organizations_agents DROP CONSTRAINT fk_organizations_agents_organization_id_organization');
$this->addSql('ALTER TABLE organizations_agents DROP CONSTRAINT fk_organizations_agents_agent_id_agent');
$this->addSql('DROP TABLE organization');
$this->addSql('DROP TABLE organizations_agents');
}
}
192 changes: 192 additions & 0 deletions src/DataFixtures/OrganizationFixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

declare(strict_types=1);

namespace App\DataFixtures;

use App\Entity\Organization;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Serializer\SerializerInterface;

final class OrganizationFixtures extends Fixture implements DependentFixtureInterface
{
public const string ORGANIZATION_ID_PREFIX = 'project';
public const string ORGANIZATION_ID_1 = 'f077aecd-4830-1b8c-c607-72090dc31f71';
public const string ORGANIZATION_ID_2 = 'a65aa657-c537-1f33-c06e-31c2e219136e';
public const string ORGANIZATION_ID_3 = 'd12ead05-ef32-157a-c59e-4a83147ed9ec';
public const string ORGANIZATION_ID_4 = 'd68da96e-a834-1bb1-cb3d-5ac2c2dbae7b';
public const string ORGANIZATION_ID_5 = 'd430ade5-7f3d-1817-cae0-7152674ade73';
public const string ORGANIZATION_ID_6 = '5d85a939-263f-19b5-c912-7825967271a4';
public const string ORGANIZATION_ID_7 = '26c2aaf2-bf38-11d9-c036-7d6b4e56c350';
public const string ORGANIZATION_ID_8 = '7241a715-453a-12db-c707-725dc3ab988c';
public const string ORGANIZATION_ID_9 = '7cb6a1b8-f33e-1218-cb41-820b0f74e4d1';
public const string ORGANIZATION_ID_10 = '8c4ca8bd-6e33-1b62-c58b-a66969c49f66';

public const array ITEMS = [
[
'id' => self::ORGANIZATION_ID_1,
'name' => 'PHP com Rapadura',
'description' => 'Comunidade de devs PHP do Estado do Ceará',
'createdBy' => AgentFixtures::AGENT_ID_1,
'owner' => AgentFixtures::AGENT_ID_1,
'agents' => [],
'parent' => null,
'space' => null,
'createdAt' => '2024-07-10T11:30:00+00:00',
'updatedAt' => null,
'deletedAt' => null,
],
[
'id' => self::ORGANIZATION_ID_2,
'name' => 'Grupo de Capoeira Axé Zumbi',
'description' => 'Grupo de Capoeira Axé Zumbi',
'createdBy' => AgentFixtures::AGENT_ID_1,
'owner' => AgentFixtures::AGENT_ID_1,
'agents' => [
AgentFixtures::AGENT_ID_1,
AgentFixtures::AGENT_ID_2,
],
'parent' => null,
'space' => null,
'createdAt' => '2024-07-11T10:49:00+00:00',
'updatedAt' => null,
'deletedAt' => null,
],
[
'id' => self::ORGANIZATION_ID_3,
'name' => 'Devs do Sertão',
'description' => 'Grupo de devs que se reúnem velas veredas do sertão',
'createdBy' => AgentFixtures::AGENT_ID_3,
'owner' => AgentFixtures::AGENT_ID_3,
'parent' => null,
'space' => null,
'createdAt' => '2024-07-16T17:22:00+00:00',
'updatedAt' => null,
'deletedAt' => null,
],
[
'id' => self::ORGANIZATION_ID_4,
'name' => 'SertãoDev',
'description' => 'Cooperativa de devs do Estado do Ceará',
'createdBy' => AgentFixtures::AGENT_ID_1,
'owner' => AgentFixtures::AGENT_ID_1,
'parent' => null,
'space' => null,
'createdAt' => '2024-07-17T15:12:00+00:00',
'updatedAt' => null,
'deletedAt' => null,
],
[
'id' => self::ORGANIZATION_ID_5,
'name' => 'De RapEnte',
'description' => 'Grupo de Rap e Repente da caatinga nordestina',
'createdBy' => AgentFixtures::AGENT_ID_3,
'owner' => AgentFixtures::AGENT_ID_3,
'parent' => null,
'space' => null,
'createdAt' => '2024-07-22T16:20:00+00:00',
'updatedAt' => null,
'deletedAt' => null,
],
[
'id' => self::ORGANIZATION_ID_6,
'name' => 'Comunidade Vida com Cristo',
'description' => 'Grupo de oração destinado a cristãos de boa fé',
'createdBy' => AgentFixtures::AGENT_ID_2,
'owner' => AgentFixtures::AGENT_ID_2,
'parent' => null,
'space' => null,
'createdAt' => '2024-08-10T11:26:00+00:00',
'updatedAt' => null,
'deletedAt' => null,
],
[
'id' => self::ORGANIZATION_ID_7,
'name' => 'Candomblé Raizes do Brasil ',
'description' => 'Grupo de praticantes do candomblé - Natal-RN',
'createdBy' => AgentFixtures::AGENT_ID_1,
'owner' => AgentFixtures::AGENT_ID_1,
'parent' => null,
'space' => null,
'createdAt' => '2024-08-11T15:54:00+00:00',
'updatedAt' => null,
'deletedAt' => null,
],
[
'id' => self::ORGANIZATION_ID_8,
'name' => 'Baião de Dev',
'description' => 'Grupo de desenvolvedores do nordeste',
'createdBy' => AgentFixtures::AGENT_ID_1,
'owner' => AgentFixtures::AGENT_ID_1,
'parent' => null,
'space' => null,
'createdAt' => '2024-08-12T14:24:00+00:00',
'updatedAt' => null,
'deletedAt' => null,
],
[
'id' => self::ORGANIZATION_ID_9,
'name' => 'PHPeste',
'description' => 'Organização da Conferencia de PHP do Nordeste',
'createdBy' => AgentFixtures::AGENT_ID_1,
'owner' => AgentFixtures::AGENT_ID_1,
'parent' => self::ORGANIZATION_ID_8,
'space' => null,
'createdAt' => '2024-08-13T20:25:00+00:00',
'updatedAt' => null,
'deletedAt' => null,
],
[
'id' => self::ORGANIZATION_ID_10,
'name' => 'Banda de Forró tô nem veno',
'description' => 'Banda de forró formada com pessoas de baixa ou nenhuma visão',
'createdBy' => AgentFixtures::AGENT_ID_1,
'owner' => AgentFixtures::AGENT_ID_1,
'parent' => self::ORGANIZATION_ID_9,
'space' => null,
'createdAt' => '2024-08-14T10:00:00+00:00',
'updatedAt' => null,
'deletedAt' => null,
],
];

public function __construct(
private readonly SerializerInterface $serializer,
) {
}

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

public function load(ObjectManager $manager): void
{
foreach (self::ITEMS as $objectData) {
$agents = $objectData['agents'] ?? [];
unset($objectData['agents']);

/* @var Organization $object */
$object = $this->serializer->denormalize($objectData, Organization::class);

foreach ($agents ?? [] as $agentId) {
$object->addAgent(
$this->getReference(sprintf('%s-%s', AgentFixtures::AGENT_ID_PREFIX, $agentId))
);
}

$object->setCreatedBy($this->getReference(sprintf('%s-%s', AgentFixtures::AGENT_ID_PREFIX, $objectData['createdBy'])));
$object->setOwner($this->getReference(sprintf('%s-%s', AgentFixtures::AGENT_ID_PREFIX, $objectData['owner'])));

$this->setReference(sprintf('%s-%s', self::ORGANIZATION_ID_PREFIX, $objectData['id']), $object);

$manager->persist($object);
}

$manager->flush();
}
}
20 changes: 20 additions & 0 deletions src/Entity/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use App\Repository\AgentRepository;
use DateTime;
use DateTimeImmutable;
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\Uid\Uuid;
Expand All @@ -21,6 +23,9 @@ class Agent extends AbstractEntity
#[ORM\Column(length: 100)]
private ?string $name = null;

#[ORM\ManyToMany(targetEntity: Organization::class, mappedBy: 'agents')]
private Collection $organizations;

#[ORM\Column]
private DateTimeImmutable $createdAt;

Expand All @@ -30,6 +35,11 @@ class Agent extends AbstractEntity
#[ORM\Column(nullable: true)]
private ?DateTime $deletedAt = null;

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

public function getId(): ?Uuid
{
return $this->id;
Expand All @@ -50,6 +60,16 @@ public function setName(string $name): void
$this->name = $name;
}

public function getOrganizations(): Collection
{
return $this->organizations;
}

public function addOrganization(Organization $organization): void
{
$this->organizations->add($organization);
}

public function getCreatedAt(): ?DateTimeImmutable
{
return $this->createdAt;
Expand Down
Loading

0 comments on commit bc700ad

Please sign in to comment.