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') +| No | +Nama | +Kontak | +Dibuat | +Aksi | +
|---|---|---|---|---|
| {{ $loop->iteration }} | +{{ $customer->name }} | +{{ $customer->contact_number ?? '-' }} | +{{ formatDateIndo($customer->created_at) }} | +
+
|
+