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

@@ -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' );
}
}