Skip to content

Commit e44a3d9

Browse files
committed
Separated manual & planned meals tables
1 parent 02cefcc commit e44a3d9

16 files changed

+336
-163
lines changed

migrations/Version20220730172558.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20220730172558 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('CREATE TABLE aln_manual_meal (id INT AUTO_INCREMENT NOT NULL, feeder_id INT NOT NULL, distributed_on DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', amount SMALLINT NOT NULL, previous_meal VARCHAR(5) DEFAULT NULL, INDEX IDX_B2B660221274E059 (feeder_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
24+
$this->addSql('CREATE TABLE aln_planned_meal (id INT AUTO_INCREMENT NOT NULL, feeder_id INT NOT NULL, planning_id INT NOT NULL, time VARCHAR(5) DEFAULT NULL, amount SMALLINT NOT NULL, is_enabled TINYINT(1) NOT NULL, INDEX IDX_4B9EA6C71274E059 (feeder_id), INDEX IDX_4B9EA6C73D865311 (planning_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
25+
$this->addSql('ALTER TABLE aln_manual_meal ADD CONSTRAINT FK_B2B660221274E059 FOREIGN KEY (feeder_id) REFERENCES aln_feeder (id)');
26+
$this->addSql('ALTER TABLE aln_planned_meal ADD CONSTRAINT FK_4B9EA6C71274E059 FOREIGN KEY (feeder_id) REFERENCES aln_feeder (id)');
27+
$this->addSql('ALTER TABLE aln_planned_meal ADD CONSTRAINT FK_4B9EA6C73D865311 FOREIGN KEY (planning_id) REFERENCES aln_planning (id)');
28+
$this->addSql('DROP TABLE aln_meal');
29+
}
30+
31+
public function down(Schema $schema): void
32+
{
33+
// this down() migration is auto-generated, please modify it to your needs
34+
$this->addSql('CREATE TABLE aln_meal (id INT AUTO_INCREMENT NOT NULL, feeder_id INT NOT NULL, planning_id INT DEFAULT NULL, distributed_on DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', time VARCHAR(5) CHARACTER SET utf8mb4 DEFAULT NULL COLLATE `utf8mb4_unicode_ci`, amount SMALLINT NOT NULL, is_enabled TINYINT(1) NOT NULL, INDEX IDX_27A3E6D1274E059 (feeder_id), INDEX IDX_27A3E6D3D865311 (planning_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB COMMENT = \'\' ');
35+
$this->addSql('ALTER TABLE aln_meal ADD CONSTRAINT FK_27A3E6D1274E059 FOREIGN KEY (feeder_id) REFERENCES aln_feeder (id)');
36+
$this->addSql('ALTER TABLE aln_meal ADD CONSTRAINT FK_27A3E6D3D865311 FOREIGN KEY (planning_id) REFERENCES aln_planning (id)');
37+
$this->addSql('DROP TABLE aln_manual_meal');
38+
$this->addSql('DROP TABLE aln_planned_meal');
39+
}
40+
}

src/Controller/ChangePlanningController.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
use App\ApiPlatform\Dto\PlanningInput;
88
use App\Entity\AlnFeeder;
9-
use App\Entity\AlnMeal;
9+
use App\Entity\AlnPlannedMeal;
1010
use App\Entity\AlnPlanning;
1111
use App\Queue\MessageEnqueueInterface;
12-
use App\Repository\AlnMealRepository;
12+
use App\Repository\AlnPlannedMealRepository;
1313
use App\Repository\AlnPlanningRepository;
1414
use App\Socket\Messages\ChangePlanningMessage;
1515
use Doctrine\Persistence\ManagerRegistry;
@@ -20,13 +20,13 @@
2020
final class ChangePlanningController extends AbstractSocketController
2121
{
2222
private AlnPlanningRepository $planningRepository;
23-
private AlnMealRepository $mealRepository;
23+
private AlnPlannedMealRepository $mealRepository;
2424
private ManagerRegistry $doctrine;
2525

2626
public function __construct(
2727
MessageEnqueueInterface $queue,
2828
AlnPlanningRepository $planningRepository,
29-
AlnMealRepository $mealRepository,
29+
AlnPlannedMealRepository $mealRepository,
3030
ManagerRegistry $doctrine
3131
) {
3232
$this->doctrine = $doctrine;
@@ -49,11 +49,11 @@ public function __invoke(AlnFeeder $data): Response
4949
$alnPlanning = new AlnPlanning();
5050
$feeder->addPlanning($alnPlanning);
5151
foreach ($planning->meals as $meal) {
52-
$alnMeal = new AlnMeal();
52+
$alnMeal = new AlnPlannedMeal();
5353
$alnMeal->setTime($meal->time->toArray());
5454
$alnMeal->setAmount($meal->amount);
5555
$alnMeal->setIsEnabled($meal->isEnabled);
56-
$feeder->addMeal($alnMeal);
56+
$alnMeal->setFeeder($feeder);
5757
$alnPlanning->addMeal($alnMeal);
5858
$this->mealRepository->add($alnMeal);
5959
}

src/Controller/FeedNowController.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use ApiPlatform\Core\Validator\ValidatorInterface;
88
use App\Entity\AlnFeeder;
9-
use App\Entity\AlnMeal;
9+
use App\Entity\AlnManualMeal;
1010
use App\Queue\MessageEnqueueInterface;
11-
use App\Repository\AlnMealRepository;
11+
use App\Repository\AlnManualMealRepository;
1212
use App\Socket\Messages\FeedNowMessage;
1313
use Doctrine\Persistence\ManagerRegistry;
1414
use Safe\DateTimeImmutable;
@@ -19,13 +19,13 @@
1919
final class FeedNowController extends AbstractSocketController
2020
{
2121
private ValidatorInterface $validator;
22-
private AlnMealRepository $repository;
22+
private AlnManualMealRepository $repository;
2323
private ManagerRegistry $doctrine;
2424

2525
public function __construct(
2626
MessageEnqueueInterface $queue,
2727
ValidatorInterface $validator,
28-
AlnMealRepository $repository,
28+
AlnManualMealRepository $repository,
2929
ManagerRegistry $doctrine
3030
) {
3131
$this->validator = $validator;
@@ -45,10 +45,10 @@ public function __invoke(AlnFeeder $data): Response
4545
$message = new FeedNowMessage($amount);
4646
$this->sendSocketMessage($feeder, $message);
4747

48-
$meal = new AlnMeal();
48+
$meal = new AlnManualMeal();
4949
$meal->setDistributedOn(new DateTimeImmutable('now'));
5050
$meal->setAmount($amount);
51-
$feeder->addMeal($meal);
51+
$feeder->addManualMeal($meal);
5252
$this->repository->add($meal);
5353

5454
$this->doctrine->getManager()->flush();

src/Entity/AlnFeeder.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,10 @@ class AlnFeeder
233233
private Collection $plannings;
234234

235235
/**
236-
* @var Collection<int, AlnMeal>
236+
* @var Collection<int, AlnManualMeal>
237237
*/
238-
#[ORM\OneToMany(mappedBy: 'feeder', targetEntity: AlnMeal::class, orphanRemoval: true)]
239-
private Collection $meals;
238+
#[ORM\OneToMany(mappedBy: 'feeder', targetEntity: AlnManualMeal::class, orphanRemoval: true)]
239+
private Collection $manualMeals;
240240

241241
#[ApiProperty(required: true, example: 5)]
242242
#[Groups(['feeding:input'])]
@@ -250,7 +250,7 @@ public function __construct()
250250
$this->identifier = '';
251251
$this->name = '';
252252
$this->lastSeen = new DateTimeImmutable('now');
253-
$this->meals = new ArrayCollection();
253+
$this->manualMeals = new ArrayCollection();
254254
$this->plannings = new ArrayCollection();
255255
}
256256

@@ -362,26 +362,26 @@ public function removePlanning(AlnPlanning $planning): self
362362
}
363363

364364
/**
365-
* @return Collection<int, AlnMeal>
365+
* @return Collection<int, AlnManualMeal>
366366
*/
367-
public function getMeals(): Collection
367+
public function getManualMeals(): Collection
368368
{
369-
return $this->meals;
369+
return $this->manualMeals;
370370
}
371371

372-
public function addMeal(AlnMeal $meal): self
372+
public function addManualMeal(AlnManualMeal $meal): self
373373
{
374-
if (!$this->meals->contains($meal)) {
375-
$this->meals[] = $meal;
374+
if (!$this->manualMeals->contains($meal)) {
375+
$this->manualMeals[] = $meal;
376376
$meal->setFeeder($this);
377377
}
378378

379379
return $this;
380380
}
381381

382-
public function removeMeal(AlnMeal $meal): self
382+
public function removeManualMeal(AlnManualMeal $meal): self
383383
{
384-
if ($this->meals->removeElement($meal)) {
384+
if ($this->manualMeals->removeElement($meal)) {
385385
// set the owning side to null (unless already changed)
386386
if ($meal->getFeeder() === $this) {
387387
$meal->setFeeder(null);

src/Entity/AlnManualMeal.php

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Entity;
6+
7+
use App\Dbal\Types\AlnTimeType;
8+
use App\Repository\AlnManualMealRepository;
9+
use Doctrine\DBAL\Types\Types;
10+
use Doctrine\ORM\Mapping as ORM;
11+
12+
#[ORM\Entity(repositoryClass: AlnManualMealRepository::class)]
13+
class AlnManualMeal
14+
{
15+
#[ORM\Id]
16+
#[ORM\GeneratedValue]
17+
#[ORM\Column()]
18+
private ?int $id = null;
19+
20+
#[ORM\ManyToOne(inversedBy: 'meals')]
21+
#[ORM\JoinColumn(nullable: false)]
22+
private ?AlnFeeder $feeder = null;
23+
24+
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
25+
private ?\DateTimeImmutable $distributedOn = null;
26+
27+
/**
28+
* @var int<5, 150>
29+
*/
30+
#[ORM\Column(type: Types::SMALLINT)]
31+
private int $amount;
32+
33+
/**
34+
* @var ?array{hours: int<0, 23>, minutes: int<0, 59>}
35+
*/
36+
#[ORM\Column(type: AlnTimeType::ALN_TIME_TYPE, nullable: true)]
37+
private ?array $previousMeal = null;
38+
39+
public function __construct()
40+
{
41+
$this->amount = 5;
42+
}
43+
44+
public function getId(): ?int
45+
{
46+
return $this->id;
47+
}
48+
49+
public function getFeeder(): ?AlnFeeder
50+
{
51+
return $this->feeder;
52+
}
53+
54+
public function setFeeder(?AlnFeeder $feeder): self
55+
{
56+
$this->feeder = $feeder;
57+
58+
return $this;
59+
}
60+
61+
public function getDistributedOn(): ?\DateTimeImmutable
62+
{
63+
return $this->distributedOn;
64+
}
65+
66+
public function setDistributedOn(?\DateTimeImmutable $distributedOn): self
67+
{
68+
$this->distributedOn = $distributedOn;
69+
70+
return $this;
71+
}
72+
73+
/**
74+
* @return int<5, 150>
75+
*/
76+
public function getAmount(): int
77+
{
78+
return $this->amount;
79+
}
80+
81+
/**
82+
* @param int<5, 150> $amount
83+
*/
84+
public function setAmount(int $amount): self
85+
{
86+
$this->amount = $amount;
87+
88+
return $this;
89+
}
90+
91+
/**
92+
* @return ?array{hours: int<0, 23>, minutes: int<0, 59>}
93+
*/
94+
public function getPreviousMeal(): ?array
95+
{
96+
return $this->previousMeal;
97+
}
98+
99+
/**
100+
* @param ?array{hours: int<0, 23>, minutes: int<0, 59>} $previousMeal
101+
*/
102+
public function setPreviousMeal(?array $previousMeal): self
103+
{
104+
$this->previousMeal = $previousMeal;
105+
106+
return $this;
107+
}
108+
}

src/Entity/AlnMeal.php src/Entity/AlnPlannedMeal.php

+4-18
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
namespace App\Entity;
66

77
use App\Dbal\Types\AlnTimeType;
8-
use App\Repository\AlnMealRepository;
8+
use App\Repository\AlnPlannedMealRepository;
99
use Doctrine\DBAL\Types\Types;
1010
use Doctrine\ORM\Mapping as ORM;
1111
use Symfony\Component\Serializer\Annotation\Groups;
1212
use Symfony\Component\Serializer\Annotation\SerializedName;
1313

14-
#[ORM\Entity(repositoryClass: AlnMealRepository::class)]
15-
class AlnMeal
14+
#[ORM\Entity(repositoryClass: AlnPlannedMealRepository::class)]
15+
class AlnPlannedMeal
1616
{
1717
#[ORM\Id]
1818
#[ORM\GeneratedValue]
@@ -24,11 +24,9 @@ class AlnMeal
2424
private ?AlnFeeder $feeder = null;
2525

2626
#[ORM\ManyToOne(inversedBy: 'meals')]
27+
#[ORM\JoinColumn(nullable: false)]
2728
private ?AlnPlanning $planning = null;
2829

29-
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
30-
private ?\DateTimeImmutable $distributedOn = null;
31-
3230
/**
3331
* @var ?array{hours: int<0, 23>, minutes: int<0, 59>}
3432
*/
@@ -83,18 +81,6 @@ public function setPlanning(?AlnPlanning $planning): self
8381
return $this;
8482
}
8583

86-
public function getDistributedOn(): ?\DateTimeImmutable
87-
{
88-
return $this->distributedOn;
89-
}
90-
91-
public function setDistributedOn(?\DateTimeImmutable $distributedOn): self
92-
{
93-
$this->distributedOn = $distributedOn;
94-
95-
return $this;
96-
}
97-
9884
/**
9985
* @return ?array{hours: int<0, 23>, minutes: int<0, 59>}
10086
*/

src/Entity/AlnPlanning.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class AlnPlanning
2424
private ?AlnFeeder $feeder = null;
2525

2626
/**
27-
* @var Collection<int, AlnMeal>
27+
* @var Collection<int, AlnPlannedMeal>
2828
*/
29-
#[ORM\OneToMany(mappedBy: 'planning', targetEntity: AlnMeal::class)]
29+
#[ORM\OneToMany(mappedBy: 'planning', targetEntity: AlnPlannedMeal::class)]
3030
#[Groups(['feeder:output'])]
3131
private Collection $meals;
3232

@@ -57,14 +57,14 @@ public function setFeeder(?AlnFeeder $feeder): self
5757
}
5858

5959
/**
60-
* @return Collection<int, AlnMeal>
60+
* @return Collection<int, AlnPlannedMeal>
6161
*/
6262
public function getMeals(): Collection
6363
{
6464
return $this->meals;
6565
}
6666

67-
public function addMeal(AlnMeal $meal): self
67+
public function addMeal(AlnPlannedMeal $meal): self
6868
{
6969
if (!$this->meals->contains($meal)) {
7070
$this->meals[] = $meal;
@@ -74,7 +74,7 @@ public function addMeal(AlnMeal $meal): self
7474
return $this;
7575
}
7676

77-
public function removeMeal(AlnMeal $meal): self
77+
public function removeMeal(AlnPlannedMeal $meal): self
7878
{
7979
if ($this->meals->removeElement($meal)) {
8080
// set the owning side to null (unless already changed)

0 commit comments

Comments
 (0)