From 718d0e39f282073ab5f654651aec226448cdb842 Mon Sep 17 00:00:00 2001 From: Mateja Date: Mon, 16 Nov 2020 22:42:08 +0100 Subject: Finished. No errors. Go to testing. --- app/Http/Controllers/ReservationsController.php | 51 ++++++++++++++++++- public/js/activities.js | 2 +- public/js/reservations.js | 66 ++++++++++++++++++++++++- routes/api.php | 1 - routes/web.php | 2 + 5 files changed, 117 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/ReservationsController.php b/app/Http/Controllers/ReservationsController.php index 2a46343..c105433 100644 --- a/app/Http/Controllers/ReservationsController.php +++ b/app/Http/Controllers/ReservationsController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use function GuzzleHttp\json_encode; use function dd; use function redirect; @@ -167,6 +168,54 @@ class ReservationsController extends Controller public function update(Request $request) { - return Redirect::back(); + $rules = [ + 'action' => 'required', + 'id' => 'required|numeric', + 'qso' => 'required|numeric', + 'approved' => 'required', + 'specialCall' => 'required|alphanum', + 'fromTime' => 'required|date', + 'toTime' => 'required|date|after:fromTime', + 'frequencies' => 'required', + 'modes' => 'required', + 'operatorCall' => 'required|alphanum', + 'operatorName' => 'required', + 'operatorEmail' => 'required|email', + 'operatorPhone' => ['required', 'regex:/^[0-9 ]+$/'], + ]; + $validatedData = $request->validate($rules); + + $record = Reservation::findOrFail($request->id); + + if ($request->action == "update") { + $record->approved = filter_var($request->approved, FILTER_VALIDATE_BOOLEAN); + $record->qso = $request->qso; + $record->specialCall = $request->specialCall; + $record->fromTime = $request->fromTime; + $record->toTime = $request->toTime; + $record->frequencies = $request->frequencies; + $record->modes = $request->modes; + $record->operatorCall = $request->operatorCall; + $record->operatorName = $request->operatorName; + $record->operatorEmail = $request->operatorEmail; + $record->operatorPhone = $request->operatorPhone; + $record->saveOrFail(); + + return response()->json(['action' => $request->input('action')]); + + } else if ($request->action == "restore") { + $resp = $record->toArray(); + $resp['action'] = $request->input('action'); + + return response()->json($resp); + + } else if ($request->action == "delete") { + $record->delete(); + + return response()->json(['action' => $request->input('action')]); + + } else { + return response()->json(['action' => 'error']); + } } } diff --git a/public/js/activities.js b/public/js/activities.js index 94bee2d..6529e6b 100644 --- a/public/js/activities.js +++ b/public/js/activities.js @@ -17,7 +17,7 @@ function fillTable() { if (status === 'success') { if (data.data.length > 0) { tableData.empty(); - for (var i = 0, len = data.data.length; i < len; i++) { + for (i = 0, len = data.data.length; i < len; i++) { tr = '' + data.data[i].operatorCall + '' + '' + data.data[i].fromTime + '' + '' + data.data[i].toTime + '' + diff --git a/public/js/reservations.js b/public/js/reservations.js index 65b2e2e..781b835 100644 --- a/public/js/reservations.js +++ b/public/js/reservations.js @@ -17,7 +17,7 @@ function fillTable() { if (status === 'success') { if (data.data.length > 0) { tableData.empty(); - for (var i = 0, len = data.data.length; i < len; i++) { + for (i = 0, len = data.data.length; i < len; i++) { tr = ''; tr += '' + data.data[i].id + ''; if (data.data[i].approved === 1) @@ -56,5 +56,67 @@ function fillTable() { } function btnAction(action, btn) { - console.log(action); + trDom = btn.parentElement.parentElement; + trData = trDom.children; + + actionData = { + action: action, + id: trData[0].innerText, + approved: trData[1].firstElementChild.checked, + operatorCall: trData[2].innerText, + qso: trData[3].innerText, + fromTime: trData[4].innerText, + toTime: trData[5].innerText, + specialCall: trData[6].innerText, + frequencies: trData[7].innerText, + modes: trData[8].innerText, + operatorName: trData[9].innerText, + operatorEmail: trData[10].innerText, + operatorPhone: trData[11].innerText + }; + + if (actionData.action == 'delete') { + if (confirm("Are you sure you want to delete reservation #" + actionData.id + " made by " + actionData.operatorCall + "?") === true) + trDom.remove(); + else return; + } + + jQuery.post('/api/reservations', actionData, function (response, status) { + if (status === 'success') { + try { + // Handle various actions + if (response.action == "update") { + //document.getElementById("notice").innerText = "Record #" + actionData.id + " updated."; + console.log('Record #' + actionData.id + ' updated.'); + } else if (response.action == "restore") { + trData[1].firstElementChild.checked = response.approved == 1; + trData[2].innerText = response.operatorCall; + trData[3].innerText = response.qso; + trData[4].innerText = response.fromTime; + trData[5].innerText = response.toTime; + trData[6].innerText = response.specialCall; + trData[7].innerText = response.frequencies; + trData[8].innerText = response.modes; + trData[9].innerText = response.operatorName; + trData[10].innerText = response.operatorEmail; + trData[11].innerText = response.operatorPhone; + //document.getElementById("notice").innerText = "Record's #" + actionData.id + " data restored."; + //console.log('Record #' + actionData.id + ' data restored.'); + } else if (response.action == "delete") { + //document.getElementById("notice").innerText = "Record #" + actionData.id + " deleted."; + //console.log('Record #' + actionData.id + ' deleted.'); + } else { + //console.log("No action?"); + //console.log(data); + } + } catch { + //console.log(data); + alert("Bad input data!"); + } + } + else { + //console.log('AJAX error'); + alert("Bad input data!"); + } + }); } diff --git a/routes/api.php b/routes/api.php index 413ea33..d85ea7a 100644 --- a/routes/api.php +++ b/routes/api.php @@ -21,4 +21,3 @@ Route::middleware('auth:api')->get('/user', function (Request $request) { }); Route::post('activities', [ReservationsController::class, 'index']); -Route::post('reservations', [ReservationsController::class, 'update'])->middleware(['auth']); diff --git a/routes/web.php b/routes/web.php index 393b226..20437b1 100644 --- a/routes/web.php +++ b/routes/web.php @@ -43,6 +43,8 @@ Route::post('/special-calls/reserve', [ReservationsController::class, 'store'])- Route::get('/special-calls/reservations', [ReservationsController::class, 'edit'])->name('reservations')->middleware(['auth']); Route::post('/special-calls/reservations', [ReservationsController::class, 'edit'])->name('reservationsForm')->middleware(['auth']); +Route::post('/api/reservations', [ReservationsController::class, 'update'])->middleware(['auth']); + Route::get('/special-calls/add', [SpecialCallsController::class, 'create'])->name('addSign')->middleware(['auth']); Route::post('/special-calls/add', [SpecialCallsController::class, 'store'])->name('addSignForm')->middleware(['auth']); -- cgit v1.2.3