192 lines
5.4 KiB
PHP
192 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Web\Ekosistem;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use App\Models\Ekosistem\RingkasanBasisData;
|
|
use App\Models\Ekosistem\TabelBasisData;
|
|
|
|
use App\Rules\ValidSql;
|
|
|
|
class RingkasanBasisDataController extends Controller
|
|
{
|
|
public function index(): View
|
|
{
|
|
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
public function store(Request $request, string $tabelId): JsonResponse
|
|
{
|
|
Gate::authorize('super_admin');
|
|
|
|
$table = TabelBasisData::find($tabelId);
|
|
if (!$table) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'message' => 'Data tabel tidak ditemukan.'
|
|
], 404);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'heading' => ['required', 'string'],
|
|
'subheading' => ['required', 'string'],
|
|
'query_ringkasan' => ['required', 'string', new ValidSql()],
|
|
]);
|
|
|
|
RingkasanBasisData::create([
|
|
'tabel_id' => $tabelId,
|
|
'heading' => $validated['heading'],
|
|
'subheading' => $validated['subheading'],
|
|
'query_ringkasan' => $validated['query_ringkasan'],
|
|
]);
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'message' => 'Ringkasan basis data berhasil ditambahkan.'
|
|
], 201);
|
|
}
|
|
|
|
public function show(string $tabelId, string $id): JsonResponse
|
|
{
|
|
Gate::authorize('super_admin');
|
|
|
|
$ringkasan = RingkasanBasisData::where('tabel_id', $tabelId)->find($id);
|
|
if (!$ringkasan) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'message' => 'Data ringkasan tidak ditemukan.'
|
|
], 404);
|
|
}
|
|
|
|
return response()->json(['data' => $ringkasan, 'status' => 'success']);
|
|
}
|
|
|
|
public function edit(string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
public function update(Request $request, string $id)
|
|
{
|
|
Gate::authorize('super_admin');
|
|
|
|
$ringkasan = RingkasanBasisData::find($id);
|
|
if (!$ringkasan) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'message' => 'Data ringkasan tidak ditemukan.'
|
|
], 404);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'heading' => ['required', 'string'],
|
|
'subheading' => ['required', 'string'],
|
|
'query_ringkasan' => ['required', 'string', new ValidSql()],
|
|
]);
|
|
|
|
$ringkasan->update([...$validated,
|
|
'updated_by' => auth()->id(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'message' => 'Ringkasan basis data berhasil diperbarui.'
|
|
], 200);
|
|
}
|
|
|
|
public function destroy(string $id)
|
|
{
|
|
Gate::authorize('super_admin');
|
|
|
|
$ringkasan = RingkasanBasisData::find($id);
|
|
if (!$ringkasan) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'message' => 'Data ringkasan tidak ditemukan.'
|
|
], 404);
|
|
}
|
|
|
|
$ringkasan->update([
|
|
'deleted_by' => auth()->id(),
|
|
]);
|
|
$ringkasan->delete();
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'message' => 'Ringkasan basis data berhasil dihapus.'
|
|
], 200);
|
|
}
|
|
|
|
public function moveUp(Request $request, string $tabelId, string $id): JsonResponse
|
|
{
|
|
Gate::authorize('super_admin');
|
|
|
|
$current = RingkasanBasisData::where('tabel_id', $tabelId)->find($id);
|
|
if (!$current) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'message' => 'Data ringkasan tidak ditemukan.'
|
|
], 404);
|
|
}
|
|
|
|
$prev = RingkasanBasisData::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' => 'Ringkasan basis data berhasil diperbarui.'
|
|
], 200);
|
|
}
|
|
|
|
public function moveDown(Request $request, string $tabelId, string $id): JsonResponse
|
|
{
|
|
Gate::authorize('super_admin');
|
|
|
|
$current = RingkasanBasisData::where('tabel_id', $tabelId)->find($id);
|
|
if (!$current) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'message' => 'Data ringkasan tidak ditemukan.'
|
|
], 404);
|
|
}
|
|
|
|
$next = RingkasanBasisData::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' => 'Ringkasan basis data berhasil diperbarui.'
|
|
], 200);
|
|
}
|
|
}
|