From 569ace008a8966d4f099e1224ded50b9669c4aa8 Mon Sep 17 00:00:00 2001 From: frederikpyt Date: Wed, 29 Jul 2020 11:53:37 +0200 Subject: [PATCH] Permission fixes and washing reservation fixes --- .../Http/Controllers/CalendarController.php | 12 --- .../Controllers/CalendarDateController.php | 95 ------------------- .../app/Http/Controllers/RolesController.php | 11 +++ .../app/Http/Controllers/UserController.php | 1 + .../WashingReservationController.php | 23 +++-- skolehjem/app/WashingReservation.php | 2 +- ..._08_085447_create_washing_reservations.php | 7 +- .../washing-reservations/create.blade.php | 11 ++- .../washing-reservations/index.blade.php | 2 +- .../app/washing-reservations/create.blade.php | 35 +++++-- .../app/washing-reservations/index.blade.php | 1 - 11 files changed, 73 insertions(+), 127 deletions(-) delete mode 100644 skolehjem/app/Http/Controllers/CalendarController.php delete mode 100644 skolehjem/app/Http/Controllers/CalendarDateController.php diff --git a/skolehjem/app/Http/Controllers/CalendarController.php b/skolehjem/app/Http/Controllers/CalendarController.php deleted file mode 100644 index 33d2d34..0000000 --- a/skolehjem/app/Http/Controllers/CalendarController.php +++ /dev/null @@ -1,12 +0,0 @@ -paginate($request->input("limit", 20)); -// -// return view("calendar-date.index", [ -// "calendarDates" => $calendarDates -// ]); -// } -// -// /** -// * Show the form for creating a new resource. -// * -// * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View -// */ -// public function create() -// { -// return view("calendar-date.create"); -// } -// -// /** -// * Store a newly created resource in storage. -// * -// * @param \Illuminate\Http\Request $request -// * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View -// */ -// public function store(Request $request) -// { -// $fields = $request->validate([ -// "" => "" -// ]); -// -// $calendarDate = new CalendarDate() -// -// return view("calendar-date.store"); -// } -// -// /** -// * Display the specified resource. -// * -// * @param int $id -// * @return \Illuminate\Http\Response -// */ -// public function show($id) -// { -// // -// } -// -// /** -// * Show the form for editing the specified resource. -// * -// * @param int $id -// * @return \Illuminate\Http\Response -// */ -// public function edit($id) -// { -// // -// } -// -// /** -// * Update the specified resource in storage. -// * -// * @param \Illuminate\Http\Request $request -// * @param int $id -// * @return \Illuminate\Http\Response -// */ -// public function update(Request $request, $id) -// { -// // -// } -// -// /** -// * Remove the specified resource from storage. -// * -// * @param int $id -// * @return \Illuminate\Http\Response -// */ -// public function destroy($id) -// { -// // -// } -//} diff --git a/skolehjem/app/Http/Controllers/RolesController.php b/skolehjem/app/Http/Controllers/RolesController.php index 4b09588..67f4154 100644 --- a/skolehjem/app/Http/Controllers/RolesController.php +++ b/skolehjem/app/Http/Controllers/RolesController.php @@ -9,6 +9,17 @@ use Spatie\Permission\Models\Role; class rolesController extends Controller { + public function __construct() + { + $this->middleware([ "auth" ]); + + $this->middleware([ "check.auth:roles.list" ])->only("index"); + $this->middleware([ "check.auth:roles.show" ])->only("show"); + $this->middleware([ "check.auth:roles.create" ])->only("create", "store"); + $this->middleware([ "check.auth:roles.edit" ])->only("edit", "update"); + $this->middleware([ "check.auth:roles.delete" ])->only("delete"); + } + /** * Display a listing of the resource. * diff --git a/skolehjem/app/Http/Controllers/UserController.php b/skolehjem/app/Http/Controllers/UserController.php index 53bbd96..225fd19 100644 --- a/skolehjem/app/Http/Controllers/UserController.php +++ b/skolehjem/app/Http/Controllers/UserController.php @@ -227,6 +227,7 @@ class UserController extends Controller if(Auth::attempt($data)) { //TODO: Implement home? + return redirect()->route("root.index"); } diff --git a/skolehjem/app/Http/Controllers/WashingReservationController.php b/skolehjem/app/Http/Controllers/WashingReservationController.php index 29f5881..d363c54 100644 --- a/skolehjem/app/Http/Controllers/WashingReservationController.php +++ b/skolehjem/app/Http/Controllers/WashingReservationController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\WashingMachine; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\View\Factory; +use Illuminate\Foundation\Auth\User; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Http\Response; @@ -46,7 +47,8 @@ class WashingReservationController extends Controller public function create() { $machines = WashingMachine::all(); - return Response::detect("washing-reservations.create", [ 'machines' => $machines ]); + $users = User::all(); + return Response::detect("washing-reservations.create", [ 'machines' => $machines, 'users' => $users ]); } /** @@ -59,7 +61,8 @@ class WashingReservationController extends Controller { $data = $request->validate([ "time" => "required", - "machine" => "required" + "machine_id" => "required|unique:washing_reservations,machine_id,NULL,id,time,' . $request->time", + "user_id" => "required" ]); $machineReservation = new WashingReservation($data); @@ -67,14 +70,14 @@ class WashingReservationController extends Controller $saved = $machineReservation->save(); - if(!$saved){ + if (!$saved) { return Response::detect("washing-reservations.store", [ "washing_reservation" => $machineReservation ]); - }else{ + } else { $reservations = WashingReservation::query()->paginate($request->input("limit", 20)); - return Response::detect("washing-reservations.index", [ "reservations" => $reservations]); + return Response::detect("washing-reservations.index", ["reservations" => $reservations]); } } @@ -160,7 +163,15 @@ class WashingReservationController extends Controller $machines = WashingMachine::all(); - $output = json_encode(['date' => $date, 'washingmachines' => $machines]); + $reservations = WashingReservation::query()->where("time", "LIKE", substr($date, 1, strpos($date, "T"))."%")->get(); + + $times = []; + + foreach ($reservations as $reservation){ + array_push($times, $reservation->time); + } + //2020-07-28% + $output = json_encode(['date' => $date, 'washingmachines' => $machines, 'unavailable_times' => $times]); return Response($output); } } diff --git a/skolehjem/app/WashingReservation.php b/skolehjem/app/WashingReservation.php index 8afd30c..c46a739 100644 --- a/skolehjem/app/WashingReservation.php +++ b/skolehjem/app/WashingReservation.php @@ -14,6 +14,6 @@ class WashingReservation extends Model { //protected variable which contains name of database field(s) to be filled. protected $fillable = [ - 'time', 'machine' + 'time', 'machine_id', 'user_id' ]; } diff --git a/skolehjem/database/migrations/2020_06_08_085447_create_washing_reservations.php b/skolehjem/database/migrations/2020_06_08_085447_create_washing_reservations.php index 3ac0a01..a1d3b14 100644 --- a/skolehjem/database/migrations/2020_06_08_085447_create_washing_reservations.php +++ b/skolehjem/database/migrations/2020_06_08_085447_create_washing_reservations.php @@ -19,8 +19,11 @@ class CreateWashingReservations extends Migration $table->id(); $table->timestamp("time"); $table->timestamps(); - $table->unsignedBigInteger('machine'); - $table->foreign("machine")->references('id')->on('washing_machines'); + $table->unsignedBigInteger('machine_id'); + $table->foreign("machine_id")->references('id')->on('washing_machines'); + $table->unsignedBigInteger('user_id'); + $table->foreign("user_id")->references('id')->on('users'); + $table->unique(['time', 'machine_id']); }); } diff --git a/skolehjem/resources/views/admin/washing-reservations/create.blade.php b/skolehjem/resources/views/admin/washing-reservations/create.blade.php index fb0308a..841d82e 100644 --- a/skolehjem/resources/views/admin/washing-reservations/create.blade.php +++ b/skolehjem/resources/views/admin/washing-reservations/create.blade.php @@ -15,13 +15,20 @@ @csrf - - @foreach($machines as $machine) @endforeach + + @endsection diff --git a/skolehjem/resources/views/admin/washing-reservations/index.blade.php b/skolehjem/resources/views/admin/washing-reservations/index.blade.php index b26f834..e9cbf21 100644 --- a/skolehjem/resources/views/admin/washing-reservations/index.blade.php +++ b/skolehjem/resources/views/admin/washing-reservations/index.blade.php @@ -28,7 +28,7 @@ @foreach($reservations as $reservation) - {{ \App\WashingMachine::query()->find($reservation->machine)->name }} + {{ \App\WashingMachine::query()->find($reservation->machine_id)->name }} {{ $reservation->time }} Update
diff --git a/skolehjem/resources/views/app/washing-reservations/create.blade.php b/skolehjem/resources/views/app/washing-reservations/create.blade.php index 19c252f..7734e62 100644 --- a/skolehjem/resources/views/app/washing-reservations/create.blade.php +++ b/skolehjem/resources/views/app/washing-reservations/create.blade.php @@ -1,5 +1,5 @@ @extends("app.layout.base") -@extends("app.layout.header") + @section("title") Booking Liste @endsection @@ -27,6 +27,7 @@ @csrf +

