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;
|
|
|
|
|
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 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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index(IndexRecipeRequest $request): Response
|
|
|
|
|
{
|
2026-07-08 18:16:46 +02:00
|
|
|
$this->authorize('viewAny', Recipe::class);
|
|
|
|
|
|
2026-07-06 12:52:52 +02:00
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder<\App\Models\Recipe> $query */
|
|
|
|
|
$query = Recipe::query();
|
|
|
|
|
|
2026-07-08 18:16:46 +02:00
|
|
|
if ($request->routeIs('back.recipes.trash')) {
|
2026-07-06 12:52:52 +02:00
|
|
|
$query = $query->onlyTrashed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$searchValue = $request->getSearchValue();
|
|
|
|
|
if ($searchValue) {
|
|
|
|
|
$this->searchQuery(
|
|
|
|
|
$query,
|
|
|
|
|
$searchValue,
|
|
|
|
|
null,
|
|
|
|
|
'name'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$searchFields = \implode(', ', [
|
|
|
|
|
trans('validation.attributes.name'),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->sortQuery($query);
|
|
|
|
|
|
|
|
|
|
$paginatedModels = $this->paginate($query);
|
|
|
|
|
|
2026-07-08 18:16:46 +02:00
|
|
|
return $this->inertiaRender('back/Pages/Recipes/Index', [
|
|
|
|
|
'paginatedModels' => CommonResource::collection($paginatedModels),
|
|
|
|
|
'search' => $searchValue,
|
|
|
|
|
'searchFields' => $searchFields,
|
|
|
|
|
'enumProductCategory' => ProductCategory::toArray(),
|
|
|
|
|
'enumConsumptionTime' => ConsumptionTime::toArray(),
|
|
|
|
|
'enumInspiration' => Inspiration::toArray(),
|
|
|
|
|
'enumSeason' => Season::toArray(),
|
2026-07-06 12:52:52 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-08 18:16:46 +02:00
|
|
|
public function show(Recipe $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 11:54:19 +01:00
|
|
|
public function edit(Recipe $recipe, GetProductsSummariesQuery $getProductsSummaries): Response
|
2026-07-06 12:52:52 +02:00
|
|
|
{
|
2026-07-08 18:16:46 +02:00
|
|
|
$this->authorize('update', $recipe);
|
|
|
|
|
|
2026-07-09 15:41:15 +02:00
|
|
|
$recipe->load('products');
|
2026-11-05 11:54:19 +01:00
|
|
|
$productSummaries = $this->executor->execute($getProductsSummaries, QueryInputEmpty::create());
|
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),
|
|
|
|
|
'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-04 16:42:20 +01:00
|
|
|
Recipe $recipe,
|
2026-11-05 11:54:19 +01:00
|
|
|
UpdateRecipe $updateRecipe,
|
|
|
|
|
): RecipeDto {
|
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 11:54:19 +01:00
|
|
|
if (! $result instanceof RecipeDto) {
|
|
|
|
|
abort(500, 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 11:54:19 +01:00
|
|
|
return $result;
|
2026-07-06 12:52:52 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-05 11:17:20 +01:00
|
|
|
public function duplicate(Recipe $recipe, GetProductsSummariesQuery $getProductsSummaries): Response
|
2026-07-06 12:52:52 +02:00
|
|
|
{
|
2026-07-08 18:16:46 +02:00
|
|
|
$this->authorize('duplicate', $recipe);
|
|
|
|
|
|
2026-11-05 11:17:20 +01:00
|
|
|
return $this->create($recipe->replicate(), $getProductsSummaries);
|
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 11:54:19 +01:00
|
|
|
public function destroy(Recipe $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'));
|
|
|
|
|
}
|
|
|
|
|
}
|