Penambahan menu Perkuliahan
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Operator;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Legacy\Account;
|
||||
use App\Support\Legacy\ActivityTypes;
|
||||
use App\Support\Legacy\PortalException;
|
||||
use App\Support\Prototype\Satu\Catalog;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Portal > Kegiatan > Kegiatan Akademik (slug ms_kegiatan) untuk akun DB lama: jenis kegiatan asli
|
||||
* (t_kalender_jenis_kegiatan) dengan tambah/ubah/hapus sebaris seperti SATU. Dipanggil SatuPageController.
|
||||
*/
|
||||
class KegiatanController extends Controller
|
||||
{
|
||||
private const string SLUG = 'ms_kegiatan';
|
||||
|
||||
public function __construct(private readonly ActivityTypes $types) {}
|
||||
|
||||
public function __invoke(): View
|
||||
{
|
||||
$page = Catalog::page(self::SLUG);
|
||||
|
||||
return view('operator.satu.kegiatan', [
|
||||
'page' => $page,
|
||||
'crumbs' => ['Beranda' => route('operator.dashboard')] + array_fill_keys(Catalog::trail(self::SLUG), null) + [$page['title'] => null],
|
||||
'note' => $this->types->ready()
|
||||
? 'Jenis kegiatan akademik asli dari SIAKAD. Tambah, ubah, dan hapus langsung tersimpan di basis data.'
|
||||
: 'Jenis kegiatan akademik asli dari SIAKAD. Kolom kode dan warna belum disiapkan (jalankan php artisan siakad:siapkan-portal); nama tetap bisa diubah.',
|
||||
'ready' => $this->types->ready(),
|
||||
'rows' => $this->types->all()->all(),
|
||||
'urls' => [
|
||||
'data' => route('operator.kegiatan.data'),
|
||||
'store' => route('operator.kegiatan.store'),
|
||||
'update' => route('operator.kegiatan.update', '__ID__'),
|
||||
'destroy' => route('operator.kegiatan.destroy', '__ID__'),
|
||||
],
|
||||
'perPage' => [10, 25, 50, 100],
|
||||
]);
|
||||
}
|
||||
|
||||
public function data(Request $request): JsonResponse
|
||||
{
|
||||
$this->legacyOnly($request);
|
||||
|
||||
return response()->json(['rows' => $this->types->all()->all()]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$this->legacyOnly($request);
|
||||
$input = $this->validated($request);
|
||||
|
||||
try {
|
||||
$id = $this->types->create($input, (string) $request->user()->username, $request->ip());
|
||||
} catch (PortalException $e) {
|
||||
return response()->json(['message' => $e->getMessage()], 422);
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'Kegiatan akademik ditambahkan.', 'id' => $id, 'rows' => $this->types->all()->all()], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$this->legacyOnly($request);
|
||||
$input = $this->validated($request);
|
||||
|
||||
try {
|
||||
$this->types->update($id, $input, (string) $request->user()->username, $request->ip());
|
||||
} catch (PortalException $e) {
|
||||
return response()->json(['message' => $e->getMessage()], 422);
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'Kegiatan akademik diperbarui.', 'rows' => $this->types->all()->all()]);
|
||||
}
|
||||
|
||||
public function destroy(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$this->legacyOnly($request);
|
||||
|
||||
try {
|
||||
$this->types->delete($id);
|
||||
} catch (PortalException $e) {
|
||||
return response()->json(['message' => $e->getMessage()], 422);
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'Kegiatan akademik dihapus.', 'rows' => $this->types->all()->all()]);
|
||||
}
|
||||
|
||||
/** Hanya akun DB lama yang menyentuh data asli; akun demo mendapat 404 (halamannya prototipe). */
|
||||
private function legacyOnly(Request $request): void
|
||||
{
|
||||
abort_unless($request->user() instanceof Account, 404);
|
||||
}
|
||||
|
||||
/** @return array{kode: ?string, nama: string, warna: ?string} */
|
||||
private function validated(Request $request): array
|
||||
{
|
||||
return $request->validate([
|
||||
'kode' => ['nullable', 'string', 'max:'.ActivityTypes::KODE_MAX],
|
||||
'nama' => ['required', 'string', 'max:'.ActivityTypes::NAMA_MAX],
|
||||
'warna' => ['nullable', 'string', 'regex:/^#[0-9A-Fa-f]{6}$/'],
|
||||
], [], ['kode' => 'kode', 'nama' => 'nama kegiatan akademik', 'warna' => 'warna latar']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user