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

168 lines
4.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Location;
use App\WashingReservation;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
2020-06-24 06:23:15 +00:00
use Illuminate\Http\Response;
2020-06-10 10:30:44 +00:00
use App\WashingMachine;
use Illuminate\View\View;
2020-06-10 10:30:44 +00:00
class WashingMachineController extends Controller
{
2020-06-30 08:04:06 +00:00
public function __construct()
{
$this->middleware([ "auth" ]);
2020-08-06 13:31:38 +00:00
$this->middleware([ "lang" ]);
2020-06-30 08:04:06 +00:00
$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.
*
2020-06-10 10:30:44 +00:00
* @param Request $request
* @return Application|Factory|View
*/
2020-06-10 10:30:44 +00:00
public function index(Request $request)
{
$machines = WashingMachine::query()->paginate($request->query("limit", 20));
2020-06-10 10:30:44 +00:00
return Response::detect("washing-machines.index", [ "machines" => $machines ]);
}
/**
* Show the form for creating a new resource.
*
* @return Application|Factory|View
*/
public function create()
{
$locations = Location::all();
return Response::detect("washing-machines.create", ["locations" => $locations] );
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return Application|Factory|View
*/
public function store(Request $request)
{
2020-06-10 10:30:44 +00:00
$data = $request->validate([
"name" => "required",
"location_id" => "required"
2020-06-10 10:30:44 +00:00
]);
$machine = new WashingMachine($data);
$allMachines = WashingMachine::query()->where('name', '=', $request->name)->where('location_id', "=", $request->location_id)->get();
// If there already is a washing machine with that name, then don't add it
if (count($allMachines) > 0)
return redirect()->route("washing-machines.store")->with('WashingNameExists', '<p><b>Der findes allerede en vaskemaskine med det navn!</b></p>');
else { // Else - Add it
$machine->save();
2020-07-01 09:54:33 +00:00
$machines = WashingMachine::query()->paginate($request->input("limit", 20));
return redirect()->route("washing-machines.index", ['machines' => $machines]);
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-10 10:30:44 +00:00
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Application|Factory|View
*/
public function edit($id)
{
2020-06-10 10:30:44 +00:00
$machine = WashingMachine::find($id);
$locations = Location::all();
2020-06-10 10:30:44 +00:00
return Response::detect("washing-machines.edit", [
"machine" => $machine,
"locations" => $locations
2020-06-10 10:30:44 +00:00
]);
}
/**
* 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-10 10:30:44 +00:00
$data = $request->validate([
"name" => "required",
"location_id" => "required",
2020-06-10 10:30:44 +00:00
]);
$machine = WashingMachine::find($id);
$allMachines = WashingMachine::query()->where('name', '=', $request->name)->where('location_id', "=", $request->location_id)->where('id', '!=', $id)->get();
// If there already is a washing machine with that name, then don't change it
if (count($allMachines) > 0)
return redirect()->route("washing-machines.store")->with('WashingNameExists', '<p><b>Der findes allerede en vaskemaskine med det navn!</b></p>');
else { // Else - Change the name
$machine->update($data);
$machine->save();
$machines = WashingMachine::query()->paginate($request->input("limit", 20));
return redirect()->route("washing-machines.index", ["machines" => $machines]);
}
}
/**
* Remove the specified resource from storage.
*
* @param $id
* @return Response
*/
public function destroy($id)
{
2020-06-10 10:30:44 +00:00
$machine = WashingMachine::find($id);
$allReservations = WashingReservation::query()->where('machine_id', '=', $id);
$allReservations->delete();
2020-06-10 10:30:44 +00:00
$machine->delete();
2020-07-01 08:56:04 +00:00
return redirect()->route("washing-machines.index");
}
public function nameCheck(Request $request){
$washing = WashingMachine::query()->where('name', 'LIKE',$request->nameCheck)->get();
if(count($washing) > 0 && $request->nameCheck !== NULL){
return 1;
}
}
}