76 lines
2.5 KiB
PHP
76 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\ActivityLog;
|
|
use App\Models\RumahIbadah;
|
|
use Illuminate\Http\Request;
|
|
|
|
class IbadahController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return response()->json(RumahIbadah::all());
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'nama' => 'required|string',
|
|
'jenis' => 'nullable|string',
|
|
'alamat' => 'nullable|string',
|
|
'latitude' => 'required|numeric',
|
|
'longitude' => 'required|numeric',
|
|
'radius' => 'nullable|integer',
|
|
'jumlah_jemaah' => 'nullable|integer',
|
|
'pengurus' => 'nullable|string',
|
|
'telepon' => 'nullable|string',
|
|
'keterangan' => 'nullable|string',
|
|
]);
|
|
|
|
$validated['user_id'] = auth()->id();
|
|
$ibadah = RumahIbadah::create($validated);
|
|
ActivityLog::record('create', "Menambah rumah ibadah: {$ibadah->nama}", 'ibadah', $ibadah->id);
|
|
return response()->json(['success' => true, 'data' => $ibadah]);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$ibadah = RumahIbadah::findOrFail($id);
|
|
|
|
if (!auth()->user()->hasRole('administrator') && $ibadah->user_id !== auth()->id()) {
|
|
return response()->json(['message' => 'Tidak diizinkan mengubah data ini.'], 403);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'nama' => 'required|string',
|
|
'jenis' => 'nullable|string',
|
|
'alamat' => 'nullable|string',
|
|
'latitude' => 'required|numeric',
|
|
'longitude' => 'required|numeric',
|
|
'radius' => 'required|integer',
|
|
'jumlah_jemaah' => 'nullable|integer',
|
|
'pengurus' => 'nullable|string',
|
|
'telepon' => 'nullable|string',
|
|
'keterangan' => 'nullable|string',
|
|
]);
|
|
|
|
$ibadah->update($validated);
|
|
ActivityLog::record('update', "Mengubah rumah ibadah: {$ibadah->nama}", 'ibadah', $ibadah->id);
|
|
return response()->json(['success' => true, 'data' => $ibadah]);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$ibadah = RumahIbadah::findOrFail($id);
|
|
|
|
if (!auth()->user()->hasRole('administrator') && $ibadah->user_id !== auth()->id()) {
|
|
return response()->json(['message' => 'Tidak diizinkan menghapus data ini.'], 403);
|
|
}
|
|
|
|
$ibadah->delete();
|
|
ActivityLog::record('delete', "Menghapus rumah ibadah: {$ibadah->nama}", 'ibadah', $id);
|
|
return response()->json(['success' => true]);
|
|
}
|
|
}
|