Files
LesRecettesDePapis/src/Domain/Ingredients/IngredientManagementController.php
2026-04-03 15:27:51 +02:00

70 lines
2.2 KiB
PHP

<?php
namespace App\Domain\Ingredients;
use App\Domain\Controller;
use App\Domain\Ingredients\IngredientRepository;
use App\Domain\Tags\TagRepository;
use App\Helpers\Authentification;
use App\Http\JSONResponse;
use App\Http\Request;
use App\Infrastructure\View;
class IngredientManagementController extends Controller {
public static function defineRoutes(): array
{
return [
self::Route( routeUrl: '/manage/ingredients', routeName: 'manage->ingredients', routeAction: 'index' ),
self::Route( routeUrl: '/ingredients/edit/{int}', routeName: 'ingredients->edit', routeAction: 'edit', routeMethods: [ 'POST' ] ),
self::Route( routeUrl: '/ingredients/delete/{int}', routeName: 'ingredients->delete', routeAction: 'delete' ),
];
}
public function index(){
if( !Authentification::isLoggedIn() )
die( "Not logged in" );
$tags = new IngredientRepository()->getAll();
return new View( 'ingredients/index', [
'ingredients' => $tags,
]);
}
public function edit( int $idIngredient ): never {
if( !Authentification::isLoggedIn() )
die( "Not logged in" );
$ing = new IngredientRepository()->getByID( $idIngredient );
if( !$ing )
die( "Ingredient not found" );
$name = Request::post( 'modif_ingr' );
if( !$name || $name == "" )
JSONResponse::sendError( [ 'error' => 'Name not defined' ] );
$ing->nom_ingredient = $name;
if( !new IngredientRepository()->update( $ing ) )
JSONResponse::sendError( [ 'error' => 'An error occured while updating ingredient' ] );
Request::redirectTo( 'manage->ingredients');
}
public function delete( int $idIngredient ): never {
if( !Authentification::isLoggedIn() )
die( "Not logged in" );
$ing = new IngredientRepository()->getByID( $idIngredient );
if( !$ing )
die( "Tag not found" );
if( !new IngredientRepository()->delete( $ing ) )
JSONResponse::sendError( [ 'error' => 'An error occured while deleting ingredient' ] );
Request::redirectTo( 'manage->ingredients' );
}
}