Finish Advanced Search

This commit is contained in:
2026-04-03 10:43:55 +02:00
parent f918134469
commit f2397b6d31
8 changed files with 187 additions and 18 deletions

View File

@@ -97,6 +97,40 @@ class RecetteRepository extends Repository implements UseIngredientsInterface, U
return $results[0];
}
public function advancedRecetteSearch( string $title = "", array $tagsId= [], array $ingredientsId= [] ): ?array {
$tableLinkIngredients = IngredientRepository::getStructure()['link_recettes'];
$tableLinkTags = TagRepository::getStructure()['link_recettes'];
$sqlQuery = "SELECT ({$this->tableName}.num_recette) FROM {$this->tableName} LEFT JOIN {$tableLinkIngredients} ON {$this->tableName}.num_recette = {$tableLinkIngredients}.num_recette LEFT JOIN {$tableLinkTags} ON {$this->tableName}.num_recette = {$tableLinkTags}.num_recette";
if( $title != "" || $tagsId !== [] || $ingredientsId !== [] ) {
$sqlQuery .= " WHERE ";
if ($title != "")
$sqlQuery .= " titre_recette LIKE '%{$title}%' AND";
if ($tagsId !== [])
$sqlQuery .= " num_tag IN (" . implode(",", $tagsId) . ") AND";
if ($ingredientsId !== [])
$sqlQuery .= " num_ingredient IN (" . implode(",", $ingredientsId) . ") AND";
$sqlQuery = substr($sqlQuery, 0, -3);
}
$sqlQuery .= ";";
$results = $this->selectGetAll($sqlQuery, true);
if( $results === null )
return null;
$alreadyGettedId = [];
for( $i = 0; $i < count( $results ); $i++ ){
if( in_array( $results[$i]['num_recette'], $alreadyGettedId ) )
unset( $results[$i] );
else
$alreadyGettedId[] = $results[$i]['num_recette'];
}
return $results;
}
public function add( Model $recette ): bool {
return $this->addEntity( $recette );
}

View File

@@ -3,6 +3,8 @@
namespace App\Domain\Recettes;
use App\Domain\Controller;
use App\Http\JSONResponse;
use App\Http\Request;
class RecettesAPIController extends Controller {
@@ -13,4 +15,29 @@ class RecettesAPIController extends Controller {
];
}
public function list(){
$title = Request::post( 'title' ) ?? "";
$tagsId = explode( ",", Request::post( 'tagsId' ) ) ?? [];
$ingredientsId = explode( ",", Request::post( 'ingredientsId' ) ) ?? [];
if( $tagsId == [ "" ] )
$tagsId = [];
if( $ingredientsId == [ "" ] )
$ingredientsId = [];
$tagsId = array_map( 'intval', $tagsId );
$ingredientsId = array_map( 'intval', $ingredientsId );
$recetteRepo = new RecetteRepository();
$resp = $recetteRepo->advancedRecetteSearch( $title, $tagsId, $ingredientsId );
$resp = array_map( function($recette) use ($recetteRepo){
return $recetteRepo->getByID( $recette['num_recette'] );
}, $resp ?? [] );
JSONResponse::sendSuccess( [ 'data' => $resp ] );
}
}