abschlussprojekt-device-man.../device-app/app/Http/Controllers/DeviceController.php

89 lines
2.2 KiB
PHP
Raw Normal View History

2023-03-25 11:02:47 +01:00
<?php
namespace App\Http\Controllers;
use App\Models\Device;
use Illuminate\Http\Request;
2023-03-26 12:17:43 +02:00
use Illuminate\Validation\Rule;
2023-03-31 17:17:09 +02:00
use Illuminate\Support\Facades\Gate;
use Illuminate\Http\RedirectResponse;
2023-04-04 15:52:47 +02:00
use Illuminate\Support\Facades\Log;
2023-03-25 11:02:47 +01:00
class DeviceController extends Controller
{
2023-03-31 17:17:09 +02:00
2023-03-25 11:02:47 +01:00
public function index()
{
return view('devices.index', [
'devices' => Device::all()
]);
}
2023-03-31 17:17:09 +02:00
public function create()
{
$this->authorize('admin-only');
2023-03-26 12:17:43 +02:00
return view('devices.create');
}
2023-03-26 11:32:54 +02:00
2023-03-31 17:17:09 +02:00
public function store(Request $request)
{
$this->authorize('admin-only');
2023-03-26 12:17:43 +02:00
$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',
2023-03-26 11:32:54 +02:00
]);
2023-03-26 12:17:43 +02:00
Device::create($formFields);
2023-04-04 15:52:47 +02:00
Log::info('Device saved successfully');
2023-03-26 12:17:43 +02:00
return redirect('/devices');
2023-03-26 11:32:54 +02:00
}
2023-03-31 17:17:09 +02:00
public function edit(Device $device)
{
$this->authorize('admin-only');
2023-03-26 17:36:20 +02:00
return view('devices.edit', ['device' => $device]);
}
2023-03-31 17:17:09 +02:00
public function update(Device $device, Request $request)
{
$this->authorize('admin-only');
2023-03-26 18:16:26 +02:00
$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);
2023-04-04 15:52:47 +02:00
Log::info('Device updated successfully');
2023-03-26 18:16:26 +02:00
return back();
}
2023-03-31 17:17:09 +02:00
public function destroy(Device $device): RedirectResponse
{
$this->authorize('admin-only');
2023-03-26 18:16:26 +02:00
$device->delete();
2023-04-04 15:52:47 +02:00
Log::info('Device deleted successfully');
2023-03-26 18:16:26 +02:00
return redirect('devices');
}
2023-03-25 11:02:47 +01:00
public function show(Device $device)
{
return view('devices.show', [
'device' => $device
]);
}
}