simedkom/app/Http/Controllers/Dashboard/LocationController.php
2025-04-16 14:45:04 +07:00

84 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
use App\Http\Requests\LocationRequest;
use App\Models\Location;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
class LocationController extends Controller
{
public function index(){
return view('dashboard.locations.index', [
'title' => 'Data Lokus',
'new_data' => session()->get('new_data', null),
]);
}
public function create(){
return view('dashboard.locations.create', [
'title' => 'Buat Lokus Baru'
]);
}
public function store(LocationRequest $request){
$validatedData = $request->validated();
$validatedData['enhancer_id'] = Auth::id();
try{
$createdLocation = Location::create($validatedData);
return redirect()->route('dashboard.locations.index')->with('success-status', 'Berhasil membuat lokus baru.')
->with('new_data', encrypt_id($createdLocation->id));
}catch(\Exception $e){
return redirect()->back()->with('failed-status', 'Gagal menambahkan lokus.');
}
}
public function edit($id){
$location = Location::findOrFail(decrypt_id($id));
return view('dashboard.locations.edit', [
'title' => 'Ubah Lokus',
'location' => $location
]);
}
public function update(LocationRequest $request, $id){
$validatedData = $request->validated();
$validatedData['enhancer_id'] = Auth::id();
try{
$location = Location::findOrFail(decrypt_id($id));
$location->update($validatedData);
return redirect()->route('dashboard.locations.show', ['id' => encrypt_id($location->id)])->with('success-status', 'Berhasil mengubah lokus.');
}catch(\Exception $e){
return redirect()->back()->with('failed-status', 'Gagal mengubah lokus.');
}
}
public function show($id){
$location = Location::findOrFail(decrypt_id($id));
return view('dashboard.locations.show', [
'title' => 'Detail Lokus',
'location' => $location
]);
}
public function destroy($id){
try {
$location = Location::findOrFail(decrypt_id($id));
$location->delete();
return response()->json(['message' => 'Berhasil menghapus lokus.']);
} catch (\Exception $e) {
return response()->json(['message' => 'Gagal menghapus lokus.'], 500);
}
}
}