ldc-gen-game-web/app/Http/Controllers/Bo/BannedIpController.php

155 lines
4.6 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-02 12:09:46 +02:00
declare(strict_types=1);
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
2023-08-21 10:15:14 +00:00
use App\Http\Requests\Bo\BannedIps\StoreBannedIpRequest;
use App\Http\Requests\Bo\BannedIps\UpdateBannedIpRequest;
use App\Models\BannedIp;
2026-10-02 12:09:46 +02:00
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
2023-08-21 10:15:14 +00:00
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class BannedIpController extends Controller
{
2023-12-05 12:33:25 +01:00
/**
* Create the controller instance.
*/
public function __construct()
{
$this->authorizeResource(BannedIp::class);
}
2023-08-21 10:15:14 +00:00
/**
* Display a listing of the resource.
*/
2026-10-02 12:09:46 +02:00
public function index(Request $request): View
2021-08-18 17:08:02 +02:00
{
2026-10-02 12:09:46 +02:00
/** @var Builder $bannedipModels */
2023-08-21 10:15:14 +00:00
$bannedipModels = BannedIp::query();
/** @var string $search Search field */
$search = $request->search;
if ($search) {
$this->searchQuery(
$bannedipModels,
$search,
null,
'ip',
);
}
2023-11-23 17:31:28 +01:00
$searchFields = trans('validation.custom.ip');
2023-08-21 10:15:14 +00:00
/** Sort columns with a query */
$this->sortQuery($bannedipModels);
/** Custom pagination */
2023-11-23 17:31:28 +01:00
$bannedipModels = $this->paginate($bannedipModels);
2023-08-21 10:15:14 +00:00
2026-10-02 12:09:46 +02:00
/** @var view-string $view */
$view = 'back::pages.banned-ips.index';
return view($view, ['bannedipModels' => $bannedipModels, 'search' => $search, 'searchFields' => $searchFields]);
2023-08-21 10:15:14 +00:00
}
/**
* Show the form for creating a new resource.
*/
2026-10-02 12:09:46 +02:00
public function create(BannedIp $banned_ip): View
2023-08-21 10:15:14 +00:00
{
2026-10-02 12:09:46 +02:00
/** @var view-string $view */
$view = 'back::pages.banned-ips.create';
return view($view, [
'bannedipModel' => $banned_ip,
2023-08-21 10:15:14 +00:00
]);
}
2023-08-21 10:15:14 +00:00
/**
* Store a newly created resource in storage.
*/
2026-10-02 12:09:46 +02:00
public function store(StoreBannedIpRequest $request): RedirectResponse
2021-08-18 17:08:02 +02:00
{
2023-08-21 10:15:14 +00:00
return DB::transaction(function () use ($request) {
$banned_ip = new BannedIp();
$banned_ip->fill($request->validated());
if ($banned_ip->saveOrFail()) {
2026-10-02 12:09:46 +02:00
return to_route('bo.banned_ips.edit', $banned_ip)
2023-08-21 10:15:14 +00:00
->with('success', trans(
'crud.messages.has_been_created',
['model' => Str::singular(trans('models.classes.banned_ips'))]
2023-08-21 10:15:14 +00:00
));
}
2026-10-02 12:09:46 +02:00
return back()
2023-08-21 10:15:14 +00:00
->with('error', trans(
'crud.messages.cannot_be_created',
['model' => Str::singular(trans('models.classes.banned_ips'))]
2023-08-21 10:15:14 +00:00
));
});
}
2023-08-21 10:15:14 +00:00
/**
* Show the form for editing the specified resource.
*/
2026-10-02 12:09:46 +02:00
public function edit(BannedIp $banned_ip): View
2023-08-21 10:15:14 +00:00
{
2026-10-02 12:09:46 +02:00
/** @var view-string $view */
$view = 'back::pages.banned-ips.edit';
return view($view, [
'bannedipModel' => $banned_ip,
2023-08-21 10:15:14 +00:00
]);
}
2023-08-21 10:15:14 +00:00
/**
* Update the specified resource in storage.
*/
2026-10-02 12:09:46 +02:00
public function update(UpdateBannedIpRequest $request, BannedIp $banned_ip): RedirectResponse
2021-08-18 17:08:02 +02:00
{
return DB::transaction(function () use ($request, $banned_ip) {
$banned_ip->fill($request->validated());
if ($banned_ip->saveOrFail()) {
2026-10-02 12:09:46 +02:00
return to_route('bo.banned_ips.edit', $banned_ip)
2023-08-21 10:15:14 +00:00
->with('success', trans(
'crud.messages.has_been_updated',
['model' => Str::singular(trans('models.classes.banned_ips'))]
2023-08-21 10:15:14 +00:00
));
}
2026-10-02 12:09:46 +02:00
return back()
2023-08-21 10:15:14 +00:00
->with('error', trans(
'crud.messages.cannot_be_updated',
['model' => Str::singular(trans('models.classes.banned_ips'))]
2023-08-21 10:15:14 +00:00
));
});
}
2023-08-21 10:15:14 +00:00
/**
* Remove the specified resource from storage.
*/
2026-10-02 12:09:46 +02:00
public function destroy(BannedIp $banned_ip): RedirectResponse
2023-08-21 10:15:14 +00:00
{
return DB::transaction(function () use ($banned_ip) {
if ($banned_ip->deleteOrFail()) {
2026-10-02 12:09:46 +02:00
return to_route('bo.banned_ips.index')
2023-08-21 10:15:14 +00:00
->with('success', __(
'crud.messages.has_been_deleted',
['model' => Str::singular(trans('models.classes.banned_ips'))]
2023-08-21 10:15:14 +00:00
));
}
2026-10-02 12:09:46 +02:00
return back()
2023-08-21 10:15:14 +00:00
->with('error', __(
':crud.messages.cannot_be_deleted',
['model' => Str::singular(trans('models.classes.banned_ips'))]
2023-08-21 10:15:14 +00:00
));
});
}
}