Files
database-pertani-web/app/Http/Controllers/Web/Program/TransformasiController.php
T
2026-04-27 09:13:35 +07:00

130 lines
3.5 KiB
PHP

<?php
namespace App\Http\Controllers\Web\Program;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Gate;
use App\Models\Program\Framework;
use App\Models\Program\Transformasi;
class TransformasiController extends Controller
{
public function index()
{
//
}
public function create()
{
//
}
public function store(Request $request, string $frameworkId): JsonResponse
{
if (!Gate::any(['super_admin', 'admin'])) {
abort(403);
}
$framework = Framework::find($frameworkId);
if (!$framework) {
return response()->json([
'status' => 'error',
'message' => 'Data kerangka kerja tidak ditemukan.'
], 404);
}
$validated = $request->validate([
'kode' => ['required','string','max:255'],
'nama' => ['required','string','max:255'],
]);
Transformasi::create([...$validated,
'framework_id' => $frameworkId,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);
return response()->json([
'status' => 'success',
'message' => 'Transformasi berhasil ditambahkan.'
], 201);
}
public function show(string $frameworkId, string $id)
{
if (!Gate::any(['super_admin', 'admin'])) {
abort(403);
}
$transformasi = Transformasi::where('framework_id', $frameworkId)->find($id);
if (!$transformasi) {
return response()->json([
'status' => 'error',
'message' => 'Data transformasi tidak ditemukan.'
], 404);
}
return response()->json(['data' => $transformasi, 'status' => 'success']);
}
public function edit(string $frameworkId, string $id)
{
//
}
public function update(Request $request, string $frameworkId, string $id)
{
if (!Gate::any(['super_admin', 'admin'])) {
abort(403);
}
$transformasi = Transformasi::where('framework_id', $frameworkId)->find($id);
if (!$transformasi) {
return response()->json([
'status' => 'error',
'message' => 'Data transformasi tidak ditemukan.'
], 404);
}
$validated = $request->validate([
'kode' => ['required','string','max:255'],
'nama' => ['required','string','max:255'],
]);
$transformasi->update([...$validated,
'framework_id' => $frameworkId,
'updated_by' => auth()->id(),
]);
return response()->json([
'status' => 'success',
'message' => 'Transformasi berhasil diperbarui.'
], 200);
}
public function destroy(string $frameworkId, string $id)
{
if (!Gate::any(['super_admin', 'admin'])) {
abort(403);
}
$transformasi = Transformasi::where('framework_id', $frameworkId)->find($id);
if (!$transformasi) {
return response()->json([
'status' => 'error',
'message' => 'Data transformasi tidak ditemukan.'
], 404);
}
$transformasi->delete();
return response()->json([
'status' => 'success',
'message' => 'Transformasi berhasil dihapus.'
], 200);
}
}