Files
database-pertani-web/app/Http/Controllers/Web/Ekosistem/KolomBasisDataController.php
T

219 lines
6.4 KiB
PHP

<?php
namespace App\Http\Controllers\Web\Ekosistem;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
use Illuminate\Http\JsonResponse;
use App\Models\Ekosistem\KolomBasisData;
use App\Models\Ekosistem\TabelBasisData;
class KolomBasisDataController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request, string $tabelId): JsonResponse
{
Gate::authorize('super_admin');
$tabel = TabelBasisData::find($tabelId);
if (!$tabel) {
return response()->json([
'status' => 'error',
'message' => 'Data tabel tidak ditemukan.'
], 404);
}
$validated = $request->validate([
'kolom' => ['required',
'string',
'regex:/^[a-z0-9]+(_[a-z0-9]+)*$/',
Rule::unique('ekosistem_basis_data_kolom')->where(function ($query) use ($request) {
return $query->where('tabel_id', $request->tabel_id)->whereNull('deleted_at');
})
],
'label' => ['required', 'string'],
'tipe' => ['required', 'string', Rule::in(['varchar', 'integer', 'text', 'date', 'boolean', 'datetime', 'float'])],
'validasi' => ['nullable', 'json'],
]);
KolomBasisData::create([...$validated,
'tabel_id' => $tabelId,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);
return response()->json([
'status' => 'success',
'message' => 'Kolom basis data berhasil ditambahkan.'
], 201);
}
/**
* Display the specified resource.
*/
public function show(Request $request, string $tabelId, string $id): JsonResponse
{
Gate::authorize('super_admin');
$kolom = KolomBasisData::where('tabel_id', $tabelId)->find($id);
if (!$kolom) {
return response()->json([
'status' => 'error',
'message' => 'Data kolom tidak ditemukan.'
], 404);
}
return response()->json(['data' => $kolom, 'status' => 'success']);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $tabelId, string $id): JsonResponse
{
Gate::authorize('super_admin');
$kolom = KolomBasisData::where('tabel_id', $tabelId)->find($id);
if (!$kolom) {
return response()->json([
'status' => 'error',
'message' => 'Data kolom tidak ditemukan.'
], 404);
}
$validated = $request->validate([
'kolom' => ['required',
'string',
'regex:/^[a-z0-9]+(_[a-z0-9]+)*$/',
Rule::unique('ekosistem_basis_data_kolom')->where(function ($query) use ($request) {
return $query->where('tabel_id', $request->tabel_id)->whereNull('deleted_at');
})
],
'label' => ['required', 'string'],
'tipe' => ['required', 'string', Rule::in(['varchar', 'integer', 'text', 'date', 'boolean', 'datetime', 'float'])],
'validasi' => ['nullable', 'json'],
]);
$kolom->update([...$validated,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);
return response()->json([
'status' => 'success',
'message' => 'Kolom basis data berhasil diperbarui.'
], 200);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Request $request, string $tabelId, string $id): JsonResponse
{
Gate::authorize('super_admin');
$kolom = KolomBasisData::where('tabel_id', $tabelId)->find($id);
if (!$kolom) {
return response()->json([
'status' => 'error',
'message' => 'Data kolom tidak ditemukan.'
], 404);
}
$kolom->delete();
return response()->json([
'status' => 'success',
'message' => 'Kolom basis data berhasil dihapus.'
], 200);
}
public function moveUp(Request $request, string $tabelId, string $id): JsonResponse
{
Gate::authorize('super_admin');
$current = KolomBasisData::where('tabel_id', $tabelId)->find($id);
if (!$current) {
return response()->json([
'status' => 'error',
'message' => 'Data kolom tidak ditemukan.'
], 404);
}
$prev = KolomBasisData::where('tabel_id', $current->tabel_id)
->where('urutan', '<', $current->urutan)
->orderBy('urutan', 'desc')
->first();
if ($prev) {
// swap
[$current->urutan, $prev->urutan] = [$prev->urutan, $current->urutan];
$current->save();
$prev->save();
}
return response()->json([
'status' => 'success',
'message' => 'Kolom basis data berhasil diperbarui.'
], 200);
}
public function moveDown(Request $request, string $tabelId, string $id): JsonResponse
{
Gate::authorize('super_admin');
$current = KolomBasisData::where('tabel_id', $tabelId)->find($id);
if (!$current) {
return response()->json([
'status' => 'error',
'message' => 'Data kolom tidak ditemukan.'
], 404);
}
$next = KolomBasisData::where('tabel_id', $current->tabel_id)
->where('urutan', '>', $current->urutan)
->orderBy('urutan', 'asc')
->first();
if ($next) {
// swap
[$current->urutan, $next->urutan] = [$next->urutan, $current->urutan];
$current->save();
$next->save();
}
return response()->json([
'status' => 'success',
'message' => 'Kolom basis data berhasil diperbarui.'
], 200);
}
}