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

@@ -3,7 +3,7 @@
return [ return [
'host' => 'localhost', 'host' => 'localhost',
'port' => 3306, 'port' => 3306,
'user' => 'benjamin', 'user' => 'root',
'pass' => '011235813', 'pass' => '',
'name' => 'siterecette' 'name' => 'siterecette'
]; ];

View File

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

View File

@@ -2,10 +2,13 @@
namespace App\Domain\Ingredients; namespace App\Domain\Ingredients;
use App\Domain\LinkableInterface;
use App\Domain\Recettes\Recette;
use App\Domain\Repository; use App\Domain\Repository;
use App\Domain\Model; use App\Domain\Model;
use App\Kernel;
class IngredientRepository extends Repository { class IngredientRepository extends Repository implements LinkableInterface {
public static function getEntity(): string public static function getEntity(): string
{ {
@@ -18,12 +21,83 @@ class IngredientRepository extends Repository {
'table' => 'Ingredient', 'table' => 'Ingredient',
'columns' => [ 'columns' => [
'num_ingredient', 'nom_ingredient' '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 { 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; namespace App\Domain\Recettes;
use App\Domain\Ingredients\IngredientRepository;
use App\Domain\Ingredients\UseIngredientsInterface;
use App\Domain\Model; use App\Domain\Model;
use App\Helpers\Markdown; use App\Helpers\Markdown;
@@ -15,8 +17,18 @@ class Recette extends Model {
public string $publication_date; public string $publication_date;
public int $temps_de_preparation; public int $temps_de_preparation;
public function getID(): int
{
return $this->num_recette;
}
public function getHTMLDescription(): string { public function getHTMLDescription(): string {
return Markdown::convertToHTML( $this->description_recette ); 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; 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\Model;
use App\Domain\Repository; use App\Domain\Repository;
class RecetteRepository extends Repository { class RecetteRepository extends Repository implements UseIngredientsInterface {
public static function getEntity(): string public static function getEntity(): string
{ {
@@ -63,4 +66,28 @@ class RecetteRepository extends Repository {
return $this->deleteEntity( $recette, 'num_recette' ); 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; abstract public static function getStructure(): array;
/**
* Contient le nom de la table principale du repo.
* @var string|mixed
*/
final public string $tableName; final public string $tableName;
/**
* Contient le nom des colonnes de la table du repo.
* @var array|mixed
*/
final public array $tableColumns; 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(){ public function __construct(){
$structure = static::getStructure(); $structure = static::getStructure();
$this->tableName = $structure['table']; $this->tableName = $structure['table'];
$this->tableColumns = $structure['columns']; $this->tableColumns = $structure['columns'];
$this->globalStructure = $structure;
} }
/** /**
* Permet d'avoir tous les éléments correspondant à la requête passée en paramètre. * Permet d'avoir tous les éléments correspondant à la requête passée en paramètre.
* *
* @param string $sqlQuery * @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 * @return array|null
*/ */
public function selectGetAll( string $sqlQuery ): ?array { public function selectGetAll( string $sqlQuery, bool $asArray = false ): ?array {
$statement = Kernel::$DB->pdo->prepare( $sqlQuery ); $statement = Kernel::$DB->pdo->prepare( $sqlQuery );
if( !$statement->execute() ) if( !$statement->execute() )
return null; return null;
if( $asArray )
$results = $statement->fetchAll( PDO::FETCH_ASSOC );
else
$results = $statement->fetchAll( PDO::FETCH_CLASS, static::getEntity() ); $results = $statement->fetchAll( PDO::FETCH_CLASS, static::getEntity() );
if( empty( $results ) ) if( empty( $results ) )
return null; return null;
return $results; return $results;
} }
/**
* Permet d'ajouter une entité à la base de données.
*
* @param Model $entity
* @return bool
*/
public function addEntity( Model $entity ): bool { public function addEntity( Model $entity ): bool {
$query = "INSERT INTO {$this->tableName} ("; $query = "INSERT INTO {$this->tableName} (";
@@ -69,6 +98,14 @@ abstract class Repository {
return $statement->execute(); 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 { public function updateEntity( Model $entity, string $identifier ): bool {
$query = "UPDATE {$this->tableName} SET "; $query = "UPDATE {$this->tableName} SET ";
foreach( $this->tableColumns as $column ) { foreach( $this->tableColumns as $column ) {
@@ -84,6 +121,14 @@ abstract class Repository {
return $statement->execute(); 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 { public function deleteEntity( Model $entity, string $identifier ): bool {
$query = "DELETE FROM {$this->tableName} WHERE {$identifier} = :{$identifier};"; $query = "DELETE FROM {$this->tableName} WHERE {$identifier} = :{$identifier};";
$statement = Kernel::$DB->pdo->prepare( $query ); $statement = Kernel::$DB->pdo->prepare( $query );
@@ -91,8 +136,31 @@ abstract class Repository {
return $statement->execute(); 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; 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; 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; abstract public function delete( Model $entity ): bool;

View File

@@ -1,9 +1,19 @@
<h1>Coucou</h1> <h1>Coucou</h1>
<?php $str = "Markdown **text**\n# SOLONG\n*text*."; $md = \App\Helpers\Markdown::convertToHTML( $str ); echo "<p>$md</p>"; ?> <?php $str = "Markdown **text**\n# SOLONG\n*text*."; $md = \App\Helpers\Markdown::convertToHTML( $str ); echo "<p>$md</p>"; ?>
<?php $rec = new \App\Domain\Recettes\RecetteRepository()->getByID( 4 ); <?php
$rec->temps_de_preparation = 7; /*
$rec->titre_recette = "Tutu"; $ing = new \App\Domain\Ingredients\Ingredient();
$ing->num_ingredient = 0;
$ing->nom_ingredient = "coucou";
new \App\Domain\Ingredients\IngredientRepository()->add($ing);
*/
new \App\Domain\Recettes\RecetteRepository()->delete( $rec ); $recette = new \App\Domain\Recettes\RecetteRepository()->getByID( 2 );
$ingredient = new \App\Domain\Ingredients\IngredientRepository()->getByID( 2 );
// var_dump( $recette );
// var_dump( $ingredient );
// new \App\Domain\Recettes\RecetteRepository()->removeAnIngredient( $ingredient, $recette );
var_dump( $recette->getAllLinkedIngredients() );
?> ?>