Finish tag et commencer à ajouter les template.s

This commit is contained in:
2026-04-02 19:07:08 +02:00
parent 8a173ed2c7
commit 69a5a99e15
19 changed files with 620 additions and 34 deletions

View File

@@ -2,11 +2,15 @@
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\Helpers\Markdown;
/**
* Classe qui va permettre de gérer tous les objets recette.
*/
class Recette extends Model {
public int $num_recette;
@@ -22,13 +26,27 @@ class Recette extends Model {
return $this->num_recette;
}
/**
* Convertit la description de Markdown à HTML.
* @return string
*/
public function getHTMLDescription(): string {
return Markdown::convertToHTML( $this->description_recette );
}
/**
* Récupère une liste de tous les ingrédients liés à la recette.
* @return Ingredient[]|null
*/
public function getAllLinkedIngredients(): ?array
{
return new RecetteRepository()->getAllLinkedIngredients( $this );
}
public function getNumberOfIngredients(): int {
$response = $this->getAllLinkedIngredients();
return $response !== null ? count( $response ) : 0;
}
}

View File

@@ -7,8 +7,15 @@ use App\Domain\Ingredients\IngredientRepository;
use App\Domain\Ingredients\UseIngredientsInterface;
use App\Domain\Model;
use App\Domain\Repository;
use App\Domain\Tags\Tag;
use App\Domain\Tags\TagRepository;
use App\Domain\Tags\UseTagsInterface;
class RecetteRepository extends Repository implements UseIngredientsInterface {
/**
* Classe qui permet de faire le lien entre la BDD et le site pour les recettes.
* Les recettes sont en lien avec les ingrédients.
*/
class RecetteRepository extends Repository implements UseIngredientsInterface, UseTagsInterface {
public static function getEntity(): string
{
@@ -26,6 +33,42 @@ class RecetteRepository extends Repository implements UseIngredientsInterface {
}
/**
* Permet d'obtenir une liste de toutes les recettes objet Recette.
*
* @return Recette[]|null
*/
public function getAll(): ?array {
$sqlQuery = "SELECT * FROM {$this->tableName};";
$results = $this->selectGetAll($sqlQuery);
if( $results === null )
return null;
return $results;
}
/**
* Permet d'obtenir toutes les recettes paginées.
* Mise par défaut dans l'ordre croissant du titre et avec une pagination de 15 éléments.
*
* @param int $page
* @param int $pagination
*
* @return array|null
*/
public function getAllRecettesBrowse( int $page = 1, int $pagination = 15 ){
if( $page <= 0 )
$page = 1;
$offset = ( $page - 1 ) * $pagination;
$sqlQuery = "SELECT * FROM {$this->tableName} ORDER BY titre_recette ASC LIMIT {$pagination} OFFSET {$offset};";
$results = $this->selectGetAll($sqlQuery);
if( $results === null )
return null;
return $results;
}
/**
* Permet d'avoir une recette par un ID.
*
@@ -72,6 +115,8 @@ class RecetteRepository extends Repository implements UseIngredientsInterface {
{
$ingredientRepo = new IngredientRepository();
$response = $ingredientRepo->getIdLinkedTo( 'recettes', 'num_recette', $entity );
if( $response === null )
return null;
return array_map( function($arr) use($ingredientRepo) {
return $ingredientRepo->getByID( $arr['num_ingredient'] );
@@ -81,13 +126,37 @@ class RecetteRepository extends Repository implements UseIngredientsInterface {
public function addAnIngredient(Ingredient $ingredient, Model $entity): bool
{
$ingredientRepo = new IngredientRepository();
return $ingredientRepo->addLinkBetween( 'recettes', 'num_recette', $ingredient, $entity );
return $ingredientRepo->addLinkBetween( 'recettes', 'num_recette', $entity, $ingredient );
}
public function removeAnIngredient(Ingredient $ingredient, Model $entity): bool
{
$ingredientRepo = new IngredientRepository();
return $ingredientRepo->removeLinkBetween( 'recettes', 'num_recette', $ingredient, $entity );
return $ingredientRepo->removeLinkBetween( 'recettes', 'num_recette', $entity, $ingredient );
}
public function getAllLinkedTags(Model $entity): ?array
{
$tagRepo = new TagRepository();
$response = $tagRepo->getIdLinkedTo( 'recettes', 'num_recette', $entity );
if( $response === null )
return null;
return array_map( function($arr) use($tagRepo) {
return $tagRepo->getByID( $arr['num_tag'] );
}, $response );
}
public function addATag(Tag $tag, Model $entity): bool
{
$tagRepo = new TagRepository();
return $tagRepo->addLinkBetween( 'recettes', 'num_recette', $entity, $tag );
}
public function removeATag(Tag $tag, Model $entity): bool
{
$tagRepo = new TagRepository();
return $tagRepo->removeLinkBetween( 'recettes', 'num_recette', $entity, $tag );
}
}

View File

@@ -3,7 +3,10 @@
namespace App\Domain\Recettes;
use App\Domain\Controller;
use App\Domain\Ingredients\IngredientRepository;
use App\Domain\Tags\TagRepository;
use App\Http\JSONResponse;
use App\Http\Request;
use App\Infrastructure\View;
class RecettesController extends Controller {
@@ -18,7 +21,16 @@ class RecettesController extends Controller {
}
public function index(): View {
return new View( 'recettes/index', [] );
$page = Request::get( 'page' );
if( $page == null )
$page = 1;
return new View( 'recettes/index', [
'tagsList' => new TagRepository()->getAll(),
'ingredientsList' => new IngredientRepository()->getAll(),
'recettesList' => new RecetteRepository()->getAllRecettesBrowse( $page ),
] );
}
}