diff --git a/api/composer.json b/api/composer.json index 47725b1..74ca054 100644 --- a/api/composer.json +++ b/api/composer.json @@ -73,7 +73,7 @@ ], "setup": [ "php bin/console doctrine:database:create", - "php bin/console make:entity" + "php bin/console doctrine:migrations:migrate" ], "test_setup_db": "php bin/console doctrine:fixtures:load --env=test", "test": "php bin/phpunit", diff --git a/api/migrations/Version20231118065043.php b/api/migrations/Version20231118065043.php new file mode 100644 index 0000000..85a3a1d --- /dev/null +++ b/api/migrations/Version20231118065043.php @@ -0,0 +1,35 @@ +addSql('CREATE TABLE favorite (id INT AUTO_INCREMENT NOT NULL, fruit_id INT NOT NULL, date_added DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_68C58ED9BAC115F0 (fruit_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE favorite ADD CONSTRAINT FK_68C58ED9BAC115F0 FOREIGN KEY (fruit_id) REFERENCES fruits (id)'); + $this->addSql('ALTER TABLE fruits CHANGE created_at created_at DATETIME DEFAULT NULL, CHANGE updated_at updated_at DATETIME DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE favorite DROP FOREIGN KEY FK_68C58ED9BAC115F0'); + $this->addSql('DROP TABLE favorite'); + $this->addSql('ALTER TABLE fruits CHANGE created_at created_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', CHANGE updated_at updated_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\''); + } +} diff --git a/api/src/Entity/Favorite.php b/api/src/Entity/Favorite.php new file mode 100644 index 0000000..efe2cce --- /dev/null +++ b/api/src/Entity/Favorite.php @@ -0,0 +1,52 @@ +id; + } + + public function getFruit(): ?Fruit + { + return $this->fruit; + } + + public function setFruit(Fruit $fruit): static + { + $this->fruit = $fruit; + + return $this; + } + + public function getDateAdded(): ?\DateTimeInterface + { + return $this->dateAdded; + } + + public function setDateAdded(?\DateTimeInterface $dateAdded): static + { + $this->dateAdded = $dateAdded; + + return $this; + } +} diff --git a/api/src/Entity/Fruit.php b/api/src/Entity/Fruit.php index e9deea1..b890bb7 100644 --- a/api/src/Entity/Fruit.php +++ b/api/src/Entity/Fruit.php @@ -89,6 +89,9 @@ class Fruit #[ORM\Column(nullable: true)] private ?\DateTime $updatedAt = null; + #[ORM\OneToOne(mappedBy: 'fruit', cascade: ['persist', 'remove'])] + private ?Favorite $favorite = null; + public function getId(): ?int { return $this->id; diff --git a/api/src/Repository/FavoriteRepository.php b/api/src/Repository/FavoriteRepository.php new file mode 100644 index 0000000..71dee5c --- /dev/null +++ b/api/src/Repository/FavoriteRepository.php @@ -0,0 +1,48 @@ + + * + * @method Favorite|null find($id, $lockMode = null, $lockVersion = null) + * @method Favorite|null findOneBy(array $criteria, array $orderBy = null) + * @method Favorite[] findAll() + * @method Favorite[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + */ +class FavoriteRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Favorite::class); + } + +// /** +// * @return Favorite[] Returns an array of Favorite objects +// */ +// public function findByExampleField($value): array +// { +// return $this->createQueryBuilder('f') +// ->andWhere('f.exampleField = :val') +// ->setParameter('val', $value) +// ->orderBy('f.id', 'ASC') +// ->setMaxResults(10) +// ->getQuery() +// ->getResult() +// ; +// } + +// public function findOneBySomeField($value): ?Favorite +// { +// return $this->createQueryBuilder('f') +// ->andWhere('f.exampleField = :val') +// ->setParameter('val', $value) +// ->getQuery() +// ->getOneOrNullResult() +// ; +// } +} diff --git a/api/src/Service/FruitDecomulator.php b/api/src/Service/FruitDecomulator.php index c0ca053..909355b 100644 --- a/api/src/Service/FruitDecomulator.php +++ b/api/src/Service/FruitDecomulator.php @@ -27,7 +27,11 @@ public function __invoke(): void $connection = $this->em->getConnection(); $platform = $connection->getDatabasePlatform(); $table = $this->em->getClassMetadata(Fruit::class)->getTableName(); + // @INFO: Disable foreign key checking when truncating + $connection->executeStatement("SET foreign_key_checks = 0;"); $connection->executeStatement($platform->getTruncateTableSQL($table, true)); + // @INFO: Put back the original setting + $connection->executeStatement("SET foreign_key_checks = 0;"); // @INFO: Purge cache $cache = new FilesystemAdapter(); diff --git a/todo.md b/todo.md index cc37ded..3cd697c 100644 --- a/todo.md +++ b/todo.md @@ -23,6 +23,10 @@ API: [ ] - Need to create pagination /fruits [ ] - Need to accept POST data [POST] /fruit [x] - Send a mail once the fruits:fetch is executed +[x] - Search or filter via Name or Family +[x] - Send a mail once the fruits:fetch is executed +[x] - CRUD + Database @@ -39,14 +43,11 @@ fruits: - calories (float) default 0f - source (string) options: FRUITY_VICE_API | FRUITY_VICE_APP -users: +favorites + - id (primary) + - fruit_id (One-To-One) + - date_added (datetime) -[ ] - Search or filter via Name or Family -[ ] - Send a mail once the fruits:fetch is executed -[ ] - CRUD - - - ------ Fruits Test Task