Ekapp/skolehjem/app/Http/Controllers/WashingReservationControlle...

157 lines
4.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\WashingMachine;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
2020-06-24 06:23:15 +00:00
use Illuminate\Http\Response;
2020-06-15 06:59:15 +00:00
use App\WashingReservation;
use Illuminate\View\View;
class WashingReservationController extends Controller
{
2020-06-30 08:04:06 +00:00
public function __construct()
{
$this->middleware([ "auth" ]);
$this->middleware([ "check.auth:washing.machine.reservation.list" ])->only("index");
$this->middleware([ "check.auth:washing.machine.reservation.show" ])->only("show");
$this->middleware([ "check.auth:washing.machine.reservation.create" ])->only("create", "store");
$this->middleware([ "check.auth:washing.machine.reservation.edit" ])->only("edit", "update");
$this->middleware([ "check.auth:washing.machine.reservation.delete" ])->only("delete");
}
/**
* Display a listing of the resource.
*
2020-06-15 06:59:15 +00:00
* @param Request $request
* @return Application|Factory|View
*/
2020-06-15 06:59:15 +00:00
public function index(Request $request)
{
$reservations = WashingReservation::query()->paginate($request->query("limit", 20));
2020-06-15 06:59:15 +00:00
return Response::detect("washing-reservations.index", [ "reservations" => $reservations]);
}
/**
* Show the form for creating a new resource.
*
* @return Application|Factory|View
*/
public function create()
{
$machines = WashingMachine::all();
return Response::detect("washing-reservations.create", [ 'machines' => $machines ]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return Application|Factory|View
*/
public function store(Request $request)
{
2020-06-15 06:59:15 +00:00
$data = $request->validate([
"time" => "required",
"machine" => "required"
2020-06-15 06:59:15 +00:00
]);
$machineReservation = new WashingReservation($data);
$machineReservation->save();
2020-07-01 09:54:33 +00:00
$saved = $machineReservation->save();
if(!$saved){
return Response::detect("washing-reservations.store", [
"washing_reservation" => $machineReservation
]);
}else{
$reservations = WashingReservation::query()->paginate($request->input("limit", 20));
2020-07-01 09:56:44 +00:00
2020-07-02 08:06:17 +00:00
return Response::detect("washing-reservations.index", [ "reservations" => $reservations]);
2020-07-01 09:54:33 +00:00
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return Application|Factory|View
*/
public function show($id)
{
2020-06-15 06:59:15 +00:00
$machineReservation = WashingReservation::find($id);
return Response::detect("washing-reservations.show", [
2020-06-15 06:59:15 +00:00
"machineReservation" => $machineReservation
]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Application|Factory|View
*/
public function edit($id)
{
$reservation = WashingReservation::query()->find($id);
$machines = WashingMachine::all();
return Response::detect("washing-reservations.edit", ['washing_reservation' => $reservation, 'machines' => $machines ]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return Application|Factory|View
*/
public function update(Request $request, $id)
{
2020-06-15 06:59:15 +00:00
$data = $request->validate([
"time" => "required",
"machine" => "required"
2020-06-15 06:59:15 +00:00
]);
$machineReservation = WashingReservation::find($id);
$machineReservation->update($data);
2020-07-01 09:54:33 +00:00
$saved = $machineReservation->save();
2020-06-15 06:59:15 +00:00
2020-07-01 09:54:33 +00:00
if(!$saved){
return Response::detect("washing-reservations.update", [
"washing_reservation" => $machineReservation
]);
}else{
2020-07-02 08:06:17 +00:00
$reservations = WashingReservation::query()->paginate($request->query("limit", 20));
2020-07-01 09:56:44 +00:00
2020-07-02 08:06:17 +00:00
return Response::detect("washing-reservations.index", [ "reservations" => $reservations]);
2020-07-01 09:54:33 +00:00
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return RedirectResponse
*/
public function destroy($id)
{
2020-06-15 06:59:15 +00:00
$machineReservation = WashingReservation::find($id);
$machineReservation->delete();
$reservations = WashingReservation::query()->paginate( 20);
return redirect()->route("washing-reservations.index", [ "reservations" => $reservations]);
}
}