From 446f9f297e54f62b273f3373616051e8f011843f Mon Sep 17 00:00:00 2001 From: Vincent BRIDIER Date: Mon, 2 Aug 2021 17:22:56 +0200 Subject: [PATCH] - Add customer delete on BO --- app/Http/Controllers/CustomerController.php | 16 +++++++++ app/Http/Requests/CustomerDeleteRequest.php | 38 +++++++++++++++++++++ resources/views/customers/index.blade.php | 18 ++++++++++ resources/views/customers/show.blade.php | 14 ++++++++ routes/web.php | 1 + 5 files changed, 87 insertions(+) create mode 100644 app/Http/Requests/CustomerDeleteRequest.php diff --git a/app/Http/Controllers/CustomerController.php b/app/Http/Controllers/CustomerController.php index 620dc7e..8b87ebe 100644 --- a/app/Http/Controllers/CustomerController.php +++ b/app/Http/Controllers/CustomerController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Http\Requests\CustomerDeleteRequest; use App\Models\Brand; use App\Models\Customer; use App\Models\CustomerInfos; @@ -44,4 +45,19 @@ class CustomerController extends Controller return response()->json($customers); } + + public function delete(CustomerDeleteRequest $request, Customer $customer, Brand $brand) + { + $nbBrand = $customer->infos()->count(); + + $customer->infos()->where('brand_id', $brand->id)->delete(); + $customer->logins()->where('brand_id', $brand->id)->delete(); + $customer->optInHistory()->where('brand_id', $brand->id)->delete(); + + if ($nbBrand === 1) { + $customer->delete(); + } + + return redirect()->route('customers')->with('success', "Compte supprimé."); + } } diff --git a/app/Http/Requests/CustomerDeleteRequest.php b/app/Http/Requests/CustomerDeleteRequest.php new file mode 100644 index 0000000..535e54e --- /dev/null +++ b/app/Http/Requests/CustomerDeleteRequest.php @@ -0,0 +1,38 @@ + 'exists:customers,id|required', + 'brand' => 'exists:brands,id|required' + ]; + } + + protected function prepareForValidation() + { + $this->merge(['customer' => $this->route('customer')->id, 'brand' => $this->route('brand')->id]); + } +} diff --git a/resources/views/customers/index.blade.php b/resources/views/customers/index.blade.php index a8ee99d..ba4ad56 100644 --- a/resources/views/customers/index.blade.php +++ b/resources/views/customers/index.blade.php @@ -1,5 +1,23 @@ @extends('layout.app') @section('content') +@if (session()->has('success')) +
+
+

{{ session()->get('success') }}

+
+
+@endif +@if ($errors->any()) +
+
+
    + @foreach ($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+
+@endif @endsection diff --git a/resources/views/customers/show.blade.php b/resources/views/customers/show.blade.php index d740c94..2005343 100644 --- a/resources/views/customers/show.blade.php +++ b/resources/views/customers/show.blade.php @@ -5,6 +5,15 @@

Informations du client {{ $customerInfos->firstname }} {{ $customerInfos->lastname }} - Marque {{ $customerInfos->brand->name }}

Retour + @if ($errors->any()) +
+ +
+ @endif @@ -81,4 +90,9 @@
+
+ @csrf + @method('DELETE') + +
@endsection diff --git a/routes/web.php b/routes/web.php index aa3628e..0ddef6b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -35,6 +35,7 @@ Route::middleware(['auth'])->group(function () { Route::get('/customers', [CustomerController::class, 'index'])->name('customers'); Route::get('/customers/filter/{texte}/{brandId?}', [CustomerController::class, 'filter'])->name('filter-customer'); Route::get('/customer/{id}/{brandId}', [CustomerController::class, 'show'])->name('show-customer'); + Route::delete('/customer/{customer}/{brand}', [CustomerController::class, 'delete'])->name('customers.delete'); // Single optins Route::get('/single-optins', [SingleOptinsController::class, 'index'])->name('single-optins'); Route::get('/single-optins/filter/{texte}/{brandId?}', [SingleOptinsController::class, 'filter'])->name('filter-single-optins');