Files
api_arrahn/app/Http/Controllers/FeatureController.php
T
2026-09-08 14:48:13 +08:00

60 lines
1.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Cawangan;
use App\Feature;
use Illuminate\Http\Request;
class FeatureController extends Controller
{
public function index()
{
return response()->json([
'keys' => Feature::keys(),
'labels' => Feature::labels(),
]);
}
public function cawangan()
{
$cawangan = Cawangan::on('mysql7')
->orderBy('kodcaw')
->get(['kodcaw', 'details_caw']);
$maps = Feature::mapForMany($cawangan->pluck('kodcaw')->all());
$data = $cawangan->map(function ($c) use ($maps) {
return [
'kodcaw' => $c->kodcaw,
'details_caw' => $c->nama_cawangan,
'features' => $maps[$c->kodcaw] ?? Feature::defaults(),
];
});
return response()->json($data);
}
public function update($kodcaw, $feature, Request $request)
{
$cawangan = Cawangan::on('mysql7')->where('kodcaw', $kodcaw)->first();
if (!$cawangan) {
return response()->json(['message' => 'Cawangan tidak dijumpai'], 404);
}
if (!in_array($feature, Feature::keys(), true)) {
return response()->json(['message' => 'Ciri tidak sah'], 422);
}
$enabled = (int) $request->input('enabled') ? 1 : 0;
Feature::setEnabled($kodcaw, $feature, $enabled);
return response()->json([
'kodcaw' => $kodcaw,
'feature' => $feature,
'enabled' => $enabled,
'features' => Feature::mapFor($kodcaw),
]);
}
}