Base de ingrédient terminé

This commit is contained in:
2026-04-02 18:10:47 +02:00
parent e79cc73e6d
commit 8a173ed2c7
10 changed files with 246 additions and 13 deletions

View File

@@ -9,5 +9,10 @@ class Ingredient extends Model {
public int $num_ingredient;
public string $nom_ingredient;
public function getID(): int
{
return $this->num_ingredient;
}
}

View File

@@ -2,10 +2,13 @@
namespace App\Domain\Ingredients;
use App\Domain\LinkableInterface;
use App\Domain\Recettes\Recette;
use App\Domain\Repository;
use App\Domain\Model;
use App\Kernel;
class IngredientRepository extends Repository {
class IngredientRepository extends Repository implements LinkableInterface {
public static function getEntity(): string
{
@@ -18,12 +21,83 @@ class IngredientRepository extends Repository {
'table' => 'Ingredient',
'columns' => [
'num_ingredient', 'nom_ingredient'
]
],
'link_recettes' => 'Listeingredient'
];
}
public function add( Model $ingredient ): bool {
/**
* Permet d'obtenir un ingrédient spécifique par son ID.
*
* @param int $id
*
* @return Ingredient|null
*/
public function getByID( int $id ): ?Ingredient {
$sqlQuery = "SELECT * FROM {$this->tableName} WHERE num_ingredient = {$id}";
$results = $this->selectGetAll($sqlQuery);
if( $results === null || count( $results ) > 1 )
return null;
return $results[0];
}
/**
* Permet d'obtenir, sous forme de liste, toutes les entrées qui lient des ingrédients.
*
* @param string $linkedTo La table qui permet de faire la liaison des ingrédients avec une autre entité.
* @param string $linkingField Le champ qui permet de faire la liaison des ingrédients avec une autre entité.
* @param Model $linkedEntity L'autre entité.
*
* @return array|null
*/
public function getIdLinkedTo( string $linkedTo, string $linkingField, Model $linkedEntity ): ?array {
$linkName = 'link_' . $linkedTo;
if( !isset( $this->globalStructure[$linkName]))
return null;
$sqlQuery = "SELECT * FROM {$this->globalStructure[$linkName]} WHERE {$linkingField} = {$linkedEntity->getId()};";
$results = $this->selectGetAll($sqlQuery, true);
if( $results === null )
return null;
return $results;
}
public function addLinkBetween( string $linkedTo, string $linkingField, Model $linkedEntity, Model $ingredientEntity ): bool {
$linkName = 'link_' . $linkedTo;
if( !isset( $this->globalStructure[$linkName]))
return false;
$query = "INSERT INTO {$this->globalStructure[$linkName]} ({$linkingField},num_ingredient) VALUES ({$linkedEntity->getId()}, {$ingredientEntity->getID()});";
$statement = Kernel::$DB->pdo->prepare( $query );
return $statement->execute();
}
public function removeLinkBetween(string $linkedTo, string $linkingField, Model $linkedEntity, Model $ingredientEntity ): bool
{
$linkName = 'link_' . $linkedTo;
if( !isset( $this->globalStructure[$linkName]))
return false;
$query = "DELETE FROM {$this->globalStructure[$linkName]} WHERE {$linkingField} = {$linkedEntity->getId()} AND num_ingredient = {$ingredientEntity->getId()};";
$statement = Kernel::$DB->pdo->prepare( $query );
return $statement->execute();
}
public function add( Model $ingredient ): bool {
return $this->addEntity( $ingredient );
}
public function update( Model $ingredient ): bool {
return $this->updateEntity( $ingredient, 'num_ingredient' );
}
public function delete( Model $ingredient ): bool {
return $this->deleteEntity( $ingredient, 'num_ingredient' );
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Domain\Ingredients;
use App\Domain\Model;
interface UseIngredientsInterface {
/**
* Permet de récupérer tous les ingrédients liés à ce Modèle.
* @return array|null
*/
public function getAllLinkedIngredients( Model $entity ): ?array;
public function addAnIngredient( Ingredient $ingredient, Model $entity ): bool;
public function removeAnIngredient( Ingredient $ingredient, Model $entity ): bool;
}

View File

@@ -0,0 +1,13 @@
<?php
namespace App\Domain;
interface LinkableInterface {
public function getIdLinkedTo( string $linkedTo, string $linkingField, Model $linkedEntity ): ?array;
public function addLinkBetween( string $linkedTo, string $linkingField, Model $linkedEntity, Model $entity ): bool;
public function removeLinkBetween( string $linkedTo, string $linkingField, Model $linkedEntity, Model $entity ): bool;
}

View File

@@ -4,5 +4,10 @@ namespace App\Domain;
abstract class Model {
/**
* Getter de l'ID primaire du model spécifique.
* @return int
*/
abstract public function getID(): int;
}

View File

@@ -2,6 +2,8 @@
namespace App\Domain\Recettes;
use App\Domain\Ingredients\IngredientRepository;
use App\Domain\Ingredients\UseIngredientsInterface;
use App\Domain\Model;
use App\Helpers\Markdown;
@@ -15,8 +17,18 @@ class Recette extends Model {
public string $publication_date;
public int $temps_de_preparation;
public function getID(): int
{
return $this->num_recette;
}
public function getHTMLDescription(): string {
return Markdown::convertToHTML( $this->description_recette );
}
public function getAllLinkedIngredients(): ?array
{
return new RecetteRepository()->getAllLinkedIngredients( $this );
}
}

View File

@@ -2,10 +2,13 @@
namespace App\Domain\Recettes;
use App\Domain\Ingredients\Ingredient;
use App\Domain\Ingredients\IngredientRepository;
use App\Domain\Ingredients\UseIngredientsInterface;
use App\Domain\Model;
use App\Domain\Repository;
class RecetteRepository extends Repository {
class RecetteRepository extends Repository implements UseIngredientsInterface {
public static function getEntity(): string
{
@@ -63,4 +66,28 @@ class RecetteRepository extends Repository {
return $this->deleteEntity( $recette, 'num_recette' );
}
public function getAllLinkedIngredients(Model $entity): ?array
{
$ingredientRepo = new IngredientRepository();
$response = $ingredientRepo->getIdLinkedTo( 'recettes', 'num_recette', $entity );
return array_map( function($arr) use($ingredientRepo) {
return $ingredientRepo->getByID( $arr['num_ingredient'] );
}, $response );
}
public function addAnIngredient(Ingredient $ingredient, Model $entity): bool
{
$ingredientRepo = new IngredientRepository();
return $ingredientRepo->addLinkBetween( 'recettes', 'num_recette', $ingredient, $entity );
}
public function removeAnIngredient(Ingredient $ingredient, Model $entity): bool
{
$ingredientRepo = new IngredientRepository();
return $ingredientRepo->removeLinkBetween( 'recettes', 'num_recette', $ingredient, $entity );
}
}

View File

@@ -21,34 +21,63 @@ abstract class Repository {
*/
abstract public static function getStructure(): array;
/**
* Contient le nom de la table principale du repo.
* @var string|mixed
*/
final public string $tableName;
/**
* Contient le nom des colonnes de la table du repo.
* @var array|mixed
*/
final public array $tableColumns;
/**
* Contient les mêmes données que getStructure().
* @var array
*/
public private(set) array $globalStructure;
/**
* Constructeur.
* Reprend les informations de getStructure et les met dans des attributs.
*/
public function __construct(){
$structure = static::getStructure();
$this->tableName = $structure['table'];
$this->tableColumns = $structure['columns'];
$this->globalStructure = $structure;
}
/**
* Permet d'avoir tous les éléments correspondant à la requête passée en paramètre.
*
* @param string $sqlQuery
* @param bool $asArray Permet de savoir si on veut que le retour soit une array ou bien un tableau de Model.
* @return array|null
*/
public function selectGetAll( string $sqlQuery ): ?array {
public function selectGetAll( string $sqlQuery, bool $asArray = false ): ?array {
$statement = Kernel::$DB->pdo->prepare( $sqlQuery );
if( !$statement->execute() )
return null;
$results = $statement->fetchAll( PDO::FETCH_CLASS, static::getEntity() );
if( $asArray )
$results = $statement->fetchAll( PDO::FETCH_ASSOC );
else
$results = $statement->fetchAll( PDO::FETCH_CLASS, static::getEntity() );
if( empty( $results ) )
return null;
return $results;
}
/**
* Permet d'ajouter une entité à la base de données.
*
* @param Model $entity
* @return bool
*/
public function addEntity( Model $entity ): bool {
$query = "INSERT INTO {$this->tableName} (";
@@ -69,6 +98,14 @@ abstract class Repository {
return $statement->execute();
}
/**
* Permet de mettre à jour les informations de l'entité dans la base de données.
*
* @param Model $entity
* @param string $identifier
*
* @return bool
*/
public function updateEntity( Model $entity, string $identifier ): bool {
$query = "UPDATE {$this->tableName} SET ";
foreach( $this->tableColumns as $column ) {
@@ -84,6 +121,14 @@ abstract class Repository {
return $statement->execute();
}
/**
* Permet de supprimer une entrée d'entité dans la base de données.
*
* @param Model $entity
* @param string $identifier
*
* @return bool
*/
public function deleteEntity( Model $entity, string $identifier ): bool {
$query = "DELETE FROM {$this->tableName} WHERE {$identifier} = :{$identifier};";
$statement = Kernel::$DB->pdo->prepare( $query );
@@ -91,8 +136,31 @@ abstract class Repository {
return $statement->execute();
}
/**
* Fonction raccourcie pour préparer les champs nécessaires pour addEntity.
*
* @param Model $entity
*
* @return bool
*/
abstract public function add( Model $entity ): bool;
/**
* Fonction raccourcie pour préparer les champs nécessaires pour updateEntity
*
* @param Model $entity
*
* @return bool
*/
abstract public function update( Model $entity ): bool;
/**
* Fonction raccourcie pour préparer les champs nécessaires pour deleteEntity
*
* @param Model $entity
*
* @return bool
*/
abstract public function delete( Model $entity ): bool;