Ekapp/skolehjem/app/Http/Controllers/WashingMachineController.php

147 lines
3.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\WashingMachine;
use Illuminate\View\View;
class WashingMachineController extends Controller
{
public function __construct()
{
$this->middleware([ "auth" ]);
$this->middleware([ "check.auth:washing.machine.list" ])->only("index");
$this->middleware([ "check.auth:washing.machine.show" ])->only("show");
$this->middleware([ "check.auth:washing.machine.create" ])->only("create", "store");
$this->middleware([ "check.auth:washing.machine.edit" ])->only("edit", "update");
$this->middleware([ "check.auth:washing.machine.delete" ])->only("delete");
}
/**
* Display a listing of the resource.
*
* @param Request $request
* @return Application|Factory|View
*/
public function index(Request $request)
{
$machines = WashingMachine::query()->paginate($request->query("limit", 20));
return Response::detect("washing-machines.index", [ "machines" => $machines ]);
}
/**
* Show the form for creating a new resource.
*
* @return Application|Factory|View
*/
public function create()
{
return Response::detect("washing-machines.create");
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return Application|Factory|View
*/
public function store(Request $request)
{
$data = $request->validate([
"name" => "required"
]);
$machine = new WashingMachine($data);
$saved = $machine->save();
if(!$saved){
return Response::detect("washing-machines.store");
}else{
$machines = WashingMachine::query()->paginate($request->input("limit", 20));
return Response::detect("washing-machines.index", ['machines' => $machines]);
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return Application|Factory|View
*/
public function show($id)
{
$machine = WashingMachine::find($id);
return Response::detect("washing-machines.show", [
"machine" => $machine
]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Application|Factory|View
*/
public function edit($id)
{
$machine = WashingMachine::find($id);
return Response::detect("washing-machines.edit", [
"machine" => $machine
]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return Application|Factory|View
*/
public function update(Request $request, $id)
{
$data = $request->validate([
"name" => "required"
]);
$machine = WashingMachine::find($id);
$machine->update($data);
$saved = $machine->save();
if(!$saved){
return Response::detect("washing-machines.update", [
"machine" => $machine
]);
} else {
$machines = WashingMachine::query()->paginate($request->input("limit", 20));
return Response::detect("washing-machines.index", [
"machines" => $machines
]);
}
}
/**
* Remove the specified resource from storage.
*
* @param $id
* @return Response
*/
public function destroy($id)
{
$machine = WashingMachine::find($id);
$machine->delete();
return redirect()->route("washing-machines.index");
}
}