v0.5.3 - Added Locations and a location_id to washing machines

This commit is contained in:
frederikpyt
2020-08-06 08:37:16 +02:00
parent 9b631843b0
commit 5452711665
22 changed files with 424 additions and 40 deletions
@@ -0,0 +1,133 @@
<?php
namespace App\Http\Controllers;
use App\Location;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class LocationController extends Controller
{
public function __construct()
{
$this->middleware([ "auth" ]);
$this->middleware([ "check.auth:locations.list" ])->only("index");
$this->middleware([ "check.auth:locations.show" ])->only("show");
$this->middleware([ "check.auth:locations.create" ])->only("create", "store");
$this->middleware([ "check.auth:locations.edit" ])->only("edit", "update");
$this->middleware([ "check.auth:locations.delete" ])->only("delete");
}
/**
* Display a listing of the resource.
*
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$locations = Location::query()->paginate($request->input("limit", 20));
return Response::detect("locations.index", [ "locations" => $locations ]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return Response::detect("locations.create");
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data = $request->validate([
"name" => "required",
]);
$location = new Location($data);
$locations = Location::query()->where('name', '=', $request->name)->get();
// If there already is a washing machine with that name, then don't add it
if (count($locations) > 0)
return redirect()->route("locations.store")->with('NameExists', '<p><b>Der findes allerede en lokation med det navn!</b></p>');
else { // Else - Add it
$location->save();
$locations = Location::query()->paginate($request->input("limit", 20));
return redirect()->route("locations.index", ['locations' => $locations]);
}
}
/**
* Display the specified resource.
*
* @param \App\Location $location
* @return \Illuminate\Http\Response
*/
public function show(Location $location)
{
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Location $location
* @return \Illuminate\Http\Response
*/
public function edit(Location $location)
{
return Response::detect("locations.edit", [ "location" => $location] );
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Location $location
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$data = $request->validate([
"name" => "required",
]);
$location = Location::find($id);
$allMachines = Location::query()->where('name', '=', $request->name)->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("locations.store")->with('NameExists', '<p><b>Der findes allerede en lokation med det navn!</b></p>');
else { // Else - Change the name
$location->update($data);
$location->save();
$locations = Location::query()->paginate($request->input("limit", 20));
return redirect()->route("locations.index", ["locations" => $locations]);
}
}
/**
* Remove the specified resource from storage.
*
* @param \App\Location $location
* @return \Illuminate\Http\Response
*/
public function destroy(Location $location)
{
}
}
@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Location;
use App\WashingReservation;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
@@ -44,7 +45,9 @@ class WashingMachineController extends Controller
*/
public function create()
{
return Response::detect("washing-machines.create");
$locations = Location::all();
return Response::detect("washing-machines.create", ["locations" => $locations] );
}
/**
@@ -56,12 +59,13 @@ class WashingMachineController extends Controller
public function store(Request $request)
{
$data = $request->validate([
"name" => "required"
"name" => "required",
"location_id" => "required"
]);
$machine = new WashingMachine($data);
$allMachines = WashingMachine::query()->where('name', '=', $request->name)->get();
$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)
@@ -81,11 +85,7 @@ class WashingMachineController extends Controller
*/
public function show($id)
{
$machine = WashingMachine::find($id);
return Response::detect("washing-machines.show", [
"machine" => $machine
]);
}
/**
@@ -97,9 +97,11 @@ class WashingMachineController extends Controller
public function edit($id)
{
$machine = WashingMachine::find($id);
$locations = Location::all();
return Response::detect("washing-machines.edit", [
"machine" => $machine
"machine" => $machine,
"locations" => $locations
]);
}
@@ -113,13 +115,14 @@ class WashingMachineController extends Controller
public function update(Request $request, $id)
{
$data = $request->validate([
"name" => "required"
"name" => "required",
"location_id" => "required",
]);
$machine = WashingMachine::find($id);
$allMachines = WashingMachine::query()->where('name', '=', $request->name)->where('id', '!=', $id)->get();
$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)
@@ -103,10 +103,7 @@ class WashingReservationController extends Controller
*/
public function edit($id)
{
$reservation = WashingReservation::query()->find($id);
$machines = WashingMachine::all();
return Response::detect("washing-reservations.edit", ['washing_reservation' => $reservation, 'machines' => $machines ]);
}
/**
@@ -118,26 +115,7 @@ class WashingReservationController extends Controller
*/
public function update(Request $request, $id)
{
$data = $request->validate([
"time" => "required",
"machine" => "required"
]);
$machineReservation = WashingReservation::find($id);
$machineReservation->update($data);
$saved = $machineReservation->save();
if(!$saved){
return Response::detect("washing-reservations.update", [
"washing_reservation" => $machineReservation
]);
}else{
$reservations = WashingReservation::query()->paginate($request->query("limit", 20));
return Response::detect("washing-reservations.index", [ "reservations" => $reservations]);
}
}
/**
@@ -246,4 +224,3 @@ class WashingReservationController extends Controller
return Response::detect("washing-reservations.index", [ "reservations" => $reservations]);
}
}