From fc3f77af2f69f5384df0919dbe57fccbbea27678 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Sun, 24 Nov 2024 16:55:54 +0700 Subject: [PATCH] feat(customer): crud,route model binding,js,menjadikan contact_number menjadi nullable --- .../Admin/Master/CustomerController.php | 92 +++++++++++++ .../Requests/Admin/Master/CustomerRequest.php | 31 +++++ app/Providers/RouteServiceProvider.php | 2 + ...24_11_18_172439_create_customers_table.php | 2 +- public/assets/admin/js/custom/customer.js | 29 ++++ public/assets/admin/js/custom/user.js | 8 -- .../pages/admin/master/customers.blade.php | 126 ++++++++++++++++++ .../views/partials/admin/_sidebar.blade.php | 7 + routes/web.php | 8 ++ 9 files changed, 296 insertions(+), 9 deletions(-) create mode 100644 app/Http/Controllers/Admin/Master/CustomerController.php create mode 100644 app/Http/Requests/Admin/Master/CustomerRequest.php create mode 100644 public/assets/admin/js/custom/customer.js create mode 100644 resources/views/pages/admin/master/customers.blade.php diff --git a/app/Http/Controllers/Admin/Master/CustomerController.php b/app/Http/Controllers/Admin/Master/CustomerController.php new file mode 100644 index 0000000..92a9d01 --- /dev/null +++ b/app/Http/Controllers/Admin/Master/CustomerController.php @@ -0,0 +1,92 @@ + 'Pelanggan', + 'customers' => Customer::latest()->get(), + ]); + } + + public function store(CustomerRequest $request): RedirectResponse + { + $validatedData = $request->validated(); + + Customer::create(array_merge($validatedData, [ + 'user_id' => Auth::id(), + 'barbershop_id' => Auth::user()->barbershop_id, + ])); + + notify()->success('Data berhasil ditambahkan', 'Berhasil'); + + return redirect('dashboard/master/customers'); + } + + public function update(Customer $customer, CustomerRequest $request): RedirectResponse + { + $validatedData = $request->validated(); + + $customer->update($validatedData); + + notify()->success('Data berhasil ditambahkan', 'Berhasil'); + + return redirect('dashboard/master/customers'); + } + + public function delete(Customer $customer): RedirectResponse + { + $customer->delete(); + + notify()->success('Data berhasil dihapus', 'Berhasil'); + + return back(); + } + + public function generateActionButtons(Customer $customer): string + { + $editUrl = route('master.customer.edit', encryptId($customer->id)); + $deleteUrl = route('master.customer.delete', encryptId($customer->id)); + + return ' +
+ +
'; + } +} diff --git a/app/Http/Requests/Admin/Master/CustomerRequest.php b/app/Http/Requests/Admin/Master/CustomerRequest.php new file mode 100644 index 0000000..c769e64 --- /dev/null +++ b/app/Http/Requests/Admin/Master/CustomerRequest.php @@ -0,0 +1,31 @@ + ['required', 'string', 'max:30'], + 'contact_number' => ['nullable', 'string', 'regex:/^\+?[0-9]{10,15}$/'], + ]; + } + + protected function failedValidation(Validator $validator): void + { + $this->handleValidationFailure($validator); + } +} diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index 33a0fc4..12c9ffd 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -3,6 +3,7 @@ namespace App\Providers; use App\Models\Barbershop; +use App\Models\Customer; use App\Models\User; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; @@ -24,5 +25,6 @@ public function boot(): void { Route::bind('barbershop', fn (string $barbershop) => Barbershop::findOrFail(decryptId($barbershop))); Route::bind('user', fn (string $user) => User::findOrFail(decryptId($user))); + Route::bind('customer', fn (string $customer) => Customer::findOrFail(decryptId($customer))); } } diff --git a/database/migrations/2024_11_18_172439_create_customers_table.php b/database/migrations/2024_11_18_172439_create_customers_table.php index 47ff146..ded270e 100644 --- a/database/migrations/2024_11_18_172439_create_customers_table.php +++ b/database/migrations/2024_11_18_172439_create_customers_table.php @@ -16,7 +16,7 @@ public function up(): void $table->foreignId('user_id')->constrained()->cascadeOnDelete(); $table->foreignId('barbershop_id')->constrained('barbershop')->cascadeOnDelete(); $table->string('name', 30); - $table->string('contact_number', 15); + $table->string('contact_number', 15)->nullable(); $table->timestamps(); $table->softDeletes(); }); diff --git a/public/assets/admin/js/custom/customer.js b/public/assets/admin/js/custom/customer.js new file mode 100644 index 0000000..a74c4b0 --- /dev/null +++ b/public/assets/admin/js/custom/customer.js @@ -0,0 +1,29 @@ +$(document).ready(function () { + $("#createModal").on("show.bs.modal", function (event) { + const button = $(event.relatedTarget); + const data = { + modalTitle: button.data("modal-title"), + url: button.data("url"), + method: button.data("method"), + + name: button.data("name"), + contactNumber: button.data("contact-number"), + }; + + $(".modal-title").text(data.modalTitle); + $("#form").attr('action', data.url); + $("#form-method").val(data.method); + + if (data.method === 'put') { + $("#name").val(data.name); + $("#contact_number").val(data.contactNumber); + } + }); + + $('#createModal').on('hidden.bs.modal', function () { + const method = $("#form-method").val(); + if (method === 'put') { + $("#name").val(''); + } + }); +}); diff --git a/public/assets/admin/js/custom/user.js b/public/assets/admin/js/custom/user.js index f4420ab..27f0fbf 100644 --- a/public/assets/admin/js/custom/user.js +++ b/public/assets/admin/js/custom/user.js @@ -35,12 +35,4 @@ $(document).ready(function () { $('#user-gender').html(genderBadge); $('#user-sop').text(data.sop); }); - - $('#showModal').on('hidden.bs.modal', function () { - const method = $("#form-method").val(); - if (method === 'put') { - $("#name").val(''); - $("#alias").val(''); - } - }); }); diff --git a/resources/views/pages/admin/master/customers.blade.php b/resources/views/pages/admin/master/customers.blade.php new file mode 100644 index 0000000..cb7e4b3 --- /dev/null +++ b/resources/views/pages/admin/master/customers.blade.php @@ -0,0 +1,126 @@ +@extends('layouts.admin') +@section('styles') + +@endsection +@section('app') +
+
+
+
+ +
+
+
+
+ + + + + + + + + + + + @foreach ($customers as $customer) + + + + + + + + @endforeach + +
NoNamaKontakDibuatAksi
{{ $loop->iteration }}{{ $customer->name }}{{ $customer->contact_number ?? '-' }}{{ formatDateIndo($customer->created_at) }} +
    +
  • + + + +
  • +
  • +
    + @csrf + @method('delete') + + + +
    +
  • +
+
+
+
+
+
+
+ + +
+@endsection +@section('afterScripts') + + + + + +@endsection diff --git a/resources/views/partials/admin/_sidebar.blade.php b/resources/views/partials/admin/_sidebar.blade.php index 3b93d1c..4739627 100644 --- a/resources/views/partials/admin/_sidebar.blade.php +++ b/resources/views/partials/admin/_sidebar.blade.php @@ -68,6 +68,13 @@ Pengguna +
diff --git a/routes/web.php b/routes/web.php index cd7abee..d26990b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -3,6 +3,7 @@ use App\Http\Controllers\Admin\Auth\LoginController; use App\Http\Controllers\Admin\Dashboard\OverviewController; use App\Http\Controllers\Admin\Master\BarbershopController; +use App\Http\Controllers\Admin\Master\CustomerController; use App\Http\Controllers\Admin\Master\UserController; use Illuminate\Support\Facades\Route; @@ -40,5 +41,12 @@ Route::put('reset-password/{user}', [UserController::class, 'resetPassword'])->name('master.user.resetPassword'); Route::put('activation/{user}', [UserController::class, 'activation'])->name('master.user.activation'); }); + + Route::get('/customers', [CustomerController::class, 'index'])->name('master.customers'); + Route::prefix('customer')->group(function () { + Route::post('store', [CustomerController::class, 'store'])->name('master.customer.store'); + Route::put('update/{customer}', [CustomerController::class, 'update'])->name('master.customer.update'); + Route::delete('delete/{customer}', [CustomerController::class, 'delete'])->name('master.customer.delete'); + }); }); });