117 lines
4.1 KiB
PHP
117 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Master;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\Master\BarbershopRequest;
|
|
use App\Models\Barbershop;
|
|
use App\Traits\UploadAttachment;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class BarbershopController extends Controller
|
|
{
|
|
use UploadAttachment;
|
|
|
|
public function index(): View
|
|
{
|
|
return view('pages.admin.master.barbershop.index', [
|
|
'pageTitle' => 'Barbershop',
|
|
'barbershop' => Barbershop::latest()->get(),
|
|
]);
|
|
}
|
|
|
|
public function create(): View
|
|
{
|
|
return view('pages.admin.master.barbershop.create', [
|
|
'pageTitle' => 'Tambah Barbershop',
|
|
]);
|
|
}
|
|
|
|
public function store(BarbershopRequest $request): RedirectResponse
|
|
{
|
|
$validatedData = $request->validated();
|
|
|
|
DB::transaction(function () use ($validatedData) {
|
|
$newBarbershop = Barbershop::create($validatedData);
|
|
$this->uploadAttachment($validatedData['image'], 'barbershop', Barbershop::class, $newBarbershop->id);
|
|
});
|
|
|
|
notify()->success('Data berhasil ditambahkan', 'Berhasil');
|
|
|
|
return redirect('dashboard/master/barbershop');
|
|
}
|
|
|
|
public function edit(Barbershop $barbershop): View
|
|
{
|
|
return view('pages.admin.master.barbershop.edit', [
|
|
'pageTitle' => 'Edit Barbershop',
|
|
'barbershop' => $barbershop,
|
|
]);
|
|
}
|
|
|
|
public function update(Barbershop $barbershop, BarbershopRequest $request): RedirectResponse
|
|
{
|
|
$validatedData = $request->validated();
|
|
|
|
DB::transaction(function () use ($barbershop, $validatedData) {
|
|
$barbershop->update($validatedData);
|
|
$this->uploadAttachment($validatedData['image'], 'barbershop', Barbershop::class, $barbershop->id);
|
|
});
|
|
|
|
notify()->success('Data berhasil ditambahkan', 'Berhasil');
|
|
|
|
return redirect('dashboard/master/barbershop');
|
|
}
|
|
|
|
public function delete(Barbershop $barbershop): RedirectResponse
|
|
{
|
|
if (Auth::user()->barbershop_id === $barbershop->id) {
|
|
notify()->info('Anda sedang berada di barbershop ini, silakan pilih barbershop lain lalu Anda dapat menghapusnya', 'Informasi');
|
|
|
|
return back();
|
|
}
|
|
$barbershop->delete();
|
|
|
|
notify()->success('Data berhasil dihapus', 'Berhasil');
|
|
|
|
return redirect('dashboard/master/barbershop');
|
|
}
|
|
|
|
public function generateActionButtons(Barbershop $barbershop): string
|
|
{
|
|
$editUrl = route('master.barbershop.edit', encryptId($barbershop->id));
|
|
$deleteUrl = route('master.barbershop.delete', encryptId($barbershop->id));
|
|
|
|
return '
|
|
<div class="nk-tb-col nk-tb-col-tools">
|
|
<ul class="nk-tb-actions gx-1 my-n1">
|
|
<li>
|
|
<div class="drodown">
|
|
<a href="#" class="dropdown-toggle btn btn-icon btn-trigger me-n1" data-bs-toggle="dropdown"><em class="icon ni ni-more-h"></em></a>
|
|
<div class="dropdown-menu dropdown-menu-end">
|
|
<ul class="link-list-opt no-bdr">
|
|
<li>
|
|
<a href="'.$editUrl.'">
|
|
<em class="icon ni ni-edit"></em>
|
|
<span>Edit</span>
|
|
</a>
|
|
</li>
|
|
<li>
|
|
<form action="'.$deleteUrl.'" method="post">
|
|
'.csrf_field().'
|
|
'.method_field('delete').'
|
|
<a href="javascript:void(0)" class="delete"><em class="icon ni ni-trash"></em><span>Hapus</span></a>
|
|
</form>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</li>
|
|
</ul>
|
|
</div>';
|
|
}
|
|
}
|