ldc-ovoteam/app/Http/Controllers/Back/RecipeController.php

241 lines
7.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Back;
use App\Http\Requests\Back\Recipes\IndexRecipeRequest;
use App\Http\Requests\Back\Recipes\StoreRecipeRequest;
use App\Http\Requests\Back\Recipes\UpdateRecipeRequest;
use App\Models\Recipe;
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;
use Uwil\LaravelBackend\Resources\CommonResource;
/**
* Handles CRUD operations for Recipe in the backend.
*/
class RecipeController extends BackendController
{
public function __construct(
Request $request,
Session $session,
) {
parent::__construct(
$request,
$session
);
$this->authorizeResource(Recipe::class);
$this->modelName = Recipe::modelName();
$this->modelPage = Recipe::modelPage();
}
/**
* Display a listing of the resource.
*/
public function index(IndexRecipeRequest $request): Response
{
/** @var \Illuminate\Database\Eloquent\Builder<\App\Models\Recipe> $query */
$query = Recipe::query();
if ($request->routeIs("back.{$this->modelName}.trash")) {
$query = $query->onlyTrashed();
}
$searchValue = $request->getSearchValue();
if ($searchValue) {
$this->searchQuery(
$query,
$searchValue,
null,
'name'
);
}
$searchFields = \implode(', ', [
trans('validation.attributes.name'),
]);
/** Sort columns with a query */
$this->sortQuery($query);
/** Custom pagination */
$paginatedModels = $this->paginate($query);
return $this->inertiaRender("back/Pages/{$this->modelPage}/Index", [
'paginatedModels' => CommonResource::collection($paginatedModels),
'search' => $searchValue,
'searchFields' => $searchFields,
]);
}
/**
* Show the form for creating a new resource.
*/
public function create(Recipe $recipe): Response
{
return $this->inertiaRender("back/Pages/{$this->modelPage}/Create", [
'data' => CommonResource::make($recipe),
]);
}
/**
* Show a specific resource.
*/
public function show(Recipe $recipe): Response
{
return $this->inertiaRender("back/Pages/{$this->modelPage}/Show", [
'data' => CommonResource::make($recipe),
]);
}
/**
* Store a newly created resource.
*/
public function store(StoreRecipeRequest $request): RedirectResponse
{
return DB::transaction(function () use ($request) {
$recipe = new Recipe();
$recipe->inertiaFill($request->validated());
if ($recipe->saveOrFail()) {
return redirect()->route("back.{$this->modelName}.edit", $recipe)
->with('success', trans('uwil-backend::crud.messages.has_been_created', [
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
]));
}
return redirect()->back()
->with('error', trans('uwil-backend::crud.messages.cannot_be_created', [
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
]));
});
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Recipe $recipe): Response
{
return $this->inertiaRender("back/Pages/{$this->modelPage}/Edit", [
'data' => CommonResource::make($recipe),
]);
}
/**
* Update the specified resource.
*/
public function update(
UpdateRecipeRequest $request,
Recipe $recipe
): RedirectResponse {
return DB::transaction(function () use ($request, $recipe) {
$recipe->inertiaFill($request->validated());
if ($recipe->saveOrFail()) {
return redirect()->route("back.{$this->modelName}.edit", $recipe)
->with('success', trans('uwil-backend::crud.messages.has_been_updated', [
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
]));
}
return redirect()->route("back.{$this->modelName}.edit", $recipe)
->with('error', trans('uwil-backend::crud.messages.cannot_be_updated', [
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
]));
});
}
/**
* Duplicate the specified resource.
*/
public function duplicate(Recipe $recipe): Response
{
return $this->create($recipe->replicate());
}
/**
* Restore the specified resource.
*/
public function restore(int $recipe): RedirectResponse
{
$recipe = Recipe::withTrashed()->findOrFail($recipe);
$previousQueries = $this->getPreviousQueries();
if ($recipe->restore()) {
return redirect()->route("back.{$this->modelName}.trash", $previousQueries)
->with('success', trans('uwil-backend::crud.messages.has_been_restored', [
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
]));
}
return redirect()->back()
->with('error', trans('uwil-backend::crud.messages.cannot_be_restored', [
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
]));
}
/**
* Destroy the specified resource.
*/
public function destroy(Recipe $recipe): RedirectResponse
{
$previousQueries = $this->getPreviousQueries();
if ($recipe->delete()) {
return redirect()->route("back.{$this->modelName}.index", $previousQueries)
->with('success', trans('uwil-backend::crud.messages.has_been_deleted', [
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
]));
}
return redirect()->back()
->with('error', trans('uwil-backend::crud.messages.cannot_be_deleted', [
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
]));
}
/**
* Destroy the specified resource.
*/
public function forceDestroy(Recipe | int $recipe): RedirectResponse
{
if (is_int($recipe)) {
$recipe = Recipe::withTrashed()->findOrFail($recipe);
}
$previousQueries = $this->getPreviousQueries();
if ($recipe->forceDelete()) {
return redirect()->route("back.{$this->modelName}.trash", $previousQueries)
->with('success', trans('uwil-backend::crud.messages.has_been_deleted', [
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
]));
}
return redirect()->back()
->with('error', trans('uwil-backend::crud.messages.cannot_be_deleted', [
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 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'));
}
}