Files
LesRecettesDePapis/src/Domain/Recettes/RecettesAPIController.php

294 lines
10 KiB
PHP
Raw Normal View History

2026-03-20 15:54:29 +01:00
<?php
namespace App\Domain\Recettes;
use App\Domain\Controller;
2026-04-03 12:55:05 +02:00
use App\Domain\Ingredients\IngredientRepository;
use App\Domain\Tags\TagRepository;
use App\Helpers\UploadFiles;
2026-04-03 10:43:55 +02:00
use App\Http\JSONResponse;
use App\Http\Request;
2026-04-03 12:55:05 +02:00
use App\Http\Router;
2026-03-20 15:54:29 +01:00
class RecettesAPIController extends Controller {
public static function defineRoutes(): array
{
return [
self::Route( routeUrl: '/api/recettes/list', routeName: 'api->recettes->list', routeAction: 'list', routeMethods: ['POST'] ),
2026-04-03 12:55:05 +02:00
self::Route( routeUrl: '/api/recettes/create', routeName: 'api->recettes->create', routeAction: 'create', routeMethods: ['POST'] ),
self::Route( routeUrl: '/api/recettes/edit', routeName: 'api->recettes->edit', routeAction: 'edit', routeMethods: ['POST'] ),
self::Route( routeUrl: '/api/recettes/delete', routeName: 'api->recettes->delete', routeAction: 'delete', routeMethods: ['POST'] ),
2026-03-20 15:54:29 +01:00
];
}
2026-04-03 10:43:55 +02:00
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){
2026-04-03 12:55:05 +02:00
$r = $recetteRepo->getByID( $recette['num_recette'] );
$r->url = Router::getRouteURL( 'recettes->show', $r->slug );
$r->nb_ingredients = $r->getNumberOfIngredients();
return $r;
2026-04-03 10:43:55 +02:00
}, $resp ?? [] );
JSONResponse::sendSuccess( [ 'data' => $resp ] );
}
2026-04-03 12:55:05 +02:00
public function create(){
// Récupération des champs.
$name = Request::post( 'nom' ) ?? "";
$temps = Request::post( 'temps' ) ?? "";
$fileField = "image";
$tagsId = explode( ",", Request::post( 'tag' ) ) ?? [];
$ingredientsId = explode( ",", Request::post( 'ingr' ) ) ?? [];
if( $tagsId == [ "" ] )
$tagsId = [];
if( $ingredientsId == [ "" ] )
$ingredientsId = [];
$tagsId = array_map( 'intval', $tagsId );
$ingredientsId = array_map( 'intval', $ingredientsId );
$description = Request::post( 'description' );
// Vérification.
if( $name == "" || $temps == "" || $ingredientsId == [] || $description == "" )
JSONResponse::sendError( [ 'error' => "One required fields is missing" ] );
// Upload & Vérification de l'image.
$urlOrError = UploadFiles::uploadFile( $fileField );
if( is_int( $urlOrError ) ){
JSONResponse::sendError( [ 'error' => $urlOrError ] );
}
// Vérification d'une entrée qui existerait déjà.
$slug = strtolower( $name );
$slug = str_replace( ' ', '-', $slug );
$recetteRepo = new RecetteRepository();
if( $recetteRepo->getBySlug( $slug ) ){
JSONResponse::sendError( [ 'error' => "Recette already exists" ] );
}
// Création de la recette.
$recette = new Recette();
$recette->num_recette = 0;
$recette->titre_recette = $name;
$recette->slug = $slug;
$recette->temps_de_preparation = intval( $temps );
$recette->photo = $urlOrError;
$recette->description_recette = $description;
$recette->publication_date = date("Y-m-d");
// Importation de la recette.
if( !$recetteRepo->add( $recette ) ){
JSONResponse::sendError( [ 'error' => "Error adding recette" ] );
}
// Pour actualiser l'ID.
$recette = $recetteRepo->getBySlug( $slug );
// Ajout du lien ingrédients recettes.
foreach( $ingredientsId as $ingredientId ){
$ingredient = new IngredientRepository()->getByID( $ingredientId );
if( $ingredient == null )
continue;
$recetteRepo->addAnIngredient( $ingredient, $recette );
}
// Ajout du lien Recettes tags.
if( $tagsId != [] ){
foreach( $tagsId as $tagId ){
$tag = new TagRepository()->getByID( $tagId );
if( $tag == null )
continue;
$recetteRepo->addATag( $tag, $recette );
}
}
JSONResponse::sendSuccess( [ 'recette' => $recette ] );
}
public function edit(){
// Récupération des champs.
$id = Request::post( 'id' );
$name = Request::post( 'nom' ) ?? "";
$temps = Request::post( 'temps' ) ?? "";
$fileField = "image";
$tagsId = explode( ",", Request::post( 'tag' ) ) ?? [];
$ingredientsId = explode( ",", Request::post( 'ingr' ) ) ?? [];
if( $tagsId == [ "" ] )
$tagsId = [];
if( $ingredientsId == [ "" ] )
$ingredientsId = [];
$tagsId = array_map( 'intval', $tagsId );
$ingredientsId = array_map( 'intval', $ingredientsId );
$description = Request::post( 'description' );
// Vérification.
if( $id == "" || $name == "" || $temps == "" || $ingredientsId == [] || $description == "" )
JSONResponse::sendError( [ 'error' => "One required fields is missing" ] );
$recetteRepo = new RecetteRepository();
$recette = $recetteRepo->getByID( $id );
if( !$recette ){
JSONResponse::sendError( [ 'error' => "Recette not found" ] );
}
// Upload & Vérification de l'image.
$urlOrError = UploadFiles::uploadFile( $fileField );
if( is_int( $urlOrError ) ){
// Ingore image.
} else {
$recette->photo = $urlOrError;
}
// Vérification d'une entrée qui existerait déjà.
$slug = strtolower( $name );
$slug = str_replace( ' ', '-', $slug );
$recette->titre_recette = $name;
$recette->slug = $slug;
$recette->temps_de_preparation = intval( $temps );
$recette->description_recette = $description;
// Importation de la recette.
if( !$recetteRepo->update( $recette ) ){
JSONResponse::sendError( [ 'error' => "Error updating recette" ] );
}
// Actualisation des ingrédients.
$ingrObjArray = $recette->getAllLinkedIngredients();
if( !$ingrObjArray || $ingrObjArray === [] ){ // Si il n'y a pas déjà d'autres ingrédients, on ajoute tous ceux présents ici.
foreach( $ingredientsId as $ingredientId ){
$ingredient = new IngredientRepository()->getByID( $ingredientId );
if( $ingredient == null )
continue;
$recetteRepo->addAnIngredient( $ingredient, $recette );
}
} else {
$ingrIntArray = array_map( function($ingr){
return intval( $ingr->getID() );
}, $ingrObjArray );
foreach( $ingrIntArray as $ingrId ){
// Si un ingrédient n'est pas dans la nouvelle liste d'ingrédients, il faut retirer le lien.
if( !in_array( $ingrId, $ingredientsId ) ){
$ingredient = new IngredientRepository()->getByID( $ingrId );
if( $ingredient == null )
continue;
$recetteRepo->removeAnIngredient( $ingredient, $recette );
}
}
foreach( $ingredientsId as $ingrId ){
// Si un ingrédient n'est pas dans l'ancienne liste, il faut ajouter un lien.
if( !in_array( $ingrId, $ingrIntArray ) ){
$ingredient = new IngredientRepository()->getByID( $ingrId );
if( $ingredient == null )
continue;
$recetteRepo->addAnIngredient( $ingredient, $recette );
}
}
}
// Actualisation des tags.
if( $tagsId != [] ) {
$tagObjArray = $recette->getAllLinkedTags();
if (!$tagObjArray || $tagObjArray === []) { // Si il n'y a pas déjà d'autres tags, on ajoute tous ceux présents ici.
foreach ($tagsId as $tagId) {
$tag = new TagRepository()->getByID($tagId);
if ($tag == null)
continue;
$recetteRepo->addATag($tag, $recette);
}
} else {
$tagIntArray = array_map(function ($ingr) {
return intval( $ingr->getID() );
}, $tagObjArray);
foreach ($tagIntArray as $tagId) {
// Si un ingrédient n'est pas dans la nouvelle liste d'ingrédients, il faut retirer le lien.
if (!in_array($tagId, $tagsId)) {
$tag = new TagRepository()->getByID($tagId);
if ($tag == null)
continue;
$recetteRepo->removeATag($tag, $recette);
}
}
foreach ($tagsId as $tagId) {
// Si un ingrédient n'est pas dans l'ancienne liste, il faut ajouter un lien.
if (!in_array($tagId, $tagIntArray)) {
$tag = new TagRepository()->getByID($tagId);
if ($tag == null)
continue;
$recetteRepo->addATag($tag, $recette);
}
}
}
}
JSONResponse::sendSuccess( [ 'recette' => $recette ] );
}
public function delete(){
$id = Request::post( 'id' );
$recette = new RecetteRepository()->getByID( $id );
if( !$recette )
JSONResponse::sendError( [ 'error' => 'Recette not found' ] );
$recetteRepo = new RecetteRepository();
// Retirer tous les liens Ingrédients/Tags.
foreach( ( $recette->getAllLinkedIngredients() ?? [] ) as $ingr ){
$recetteRepo->removeAnIngredient( $ingr, $recette );
}
foreach( ( $recette->getAllLinkedTags() ?? [] ) as $tag ){
$recetteRepo->removeATag( $tag, $recette );
}
if( !$recetteRepo->delete( $recette ) )
JSONResponse::sendError( [ 'error' => 'An error occured while deleting recette' ] );
JSONResponse::sendSuccess();
}
2026-03-20 15:54:29 +01:00
}