Skip to content

Commit

Permalink
Merge pull request #11 from dcorroyer/5-crud-incomes
Browse files Browse the repository at this point in the history
5 crud incomes
  • Loading branch information
dcorroyer authored Sep 27, 2023
2 parents d317f40 + c7de75b commit 8d6d7a2
Show file tree
Hide file tree
Showing 15 changed files with 808 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"doctrine/doctrine-bundle": "^2.10",
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^2.15",
"lcobucci/clock": "^3.1",
"symfony/console": "~6.3.0",
"symfony/dotenv": "~6.3.0",
"symfony/flex": "^2",
Expand Down
68 changes: 66 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file modified migrations/Version1_0_0.php
100755 → 100644
Empty file.
37 changes: 37 additions & 0 deletions migrations/Version1_0_1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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 Version1_0_1 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 charge_lines (id INT AUTO_INCREMENT NOT NULL, charge_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, amount DOUBLE PRECISION NOT NULL, type VARCHAR(255) NOT NULL, INDEX IDX_592580BE55284914 (charge_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE charges (id INT AUTO_INCREMENT NOT NULL, amount DOUBLE PRECISION NOT NULL, date DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE incomes (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, amount DOUBLE PRECISION NOT NULL, date DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', type VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE charge_lines ADD CONSTRAINT FK_592580BE55284914 FOREIGN KEY (charge_id) REFERENCES charges (id)');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE charge_lines DROP FOREIGN KEY FK_592580BE55284914');
$this->addSql('DROP TABLE charge_lines');
$this->addSql('DROP TABLE charges');
$this->addSql('DROP TABLE incomes');
}
}
27 changes: 27 additions & 0 deletions src/DataFixtures/ChargeFixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\DataFixtures;

use App\Factory\ChargeFactory;
use App\Factory\ChargeLineFactory;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;

class ChargeFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
$charge = ChargeFactory::new()->create()->object();

for ($i = 0; $i < rand(3, 5); ++$i) {
$chargeLine = ChargeLineFactory::new()->create()->object();

$charge->addChargeLine($chargeLine);
}

$manager->persist($charge);
$manager->flush();
}
}
17 changes: 17 additions & 0 deletions src/DataFixtures/IncomeFixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\DataFixtures;

use App\Factory\IncomeFactory;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;

class IncomeFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
IncomeFactory::new()->create();
}
}
107 changes: 107 additions & 0 deletions src/Entity/Charge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace App\Entity;

use App\Repository\ChargeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Lcobucci\Clock\SystemClock;

#[ORM\Entity(repositoryClass: ChargeRepository::class)]
#[ORM\Table(name: 'charges')]
class Charge
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private int $id;

#[ORM\Column]
private float $amount;

#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
private \DateTimeImmutable $date;

/**
* @var Collection<int, ChargeLine>
*/
#[ORM\OneToMany(mappedBy: 'charge', targetEntity: ChargeLine::class)]
private Collection $chargeLines;

public function __construct()
{
$this->chargeLines = new ArrayCollection();
$clock = SystemClock::fromSystemTimezone();
$this->date = $clock->now();
}

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

public function setId(int $id): self
{
$this->id = $id;

return $this;
}

public function getAmount(): float
{
return $this->amount;
}

public function setAmount(float $amount): self
{
$this->amount = $amount;

return $this;
}

public function getDate(): \DateTimeImmutable
{
return $this->date;
}

public function setDate(\DateTimeImmutable $date): self
{
$this->date = $date;

return $this;
}

/**
* @return Collection<int, ChargeLine>
*/
public function getChargeLines(): Collection
{
return $this->chargeLines;
}

public function addChargeLine(ChargeLine $chargeLine): self
{
if (! $this->chargeLines->contains($chargeLine)) {
$this->chargeLines->add($chargeLine);
$chargeLine->setCharge($this);
}

return $this;
}

public function removeChargeLine(ChargeLine $chargeLine): self
{
if ($this->chargeLines->removeElement($chargeLine)) {
// set the owning side to null (unless already changed)
if ($chargeLine->getCharge() === $this) {
$chargeLine->setCharge(null);
}
}

return $this;
}
}
90 changes: 90 additions & 0 deletions src/Entity/ChargeLine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace App\Entity;

use App\Repository\ChargeLineRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: ChargeLineRepository::class)]
#[ORM\Table(name: 'charge_lines')]
class ChargeLine
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private int $id;

#[ORM\Column(length: 255)]
private string $name;

#[ORM\Column]
private float $amount;

#[ORM\Column(length: 255)]
private string $type;

#[ORM\ManyToOne(inversedBy: 'chargeLines')]
private ?Charge $charge = null;

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

public function setId(int $id): self
{
$this->id = $id;

return $this;
}

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

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

return $this;
}

public function getAmount(): float
{
return $this->amount;
}

public function setAmount(float $amount): self
{
$this->amount = $amount;

return $this;
}

public function getType(): string
{
return $this->type;
}

public function setType(string $type): self
{
$this->type = $type;

return $this;
}

public function getCharge(): ?Charge
{
return $this->charge;
}

public function setCharge(?Charge $charge): self
{
$this->charge = $charge;

return $this;
}
}
Loading

0 comments on commit 8d6d7a2

Please sign in to comment.