First commit
This commit is contained in:
102
app/Http/Controllers/Admin/CategoryController.php
Normal file
102
app/Http/Controllers/Admin/CategoryController.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\StoreCategoryRequest;
|
||||
use App\Models\Category;
|
||||
use App\Services\KnowledgeBase\AuditService;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AuditService $auditService
|
||||
) {}
|
||||
|
||||
public function index(): View
|
||||
{
|
||||
$categories = Category::withCount([
|
||||
'documents as total_documents',
|
||||
'documents as active_documents' => fn($q) => $q->where('is_active', true),
|
||||
'knowledgeItems as total_knowledge_items',
|
||||
])
|
||||
->ordered()
|
||||
->withTrashed()
|
||||
->paginate(20);
|
||||
|
||||
return view('admin.categories.index', compact('categories'));
|
||||
}
|
||||
|
||||
public function create(): View
|
||||
{
|
||||
return view('admin.categories.create');
|
||||
}
|
||||
|
||||
public function store(StoreCategoryRequest $request): RedirectResponse
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
if (empty($data['slug'])) {
|
||||
$data['slug'] = Str::slug($data['name']);
|
||||
}
|
||||
|
||||
$data['created_by'] = auth()->id();
|
||||
|
||||
$category = Category::create($data);
|
||||
|
||||
$this->auditService->categoryCreated($category);
|
||||
|
||||
return redirect()
|
||||
->route('admin.categories.index')
|
||||
->with('success', "Kategori '{$category->name}' berjaya dicipta.");
|
||||
}
|
||||
|
||||
public function edit(Category $category): View
|
||||
{
|
||||
return view('admin.categories.edit', compact('category'));
|
||||
}
|
||||
|
||||
public function update(StoreCategoryRequest $request, Category $category): RedirectResponse
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
if (empty($data['slug'])) {
|
||||
$data['slug'] = Str::slug($data['name']);
|
||||
}
|
||||
|
||||
$category->update($data);
|
||||
|
||||
return redirect()
|
||||
->route('admin.categories.index')
|
||||
->with('success', "Kategori '{$category->name}' berjaya dikemaskini.");
|
||||
}
|
||||
|
||||
public function toggleStatus(Category $category): RedirectResponse
|
||||
{
|
||||
$category->update(['is_active' => !$category->is_active]);
|
||||
|
||||
$status = $category->is_active ? 'diaktifkan' : 'dinyahaktifkan';
|
||||
|
||||
return back()->with('success', "Kategori '{$category->name}' telah {$status}.");
|
||||
}
|
||||
|
||||
public function destroy(Category $category): RedirectResponse
|
||||
{
|
||||
// Semak sama ada ada dokumen aktif dalam kategori ini
|
||||
if ($category->documents()->where('is_active', true)->exists()) {
|
||||
return back()->with('error',
|
||||
'Kategori tidak boleh dipadam kerana masih ada dokumen aktif di dalamnya.'
|
||||
);
|
||||
}
|
||||
|
||||
$category->delete(); // SoftDelete
|
||||
|
||||
return redirect()
|
||||
->route('admin.categories.index')
|
||||
->with('success', "Kategori '{$category->name}' telah dipadam.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user