simedkom/app/Http/Controllers/Dashboard/LocationController.php

83 lines
2.3 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' => 'Lokus'
]);
}
public function create(){
return view('dashboard.locations.create', [
'title' => 'Tambah Lokus'
]);
}
public function store(LocationRequest $request){
$validatedData = $request->validated();
$validatedData['enhancer_id'] = Auth::id();
$createdLocation = Location::create($validatedData);
if($createdLocation){
return redirect()->route('dashboard.locations.index')->with('success-status', 'Berhasil menambahkan lokus.');
}else{
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();
$location = Location::findOrFail(decrypt_id($id));
$updatedLocation = $location->update($validatedData);
if($updatedLocation){
return redirect()->back()->with('success-status', 'Berhasil mengubah lokus.');
}else{
return redirect()->back()->with('failed-status', 'Gagal mengubah lokus.');
}
}
public function show($id){
$location = Location::find(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);
}
}
}