84 lines
2.5 KiB
PHP
84 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\SourceRequest;
|
|
use App\Models\Source;
|
|
use App\Services\ChangeLogService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class SourceController extends Controller
|
|
{
|
|
public function __construct(private readonly ChangeLogService $changeLogService)
|
|
{
|
|
}
|
|
|
|
public function index(Request $request): View
|
|
{
|
|
$items = Source::query()
|
|
->when($request->filled('q'), function ($query) use ($request): void {
|
|
$keyword = '%'.trim((string) $request->string('q')).'%';
|
|
$query->where('name', 'like', $keyword)->orWhere('domain', 'like', $keyword);
|
|
})
|
|
->orderByDesc('is_whitelisted')
|
|
->orderBy('name')
|
|
->paginate(30)
|
|
->withQueryString();
|
|
|
|
return view('admin.sources.index', [
|
|
'items' => $items,
|
|
'filters' => $request->only(['q']),
|
|
]);
|
|
}
|
|
|
|
public function create(): View
|
|
{
|
|
return view('admin.sources.form', [
|
|
'item' => new Source(),
|
|
'submitRoute' => route('admin.sources.store'),
|
|
'method' => 'POST',
|
|
]);
|
|
}
|
|
|
|
public function store(SourceRequest $request): RedirectResponse
|
|
{
|
|
$item = Source::query()->create($this->normalizePayload($request->validated()));
|
|
$this->changeLogService->log('created', $item);
|
|
|
|
return redirect()->route('admin.sources.edit', $item)->with('status', '来源已创建');
|
|
}
|
|
|
|
public function edit(Source $source): View
|
|
{
|
|
return view('admin.sources.form', [
|
|
'item' => $source,
|
|
'submitRoute' => route('admin.sources.update', $source),
|
|
'method' => 'PUT',
|
|
]);
|
|
}
|
|
|
|
public function update(SourceRequest $request, Source $source): RedirectResponse
|
|
{
|
|
$before = $source->getAttributes();
|
|
$source->fill($this->normalizePayload($request->validated()));
|
|
$source->save();
|
|
|
|
$this->changeLogService->log('updated', $source, $before);
|
|
|
|
return redirect()->route('admin.sources.edit', $source)->with('status', '来源已更新');
|
|
}
|
|
|
|
private function normalizePayload(array $payload): array
|
|
{
|
|
$payload['is_whitelisted'] = (bool) ($payload['is_whitelisted'] ?? false);
|
|
$payload['crawl_allowed'] = (bool) ($payload['crawl_allowed'] ?? false);
|
|
|
|
return $payload;
|
|
}
|
|
}
|