diff --git a/config/database.php b/config/database.php index 334dc3f..05bbd80 100644 --- a/config/database.php +++ b/config/database.php @@ -3,7 +3,7 @@ return [ 'host' => 'localhost', 'port' => 3306, - 'user' => 'benjamin', - 'pass' => '011235813', + 'user' => 'root', + 'pass' => '', 'name' => 'siterecette' ]; \ No newline at end of file diff --git a/src/Domain/Ingredients/Ingredient.php b/src/Domain/Ingredients/Ingredient.php index bfab46f..9fb655e 100644 --- a/src/Domain/Ingredients/Ingredient.php +++ b/src/Domain/Ingredients/Ingredient.php @@ -9,5 +9,10 @@ class Ingredient extends Model { public int $num_ingredient; public string $nom_ingredient; + public function getID(): int + { + return $this->num_ingredient; + } + } \ No newline at end of file diff --git a/src/Domain/Ingredients/IngredientRepository.php b/src/Domain/Ingredients/IngredientRepository.php index 63efb45..b0f2376 100644 --- a/src/Domain/Ingredients/IngredientRepository.php +++ b/src/Domain/Ingredients/IngredientRepository.php @@ -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' ); + } + } \ No newline at end of file diff --git a/src/Domain/Ingredients/UseIngredientsInterface.php b/src/Domain/Ingredients/UseIngredientsInterface.php new file mode 100644 index 0000000..d0163d6 --- /dev/null +++ b/src/Domain/Ingredients/UseIngredientsInterface.php @@ -0,0 +1,19 @@ +num_recette; + } + public function getHTMLDescription(): string { return Markdown::convertToHTML( $this->description_recette ); } + public function getAllLinkedIngredients(): ?array + { + return new RecetteRepository()->getAllLinkedIngredients( $this ); + } + } \ No newline at end of file diff --git a/src/Domain/Recettes/RecetteRepository.php b/src/Domain/Recettes/RecetteRepository.php index 53bc4a2..10692ba 100644 --- a/src/Domain/Recettes/RecetteRepository.php +++ b/src/Domain/Recettes/RecetteRepository.php @@ -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 ); + } + } \ No newline at end of file diff --git a/src/Domain/Repository.php b/src/Domain/Repository.php index b1933c3..2de7a9a 100644 --- a/src/Domain/Repository.php +++ b/src/Domain/Repository.php @@ -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; diff --git a/views/home.php b/views/home.php index c0c0f44..325bbea 100644 --- a/views/home.php +++ b/views/home.php @@ -1,9 +1,19 @@