Maskiner skal dynamisk opdateres alt efter om det er en fra bygning E eller en af de andre bygninger der vil vaske, da bygning E har egen vaskekælder!

@@ -112,7 +113,7 @@ axios({ method: 'get', - url: '/washing-reservationsapi', + url: '{{ route("washing-reservations.api") }}', params: { 'date': date } }).then(function (response) { var data = response.data; @@ -127,7 +128,7 @@ let select = document.createElement("select"); select.classList.add("events__title"); select.id = "washing-machines"; - select.name = "machine"; + select.name = "machine_id"; container.appendChild(span); container.appendChild(select); @@ -139,7 +140,7 @@ else { let span = document.createElement("span"); span.classList.add("events__title"); - span.innerText = "Tider"; + span.innerText = "Tilgængelige tider"; let select = document.createElement("select"); select.classList.add("events__title"); @@ -178,13 +179,33 @@ events.innerHTML = ""; for (let hour = 8; hour <= 20; hour++) { - + let value = data["date"].split("T")[0].slice(1, data["date"].split("T")[0].length) + "T" + prependZero(hour) + ":00"; let option = document.createElement("option"); - option.text = prependZero(hour) + ":00"; - option.value = data["date"].split("T")[0].slice(1, data["date"].split("T")[0].length) + "T" + prependZero(hour) + ":00"; + option.text = prependZero(hour) + ":00"; + option.value = value; + option.id = value; events.appendChild(option); } + + let unavailable_times = data["unavailable_times"]; + + unavailable_times.forEach(function (item, index) { + console.log(item); + document.getElementById(item).remove(); + }); + + if(events.childElementCount == 0){ + let option = document.createElement("option"); + option.disabled = "disabled"; + option.text = "Der er ingen tilgængelige tider"; + option.selected = "selected"; + events.appendChild(option); + + document.getElementById("create-reservation").disabled = "disabled"; + } else { + document.getElementById("create-reservation").disabled = ""; + } }); } diff --git a/skolehjem/resources/views/app/washing-reservations/index.blade.php b/skolehjem/resources/views/app/washing-reservations/index.blade.php index fd31075..8333731 100644 --- a/skolehjem/resources/views/app/washing-reservations/index.blade.php +++ b/skolehjem/resources/views/app/washing-reservations/index.blade.php @@ -1,5 +1,4 @@ @extends("app.layout.base") -@extends("app.layout.header") @section("title") Booking Liste @endsection