First Commit
This commit is contained in:
+85
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Legal;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\LegalDocument;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class LegalDocumentController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of legal documents.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$this->authorize('viewAny', LegalDocument::class);
|
||||
|
||||
$documents = LegalDocument::orderBy('type')->orderByDesc('version')->paginate(20);
|
||||
|
||||
return view('legal.documents.index', compact('documents'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing a legal document.
|
||||
*/
|
||||
public function edit(string $type)
|
||||
{
|
||||
$document = LegalDocument::where('type', $type)->latest('version')->first();
|
||||
|
||||
if (!$document) {
|
||||
$document = new LegalDocument(['type' => $type]);
|
||||
}
|
||||
|
||||
$this->authorize('update', $document);
|
||||
|
||||
return view('legal.documents.edit', compact('document'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update or create a legal document.
|
||||
*/
|
||||
public function update(Request $request, string $type)
|
||||
{
|
||||
$this->authorize('create', LegalDocument::class);
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => ['required', 'string', 'max:255'],
|
||||
'content' => ['required', 'string'],
|
||||
'version' => ['required', 'string', 'max:50'],
|
||||
'published_at' => ['nullable', 'date'],
|
||||
]);
|
||||
|
||||
$document = LegalDocument::where('type', $type)->latest('version')->first();
|
||||
|
||||
if ($document && $document->version === $validated['version']) {
|
||||
$document->update([
|
||||
'title' => $validated['title'],
|
||||
'content' => $validated['content'],
|
||||
'published_at' => $validated['published_at'] ? now() : null,
|
||||
]);
|
||||
} else {
|
||||
$document = LegalDocument::create(array_merge($validated, [
|
||||
'type' => $type,
|
||||
'published_by' => Auth::id(),
|
||||
'published_at' => $validated['published_at'] ?? now(),
|
||||
]));
|
||||
}
|
||||
|
||||
return redirect()->route('legal.documents.index')->with('success', 'Documento atualizado com sucesso.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the legal document page.
|
||||
*/
|
||||
public function show(string $type)
|
||||
{
|
||||
$document = LegalDocument::where('type', $type)->latest('version')->firstOrFail();
|
||||
|
||||
return view('legal.documents.show', compact('document'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user