From 98d2a1e1fd12cb042bd8c71ab0064d50e04a9d67 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 4 Jan 2016 16:22:57 -0500 Subject: [PATCH] Delete account is no longer a GET request --- .../Controllers/Admin/AccountsController.php | 16 +++++++++------ app/Http/Routes/AdminRoutes.php | 4 ++-- resources/views/admin/accounts/view.blade.php | 20 +++++++++++++++++-- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Admin/AccountsController.php b/app/Http/Controllers/Admin/AccountsController.php index aa933baab..813162a78 100644 --- a/app/Http/Controllers/Admin/AccountsController.php +++ b/app/Http/Controllers/Admin/AccountsController.php @@ -47,13 +47,17 @@ class AccountsController extends Controller ]); } - public function getDelete(Request $request, $id) + public function deleteView(Request $request, $id) { - $user = new UserRepository; - $user->delete($id); - - Alert::success('An account has been successfully deleted.')->flash(); - return redirect()->route('admin.accounts'); + try { + User::findOrFail($id)->delete(); + return response(null, 204); + } catch(\Exception $ex) { + Log::error($ex); + return response()->json([ + 'error' => 'An error occured while attempting to delete this user.' + ], 500); + } } public function postNew(Request $request) diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index b65d665ab..e02ca4ebe 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -55,8 +55,8 @@ class AdminRoutes { ]); // Delete an Account Matching an ID - $router->get('/delete/{id}', [ - 'uses' => 'Admin\AccountsController@getDelete' + $router->delete('/view/{id}', [ + 'uses' => 'Admin\AccountsController@deleteView' ]); }); diff --git a/resources/views/admin/accounts/view.blade.php b/resources/views/admin/accounts/view.blade.php index 65e390f1f..849760352 100644 --- a/resources/views/admin/accounts/view.blade.php +++ b/resources/views/admin/accounts/view.blade.php @@ -61,8 +61,8 @@ {!! csrf_field() !!} - - + + @@ -150,6 +150,22 @@ $(document).ready(function(){ }); return false; }); + $('button[data-action="deleteUser"]').click(function (event) { + event.preventDefault(); + $.ajax({ + method: 'DELETE', + url: '/admin/accounts/view/{{ $user->id }}', + headers: { + 'X-CSRF-TOKEN': '{{ csrf_token() }}' + } + }).done(function (data) { + alert('Account was successfully deleted from the system.'); + window.location = '/admin/accounts'; + }).fail(function (jqXHR) { + console.error(jqXHR); + alert('An error occured: ' + jqXHR.JSONResponse.error); + }) + }) }); @endsection