Improved clarity by introducing a variable for request data before filling the model.
251 lines
8.7 KiB
PHP
251 lines
8.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Back;
|
|
|
|
use App\Contracts\Directories\ProductDirectory;
|
|
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\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\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Inertia\Response;
|
|
|
|
class RecipeController extends BackendController
|
|
{
|
|
public function __construct(
|
|
Request $request,
|
|
Session $session,
|
|
private readonly ProductDirectory $productDirectory,
|
|
) {
|
|
parent::__construct(
|
|
$request,
|
|
$session
|
|
);
|
|
|
|
$this->modelName = Recipe::modelName();
|
|
$this->modelPage = Recipe::modelPage();
|
|
}
|
|
|
|
public function index(IndexRecipeRequest $request): Response
|
|
{
|
|
$this->authorize('viewAny', Recipe::class);
|
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder<\App\Models\Recipe> $query */
|
|
$query = Recipe::query();
|
|
|
|
if ($request->routeIs('back.recipes.trash')) {
|
|
$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);
|
|
|
|
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(),
|
|
]);
|
|
}
|
|
|
|
public function show(Recipe $recipe): RedirectResponse
|
|
{
|
|
$this->authorize('viewAny', Recipe::class);
|
|
|
|
return redirect()->route('back.recipes.edit', $recipe);
|
|
}
|
|
|
|
public function create(Recipe $recipe): Response
|
|
{
|
|
$this->authorize('create', Recipe::class);
|
|
|
|
return $this->inertiaRender('back/Pages/Recipes/Create', [
|
|
'data' => CommonResource::make($recipe),
|
|
'enumProductCategory' => ProductCategory::toArray(),
|
|
'enumConsumptionTime' => ConsumptionTime::toArray(),
|
|
'enumInspiration' => Inspiration::toArray(),
|
|
'enumSeason' => Season::toArray(),
|
|
'allProducts' => $this->productDirectory->getAllProductsSummary(),
|
|
]);
|
|
}
|
|
|
|
public function store(StoreRecipeRequest $request): RedirectResponse
|
|
{
|
|
$this->authorize('create', Recipe::class);
|
|
|
|
return DB::transaction(function () use ($request) {
|
|
$recipe = new Recipe();
|
|
/** @var array<string, mixed> $data */
|
|
$data = $request->only($recipe->getFillable());
|
|
$recipe->fill($data);
|
|
|
|
if ($recipe->saveOrFail()) {
|
|
$recipe->products()
|
|
->sync($request->array('products'));
|
|
|
|
return redirect()->route('back.recipes.edit', $recipe)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.recipes', 1)),
|
|
]));
|
|
}
|
|
|
|
return 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): Response
|
|
{
|
|
$this->authorize('update', $recipe);
|
|
|
|
$recipe->load('products');
|
|
|
|
return $this->inertiaRender('back/Pages/Recipes/Edit', [
|
|
'data' => CommonResource::make($recipe),
|
|
'enumProductCategory' => ProductCategory::toArray(),
|
|
'enumConsumptionTime' => ConsumptionTime::toArray(),
|
|
'enumInspiration' => Inspiration::toArray(),
|
|
'enumSeason' => Season::toArray(),
|
|
'allProducts' => $this->productDirectory->getAllProductsSummary(),
|
|
]);
|
|
}
|
|
|
|
public function update(
|
|
UpdateRecipeRequest $request,
|
|
Recipe $recipe
|
|
): RedirectResponse {
|
|
$this->authorize('update', $recipe);
|
|
|
|
return DB::transaction(function () use ($request, $recipe) {
|
|
/** @var array<string, mixed> $data */
|
|
$data = $request->only($recipe->getFillable());
|
|
$recipe->fill($data);
|
|
|
|
if ($recipe->saveOrFail()) {
|
|
$recipe->products()
|
|
->sync($request->array('products'));
|
|
|
|
return 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)),
|
|
]));
|
|
}
|
|
|
|
return 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): Response
|
|
{
|
|
$this->authorize('duplicate', $recipe);
|
|
|
|
return $this->create($recipe->replicate());
|
|
}
|
|
|
|
public function restore(string $id): RedirectResponse
|
|
{
|
|
$recipe = Recipe::withTrashed()->findOrFail($id);
|
|
$this->authorize('restore', $recipe);
|
|
|
|
$previousQueries = $this->getPreviousQueries();
|
|
|
|
if ($recipe->restore()) {
|
|
return 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)),
|
|
]));
|
|
}
|
|
|
|
return 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): RedirectResponse
|
|
{
|
|
$this->authorize('delete', $recipe);
|
|
|
|
$previousQueries = $this->getPreviousQueries();
|
|
|
|
if ($recipe->delete()) {
|
|
return 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)),
|
|
]));
|
|
}
|
|
|
|
return 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): RedirectResponse
|
|
{
|
|
$recipe = Recipe::withTrashed()->findOrFail($id);
|
|
$this->authorize('forceDelete', $recipe);
|
|
|
|
$previousQueries = $this->getPreviousQueries();
|
|
|
|
if ($recipe->forceDelete()) {
|
|
return 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)),
|
|
]));
|
|
}
|
|
|
|
return 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'));
|
|
}
|
|
}
|