first
This commit is contained in:
83
app/Http/Controllers/Admin/PrizeController.php
Normal file
83
app/Http/Controllers/Admin/PrizeController.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\AuditLog;
|
||||
use App\Models\ImportLog;
|
||||
use App\Models\Prize;
|
||||
use App\Services\PrizeImportService;
|
||||
use App\Support\Spreadsheet;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class PrizeController extends Controller
|
||||
{
|
||||
public function index(): View
|
||||
{
|
||||
$prizes = Prize::query()->ordered()->with('drawResults.member')->paginate(30);
|
||||
$importLogs = ImportLog::where('type', 'prizes')->with('importedBy')->latest()->take(5)->get();
|
||||
|
||||
return view('admin.prizes.index', compact('prizes', 'importLogs'));
|
||||
}
|
||||
|
||||
public function edit(Prize $prize): View
|
||||
{
|
||||
return view('admin.prizes.edit', compact('prize'));
|
||||
}
|
||||
|
||||
public function update(Request $request, Prize $prize): RedirectResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'kod_hadiah' => ['nullable', 'string', 'max:255'],
|
||||
'nama_hadiah' => ['required', 'string', 'max:255'],
|
||||
'kategori' => ['nullable', 'string', 'max:255'],
|
||||
'nilai_anggaran' => ['nullable', 'numeric', 'min:0'],
|
||||
'draw_order' => ['required', 'integer', 'min:0'],
|
||||
]);
|
||||
|
||||
$prize->update($data);
|
||||
AuditLog::record('prize.update', "Kemaskini hadiah: {$prize->nama_hadiah}", $prize);
|
||||
|
||||
return redirect()->route('admin.prizes.index')->with('success', 'Maklumat hadiah dikemaskini.');
|
||||
}
|
||||
|
||||
public function destroy(Prize $prize): RedirectResponse
|
||||
{
|
||||
if ($prize->status === Prize::STATUS_DISAHKAN) {
|
||||
return back()->with('error', 'Hadiah yang sudah ada pemenang disahkan tidak boleh dipadam.');
|
||||
}
|
||||
|
||||
$nama = $prize->nama_hadiah;
|
||||
$prize->delete();
|
||||
AuditLog::record('prize.delete', "Padam hadiah: {$nama}");
|
||||
|
||||
return redirect()->route('admin.prizes.index')->with('success', "Hadiah '{$nama}' dipadam.");
|
||||
}
|
||||
|
||||
public function showImport(): View
|
||||
{
|
||||
$logs = ImportLog::where('type', 'prizes')->with('importedBy')->latest()->take(15)->get();
|
||||
|
||||
return view('admin.prizes.import', compact('logs'));
|
||||
}
|
||||
|
||||
public function import(Request $request, PrizeImportService $service): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
'file' => ['required', 'file', 'mimes:csv,txt,xlsx,xls', 'max:10240'],
|
||||
]);
|
||||
|
||||
$file = $request->file('file');
|
||||
$rows = Spreadsheet::read($file->getRealPath(), $file->getClientOriginalExtension());
|
||||
|
||||
$log = $service->import($rows, $file->getClientOriginalName(), $request->user()->id);
|
||||
AuditLog::record('prize.import', "Import hadiah: {$log->filename}", $log, [
|
||||
'success' => $log->success_count,
|
||||
'failed' => $log->failed_count,
|
||||
]);
|
||||
|
||||
return redirect()->route('admin.prizes.import')->with('import_result', $log->id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user