Permission fixes and washing reservation fixes

This commit is contained in:
frederikpyt 2020-07-29 11:53:37 +02:00
parent fc0f97b04e
commit 569ace008a
11 changed files with 73 additions and 127 deletions

View File

@ -1,12 +0,0 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class CalendarController extends Controller
{
// public function index()
}

View File

@ -1,95 +0,0 @@
<?php
//
//namespace App\Http\Controllers;
//
//use App\CalendarDate;
//use Illuminate\Http\Request;
//
//class CalendarDateController extends Controller
//{
// /**
// * Display a listing of the resource.
// *
// * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
// */
// public function index(Request $request)
// {
// $calendarDates = CalendarDate::query()->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)
// {
// //
// }
//}

View File

@ -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.
*

View File

@ -227,6 +227,7 @@ class UserController extends Controller
if(Auth::attempt($data)) {
//TODO: Implement home?
return redirect()->route("root.index");
}

View File

@ -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);
}
}

View File

@ -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'
];
}

View File

@ -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']);
});
}

View File

@ -15,13 +15,20 @@
@csrf
<label for="time">Tidspunkt:</label>
<input type="datetime-local" name="time" id="time" required>
<label for="machine">Vaskemaskine:</label>
<select name="machine" id="machine" required>
<label for="machine_id">Vaskemaskine:</label>
<select name="machine_id" id="machine_id" required>
<option disabled selected value> -- Vælg Vaskemaskine -- </option>
@foreach($machines as $machine)
<option value="{{ $machine->id }}">{{ $machine->name }}</option>
@endforeach
</select>
<label for="user_id">Bruger:</label>
<select name="user_id" id="user_id" required>
<option disabled selected value> -- Vælg Bruger -- </option>
@foreach($users as $user)
<option value="{{ $user->id }}">{{ ucfirst($user->name_first) }} {{ ucfirst($user->name_last) }}</option>
@endforeach
</select>
<input type="submit" class="btn btn-dark text-white" value="Opret">
</form>
@endsection

View File

@ -28,7 +28,7 @@
</tr>
@foreach($reservations as $reservation)
<tr>
<td>{{ \App\WashingMachine::query()->find($reservation->machine)->name }}</td>
<td>{{ \App\WashingMachine::query()->find($reservation->machine_id)->name }}</td>
<td>{{ $reservation->time }}</td>
<td><a href="{{ route('washing-reservations.edit', ['washing_reservation' => $reservation]) }}"><img class="w-100" src="{{ asset('/images/icons/pencil-dark.svg') }}" alt="Update"></a></td>
<td><form method="post" action="{{ route('washing-reservations.destroy', ['washing_reservation' => $reservation]) }}" class="w-100 nostyle">

View File

@ -1,5 +1,5 @@
@extends("app.layout.base")
@extends("app.layout.header")
@section("title")
Booking Liste
@endsection
@ -27,6 +27,7 @@
</div>
<form method="post" action="{{ route("washing-reservations.store") }}" id="event-container" class="events-container">
@csrf
<input type="hidden" value="{{auth()->user()->id}}" name="user_id">
<p>
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!
</p>
@ -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 = "";
}
});
}

View File

@ -1,5 +1,4 @@
@extends("app.layout.base")
@extends("app.layout.header")
@section("title")
Booking Liste
@endsection