Skip to content

Commit

Permalink
feat: added favorite table and migration
Browse files Browse the repository at this point in the history
  • Loading branch information
benborla committed Nov 18, 2023
1 parent a168874 commit 9fa0006
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 8 deletions.
2 changes: 1 addition & 1 deletion api/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
35 changes: 35 additions & 0 deletions api/migrations/Version20231118065043.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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 Version20231118065043 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 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)\'');
}
}
52 changes: 52 additions & 0 deletions api/src/Entity/Favorite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Entity;

use App\Repository\FavoriteRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: FavoriteRepository::class)]
class Favorite
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;

#[ORM\OneToOne(inversedBy: 'favorite', cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(nullable: false)]
private ?Fruit $fruit = null;

#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $dateAdded = null;

public function getId(): ?int
{
return $this->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;
}
}
3 changes: 3 additions & 0 deletions api/src/Entity/Fruit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
48 changes: 48 additions & 0 deletions api/src/Repository/FavoriteRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Repository;

use App\Entity\Favorite;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
* @extends ServiceEntityRepository<Favorite>
*
* @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()
// ;
// }
}
4 changes: 4 additions & 0 deletions api/src/Service/FruitDecomulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 8 additions & 7 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 9fa0006

Please sign in to comment.