abschlussprojekt-device-man.../device-app/app/Http/Controllers/DeviceController.php
2023-04-04 15:52:47 +02:00

89 lines
2.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Device;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Gate;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Log;
class DeviceController extends Controller
{
public function index()
{
return view('devices.index', [
'devices' => Device::all()
]);
}
public function create()
{
$this->authorize('admin-only');
return view('devices.create');
}
public function store(Request $request)
{
$this->authorize('admin-only');
$formFields = $request->validate([
'title' => 'required',
'device_type' => 'required',
'description' => 'required',
'accessories' => 'required',
'rz_username_buyer' => ['required', Rule::unique('devices', 'device_id')],
'serial_number' => 'required',
'image_url' => 'required',
]);
Device::create($formFields);
Log::info('Device saved successfully');
return redirect('/devices');
}
public function edit(Device $device)
{
$this->authorize('admin-only');
return view('devices.edit', ['device' => $device]);
}
public function update(Device $device, Request $request)
{
$this->authorize('admin-only');
$formFields = $request->validate([
'title' => 'required',
'device_type' => 'required',
'description' => 'required',
'accessories' => 'required',
'rz_username_buyer' => 'required',
'serial_number' => 'required',
'image_url' => 'required',
]);
$device->update($formFields);
Log::info('Device updated successfully');
return back();
}
public function destroy(Device $device): RedirectResponse
{
$this->authorize('admin-only');
$device->delete();
Log::info('Device deleted successfully');
return redirect('devices');
}
public function show(Device $device)
{
return view('devices.show', [
'device' => $device
]);
}
}