2026-07-06 12:52:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Back;
|
|
|
|
|
|
2026-11-05 11:17:20 +01:00
|
|
|
use App\Contracts\Executor;
|
|
|
|
|
use App\Contracts\QueryInputEmpty;
|
2026-11-05 11:54:19 +01:00
|
|
|
use App\Cqs\Actions\Recipes\Create\CreateRecipe;
|
|
|
|
|
use App\Cqs\Actions\Recipes\Delete\DeleteRecipe;
|
|
|
|
|
use App\Cqs\Actions\Recipes\Delete\DeleteRecipeInput;
|
2026-11-05 17:46:14 +01:00
|
|
|
use App\Cqs\Actions\Recipes\Duplicate\DuplicateRecipe;
|
|
|
|
|
use App\Cqs\Actions\Recipes\Duplicate\DuplicateRecipeInput;
|
2026-11-05 11:54:19 +01:00
|
|
|
use App\Cqs\Actions\Recipes\ForceDelete\ForceDeleteRecipe;
|
|
|
|
|
use App\Cqs\Actions\Recipes\ForceDelete\ForceDeleteRecipeInput;
|
|
|
|
|
use App\Cqs\Actions\Recipes\Restore\RestoreRecipe;
|
|
|
|
|
use App\Cqs\Actions\Recipes\Restore\RestoreRecipeInput;
|
|
|
|
|
use App\Cqs\Actions\Recipes\Update\UpdateRecipe;
|
2026-11-05 11:17:20 +01:00
|
|
|
use App\Cqs\Queries\Products\GetProductsSummariesQuery;
|
2026-11-05 16:49:42 +01:00
|
|
|
use App\Cqs\Queries\Products\GetRelatedProductsQuery;
|
|
|
|
|
use App\Cqs\Queries\Products\GetRelatedProductsQueryInput;
|
2026-11-05 12:17:58 +01:00
|
|
|
use App\Cqs\Queries\Recipes\GetBoPaginatedRecipesQuery;
|
2026-11-05 16:49:42 +01:00
|
|
|
use App\Dto\ProductDto;
|
2026-11-05 11:54:19 +01:00
|
|
|
use App\Dto\RecipeDto;
|
2026-07-06 12:52:52 +02:00
|
|
|
use App\Http\Requests\Back\Recipes\IndexRecipeRequest;
|
|
|
|
|
use App\Http\Requests\Back\Recipes\StoreRecipeRequest;
|
|
|
|
|
use App\Http\Requests\Back\Recipes\UpdateRecipeRequest;
|
2026-07-08 18:16:46 +02:00
|
|
|
use App\Http\Resources\CommonResource;
|
2026-07-06 12:52:52 +02:00
|
|
|
use App\Models\Recipe;
|
2026-11-05 11:54:19 +01:00
|
|
|
use Domain\Models\ValueObjects\Entities\RecipeId;
|
2026-07-08 18:16:46 +02:00
|
|
|
use Domain\Models\ValueObjects\Products\ProductCategory;
|
|
|
|
|
use Domain\Models\ValueObjects\Recipes\ConsumptionTime;
|
|
|
|
|
use Domain\Models\ValueObjects\Recipes\Inspiration;
|
|
|
|
|
use Domain\Models\ValueObjects\Recipes\Season;
|
2026-07-06 12:52:52 +02:00
|
|
|
use Illuminate\Contracts\Session\Session;
|
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Inertia\Response;
|
|
|
|
|
|
|
|
|
|
class RecipeController extends BackendController
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
Request $request,
|
|
|
|
|
Session $session,
|
2026-11-05 11:17:20 +01:00
|
|
|
private readonly Executor $executor
|
2026-07-06 12:52:52 +02:00
|
|
|
) {
|
|
|
|
|
parent::__construct(
|
|
|
|
|
$request,
|
|
|
|
|
$session
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->modelName = Recipe::modelName();
|
|
|
|
|
$this->modelPage = Recipe::modelPage();
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-05 12:17:58 +01:00
|
|
|
public function index(IndexRecipeRequest $request, GetBoPaginatedRecipesQuery $query): Response
|
2026-07-06 12:52:52 +02:00
|
|
|
{
|
2026-07-08 18:16:46 +02:00
|
|
|
$this->authorize('viewAny', Recipe::class);
|
|
|
|
|
|
2026-11-05 12:17:58 +01:00
|
|
|
$output = $this->executor->execute($query, $request->toGetBoPaginatedRecipesQueryInput());
|
2026-07-06 12:52:52 +02:00
|
|
|
|
2026-07-08 18:16:46 +02:00
|
|
|
return $this->inertiaRender('back/Pages/Recipes/Index', [
|
2026-11-05 12:17:58 +01:00
|
|
|
'paginatedModels' => CommonResource::collection($output->paginatedRecipes),
|
|
|
|
|
'search' => $output->search,
|
|
|
|
|
'searchFields' => $output->searchFields,
|
|
|
|
|
'enumProductCategory' => $output->categories,
|
|
|
|
|
'enumConsumptionTime' => $output->consumptionTimes,
|
|
|
|
|
'enumInspiration' => $output->inspirations,
|
|
|
|
|
'enumSeason' => $output->seasons,
|
2026-07-06 12:52:52 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-05 16:49:42 +01:00
|
|
|
public function show(RecipeDto $recipe): RedirectResponse
|
2026-07-06 12:52:52 +02:00
|
|
|
{
|
2026-07-08 18:16:46 +02:00
|
|
|
$this->authorize('viewAny', Recipe::class);
|
|
|
|
|
|
|
|
|
|
return redirect()->route('back.recipes.edit', $recipe);
|
2026-07-06 12:52:52 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-05 11:17:20 +01:00
|
|
|
public function create(Recipe $recipe, GetProductsSummariesQuery $getProductsSummaries): Response
|
2026-07-06 12:52:52 +02:00
|
|
|
{
|
2026-07-08 18:16:46 +02:00
|
|
|
$this->authorize('create', Recipe::class);
|
|
|
|
|
|
2026-11-05 11:17:20 +01:00
|
|
|
$productSummaries = $this->executor->execute($getProductsSummaries, QueryInputEmpty::create());
|
|
|
|
|
|
2026-07-08 18:16:46 +02:00
|
|
|
return $this->inertiaRender('back/Pages/Recipes/Create', [
|
|
|
|
|
'data' => CommonResource::make($recipe),
|
|
|
|
|
'enumProductCategory' => ProductCategory::toArray(),
|
|
|
|
|
'enumConsumptionTime' => ConsumptionTime::toArray(),
|
|
|
|
|
'enumInspiration' => Inspiration::toArray(),
|
|
|
|
|
'enumSeason' => Season::toArray(),
|
2026-11-05 11:17:20 +01:00
|
|
|
'allProducts' => $productSummaries->toArray(),
|
2026-07-06 12:52:52 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-05 11:54:19 +01:00
|
|
|
public function store(StoreRecipeRequest $request, CreateRecipe $createRecipe): RedirectResponse
|
2026-07-06 12:52:52 +02:00
|
|
|
{
|
2026-07-08 18:16:46 +02:00
|
|
|
$this->authorize('create', Recipe::class);
|
|
|
|
|
|
2026-11-05 11:54:19 +01:00
|
|
|
$result = $this->executor->execute($createRecipe, $request->toCreateRecipeInput());
|
2026-11-03 17:43:37 +01:00
|
|
|
|
2026-11-05 11:54:19 +01:00
|
|
|
return match (true) {
|
|
|
|
|
$result instanceof RecipeId => redirect()->route('back.recipes.edit', $result)
|
|
|
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_created', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
|
|
|
])),
|
|
|
|
|
default => redirect()->back()
|
2026-07-06 12:52:52 +02:00
|
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_created', [
|
2026-07-08 18:16:46 +02:00
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
2026-11-05 11:54:19 +01:00
|
|
|
])),
|
|
|
|
|
};
|
2026-07-06 12:52:52 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-05 16:49:42 +01:00
|
|
|
public function edit(
|
|
|
|
|
RecipeDto $recipe,
|
|
|
|
|
GetProductsSummariesQuery $getProductsSummaries,
|
|
|
|
|
GetRelatedProductsQuery $getRelatedProducts,
|
|
|
|
|
): Response {
|
2026-07-08 18:16:46 +02:00
|
|
|
$this->authorize('update', $recipe);
|
|
|
|
|
|
2026-11-05 16:49:42 +01:00
|
|
|
$productSummaries = $this->executor->execute($getProductsSummaries, QueryInputEmpty::create());
|
|
|
|
|
$relatedProducts = $this->executor->execute(
|
|
|
|
|
$getRelatedProducts,
|
|
|
|
|
new GetRelatedProductsQueryInput($recipe->getId(), 100)
|
|
|
|
|
);
|
|
|
|
|
$selectedProductIds = array_map(fn (ProductDto $product): string => $product->id, $relatedProducts);
|
2026-07-09 15:41:15 +02:00
|
|
|
|
2026-07-08 18:16:46 +02:00
|
|
|
return $this->inertiaRender('back/Pages/Recipes/Edit', [
|
|
|
|
|
'data' => CommonResource::make($recipe),
|
2026-11-05 16:49:42 +01:00
|
|
|
'selectedProductIds' => $selectedProductIds,
|
2026-07-08 18:16:46 +02:00
|
|
|
'enumProductCategory' => ProductCategory::toArray(),
|
|
|
|
|
'enumConsumptionTime' => ConsumptionTime::toArray(),
|
|
|
|
|
'enumInspiration' => Inspiration::toArray(),
|
|
|
|
|
'enumSeason' => Season::toArray(),
|
2026-11-05 11:54:19 +01:00
|
|
|
'allProducts' => $productSummaries->toArray(),
|
2026-07-06 12:52:52 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(
|
|
|
|
|
UpdateRecipeRequest $request,
|
2026-11-05 16:49:42 +01:00
|
|
|
RecipeDto $recipe,
|
2026-11-05 11:54:19 +01:00
|
|
|
UpdateRecipe $updateRecipe,
|
2026-11-05 13:11:46 +01:00
|
|
|
): RedirectResponse {
|
2026-07-08 18:16:46 +02:00
|
|
|
$this->authorize('update', $recipe);
|
|
|
|
|
|
2026-11-05 11:54:19 +01:00
|
|
|
$result = $this->executor->execute($updateRecipe, $request->toUpdateRecipeInput($recipe));
|
2026-07-06 12:52:52 +02:00
|
|
|
|
2026-11-05 13:11:46 +01:00
|
|
|
return match (true) {
|
|
|
|
|
$result instanceof RecipeDto => redirect()->route('back.recipes.edit', $recipe)
|
|
|
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_updated', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
|
|
|
])),
|
|
|
|
|
default => redirect()->route('back.recipes.edit', $recipe)
|
|
|
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_updated', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
|
|
|
])),
|
|
|
|
|
};
|
2026-07-06 12:52:52 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-05 17:46:14 +01:00
|
|
|
public function duplicate(RecipeDto $recipe, DuplicateRecipe $duplicateRecipe): RedirectResponse
|
2026-07-06 12:52:52 +02:00
|
|
|
{
|
2026-07-08 18:16:46 +02:00
|
|
|
$this->authorize('duplicate', $recipe);
|
|
|
|
|
|
2026-11-05 17:46:14 +01:00
|
|
|
$result = $this->executor->execute($duplicateRecipe, new DuplicateRecipeInput($recipe->getId()));
|
2026-11-05 16:49:42 +01:00
|
|
|
|
2026-11-05 17:46:14 +01:00
|
|
|
return match (true) {
|
|
|
|
|
$result instanceof RecipeId => redirect()->route('back.recipes.edit', $result)
|
|
|
|
|
->with('success', trans('back.has_been_duplicated', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
|
|
|
])),
|
|
|
|
|
default => redirect()->back()
|
|
|
|
|
->with('error', trans('back.cannot_be_duplicated', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
|
|
|
])),
|
|
|
|
|
};
|
2026-07-06 12:52:52 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-05 11:54:19 +01:00
|
|
|
public function restore(string $id, RestoreRecipe $restoreRecipe): RedirectResponse
|
2026-07-06 12:52:52 +02:00
|
|
|
{
|
2026-07-09 10:06:36 +02:00
|
|
|
$recipe = Recipe::withTrashed()->findOrFail($id);
|
2026-07-08 18:16:46 +02:00
|
|
|
$this->authorize('restore', $recipe);
|
|
|
|
|
|
2026-07-06 12:52:52 +02:00
|
|
|
$previousQueries = $this->getPreviousQueries();
|
|
|
|
|
|
2026-11-05 11:54:19 +01:00
|
|
|
$result = $this->executor->execute($restoreRecipe, new RestoreRecipeInput($recipe->getId()));
|
|
|
|
|
|
|
|
|
|
return match (true) {
|
|
|
|
|
$result instanceof RecipeId => redirect()->route('back.recipes.trash', $previousQueries)
|
2026-07-06 12:52:52 +02:00
|
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_restored', [
|
2026-07-08 18:16:46 +02:00
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
2026-11-05 11:54:19 +01:00
|
|
|
])),
|
|
|
|
|
default => redirect()->back()
|
|
|
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_restored', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
|
|
|
])),
|
|
|
|
|
};
|
2026-07-06 12:52:52 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-05 16:49:42 +01:00
|
|
|
public function destroy(RecipeDto $recipe, DeleteRecipe $deleteRecipe): RedirectResponse
|
2026-07-06 12:52:52 +02:00
|
|
|
{
|
2026-07-08 18:16:46 +02:00
|
|
|
$this->authorize('delete', $recipe);
|
2026-07-06 12:52:52 +02:00
|
|
|
$previousQueries = $this->getPreviousQueries();
|
|
|
|
|
|
2026-11-05 11:54:19 +01:00
|
|
|
$result = $this->executor->execute($deleteRecipe, new DeleteRecipeInput($recipe->getId()));
|
|
|
|
|
|
|
|
|
|
return match (true) {
|
|
|
|
|
$result instanceof RecipeId => redirect()->route('back.recipes.index', $previousQueries)
|
2026-07-06 12:52:52 +02:00
|
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_deleted', [
|
2026-07-08 18:16:46 +02:00
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
2026-11-05 11:54:19 +01:00
|
|
|
])),
|
|
|
|
|
default => redirect()->back()
|
|
|
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_deleted', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
|
|
|
])),
|
|
|
|
|
};
|
2026-07-06 12:52:52 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-05 11:54:19 +01:00
|
|
|
public function forceDestroy(string $id, ForceDeleteRecipe $forceDeleteRecipe): RedirectResponse
|
2026-07-06 12:52:52 +02:00
|
|
|
{
|
2026-07-09 10:06:36 +02:00
|
|
|
$recipe = Recipe::withTrashed()->findOrFail($id);
|
|
|
|
|
$this->authorize('forceDelete', $recipe);
|
2026-07-06 12:52:52 +02:00
|
|
|
|
|
|
|
|
$previousQueries = $this->getPreviousQueries();
|
|
|
|
|
|
2026-11-05 11:54:19 +01:00
|
|
|
$result = $this->executor->execute($forceDeleteRecipe, new ForceDeleteRecipeInput($recipe->getId()));
|
|
|
|
|
|
|
|
|
|
return match (true) {
|
|
|
|
|
$result instanceof RecipeId => redirect()->route('back.recipes.trash', $previousQueries)
|
2026-07-06 12:52:52 +02:00
|
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_deleted', [
|
2026-07-08 18:16:46 +02:00
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
2026-11-05 11:54:19 +01:00
|
|
|
])),
|
|
|
|
|
default => redirect()->back()
|
|
|
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_deleted', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
|
|
|
])),
|
|
|
|
|
};
|
2026-07-06 12:52:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return array<array-key,string|array<mixed>> */
|
|
|
|
|
private function getPreviousQueries(): array
|
|
|
|
|
{
|
|
|
|
|
$previousQueries = [];
|
|
|
|
|
$previousQueryString = \parse_url($this->getPreviousUrl(), \PHP_URL_QUERY) ?? '' ?: '';
|
|
|
|
|
\parse_str($previousQueryString, $previousQueries);
|
|
|
|
|
|
|
|
|
|
return $previousQueries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getPreviousUrl(): string
|
|
|
|
|
{
|
|
|
|
|
return url()->previous(route('back.dashboard.index'));
|
|
|
|
|
}
|
|
|
|
|
}
|