Updated the handling of nutritional values in StoreProductRequest and UpdateProductRequest to improve clarity and maintainability. The logic for processing nutritional values has been centralized into a dedicated method, enhancing code readability.
230 lines
9.2 KiB
PHP
230 lines
9.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Back;
|
|
|
|
use App\Contracts\Executor;
|
|
use App\Contracts\QueryInputEmpty;
|
|
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;
|
|
use App\Cqs\Queries\Products\GetProductsSummariesQuery;
|
|
use App\Cqs\Queries\Recipes\GetBoPaginatedRecipesQuery;
|
|
use App\Dto\RecipeDto;
|
|
use App\Http\Requests\Back\Recipes\IndexRecipeRequest;
|
|
use App\Http\Requests\Back\Recipes\StoreRecipeRequest;
|
|
use App\Http\Requests\Back\Recipes\UpdateRecipeRequest;
|
|
use App\Http\Resources\CommonResource;
|
|
use App\Models\Recipe;
|
|
use Domain\Models\ValueObjects\Entities\RecipeId;
|
|
use Domain\Models\ValueObjects\Products\ProductCategory;
|
|
use Domain\Models\ValueObjects\Recipes\ConsumptionTime;
|
|
use Domain\Models\ValueObjects\Recipes\Inspiration;
|
|
use Domain\Models\ValueObjects\Recipes\Season;
|
|
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,
|
|
private readonly Executor $executor
|
|
) {
|
|
parent::__construct(
|
|
$request,
|
|
$session
|
|
);
|
|
|
|
$this->modelName = Recipe::modelName();
|
|
$this->modelPage = Recipe::modelPage();
|
|
}
|
|
|
|
public function index(IndexRecipeRequest $request, GetBoPaginatedRecipesQuery $query): Response
|
|
{
|
|
$this->authorize('viewAny', Recipe::class);
|
|
|
|
$output = $this->executor->execute($query, $request->toGetBoPaginatedRecipesQueryInput());
|
|
|
|
return $this->inertiaRender('back/Pages/Recipes/Index', [
|
|
'paginatedModels' => CommonResource::collection($output->paginatedRecipes),
|
|
'search' => $output->search,
|
|
'searchFields' => $output->searchFields,
|
|
'enumProductCategory' => $output->categories,
|
|
'enumConsumptionTime' => $output->consumptionTimes,
|
|
'enumInspiration' => $output->inspirations,
|
|
'enumSeason' => $output->seasons,
|
|
]);
|
|
}
|
|
|
|
public function show(Recipe $recipe): RedirectResponse
|
|
{
|
|
$this->authorize('viewAny', Recipe::class);
|
|
|
|
return redirect()->route('back.recipes.edit', $recipe);
|
|
}
|
|
|
|
public function create(Recipe $recipe, GetProductsSummariesQuery $getProductsSummaries): Response
|
|
{
|
|
$this->authorize('create', Recipe::class);
|
|
|
|
$productSummaries = $this->executor->execute($getProductsSummaries, QueryInputEmpty::create());
|
|
|
|
return $this->inertiaRender('back/Pages/Recipes/Create', [
|
|
'data' => CommonResource::make($recipe),
|
|
'enumProductCategory' => ProductCategory::toArray(),
|
|
'enumConsumptionTime' => ConsumptionTime::toArray(),
|
|
'enumInspiration' => Inspiration::toArray(),
|
|
'enumSeason' => Season::toArray(),
|
|
'allProducts' => $productSummaries->toArray(),
|
|
]);
|
|
}
|
|
|
|
public function store(StoreRecipeRequest $request, CreateRecipe $createRecipe): RedirectResponse
|
|
{
|
|
$this->authorize('create', Recipe::class);
|
|
|
|
$result = $this->executor->execute($createRecipe, $request->toCreateRecipeInput());
|
|
|
|
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()
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
])),
|
|
};
|
|
}
|
|
|
|
public function edit(Recipe $recipe, GetProductsSummariesQuery $getProductsSummaries): Response
|
|
{
|
|
$this->authorize('update', $recipe);
|
|
|
|
$recipe->load('products');
|
|
$productSummaries = $this->executor->execute($getProductsSummaries, QueryInputEmpty::create());
|
|
|
|
return $this->inertiaRender('back/Pages/Recipes/Edit', [
|
|
'data' => CommonResource::make($recipe),
|
|
'enumProductCategory' => ProductCategory::toArray(),
|
|
'enumConsumptionTime' => ConsumptionTime::toArray(),
|
|
'enumInspiration' => Inspiration::toArray(),
|
|
'enumSeason' => Season::toArray(),
|
|
'allProducts' => $productSummaries->toArray(),
|
|
]);
|
|
}
|
|
|
|
public function update(
|
|
UpdateRecipeRequest $request,
|
|
Recipe $recipe,
|
|
UpdateRecipe $updateRecipe,
|
|
): RedirectResponse {
|
|
$this->authorize('update', $recipe);
|
|
|
|
$result = $this->executor->execute($updateRecipe, $request->toUpdateRecipeInput($recipe));
|
|
|
|
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)),
|
|
])),
|
|
};
|
|
}
|
|
|
|
public function duplicate(Recipe $recipe, GetProductsSummariesQuery $getProductsSummaries): Response
|
|
{
|
|
$this->authorize('duplicate', $recipe);
|
|
|
|
return $this->create($recipe->replicate(), $getProductsSummaries);
|
|
}
|
|
|
|
public function restore(string $id, RestoreRecipe $restoreRecipe): RedirectResponse
|
|
{
|
|
$recipe = Recipe::withTrashed()->findOrFail($id);
|
|
$this->authorize('restore', $recipe);
|
|
|
|
$previousQueries = $this->getPreviousQueries();
|
|
|
|
$result = $this->executor->execute($restoreRecipe, new RestoreRecipeInput($recipe->getId()));
|
|
|
|
return match (true) {
|
|
$result instanceof RecipeId => redirect()->route('back.recipes.trash', $previousQueries)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_restored', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
])),
|
|
default => redirect()->back()
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_restored', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
])),
|
|
};
|
|
}
|
|
|
|
public function destroy(Recipe $recipe, DeleteRecipe $deleteRecipe): RedirectResponse
|
|
{
|
|
$this->authorize('delete', $recipe);
|
|
$previousQueries = $this->getPreviousQueries();
|
|
|
|
$result = $this->executor->execute($deleteRecipe, new DeleteRecipeInput($recipe->getId()));
|
|
|
|
return match (true) {
|
|
$result instanceof RecipeId => redirect()->route('back.recipes.index', $previousQueries)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
])),
|
|
default => redirect()->back()
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
])),
|
|
};
|
|
}
|
|
|
|
public function forceDestroy(string $id, ForceDeleteRecipe $forceDeleteRecipe): RedirectResponse
|
|
{
|
|
$recipe = Recipe::withTrashed()->findOrFail($id);
|
|
$this->authorize('forceDelete', $recipe);
|
|
|
|
$previousQueries = $this->getPreviousQueries();
|
|
|
|
$result = $this->executor->execute($forceDeleteRecipe, new ForceDeleteRecipeInput($recipe->getId()));
|
|
|
|
return match (true) {
|
|
$result instanceof RecipeId => redirect()->route('back.recipes.trash', $previousQueries)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
])),
|
|
default => redirect()->back()
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipe', 1)),
|
|
])),
|
|
};
|
|
}
|
|
|
|
/** @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'));
|
|
}
|
|
}
|