mastercut/app/Http/Controllers/Admin/Manage/InventoryController.php

75 lines
2.3 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Admin\Manage;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\InventoryRequest;
use App\Models\Inventory;
use App\Traits\DeleteAttachment;
use App\Traits\UploadAttachment;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class InventoryController extends Controller
{
use DeleteAttachment, UploadAttachment;
public function index(): View
{
return view('pages.admin.manage.inventory', [
'pageTitle' => 'Barang',
'inventory' => Inventory::with(['user', 'user.biography', 'attachment'])
->barbershop()
->latest()
->get(),
]);
}
public function store(InventoryRequest $request): RedirectResponse
{
$validatedData = $request->validated();
DB::transaction(function () use ($validatedData) {
$newInventory = Inventory::create(array_merge($validatedData, [
'user_id' => Auth::id(),
'barbershop_id' => Auth::user()->barbershop_id,
]));
$this->uploadAttachment($validatedData['image'], 'inventory', Inventory::class, $newInventory->id, 'inventory');
});
notify()->success('Data berhasil ditambahkan', 'Berhasil');
return back();
}
public function update(Inventory $inventory, InventoryRequest $request): RedirectResponse
{
$validatedData = $request->validated();
DB::transaction(function () use ($inventory, $validatedData) {
$inventory->update($validatedData);
if (isset($validatedData['image'])) {
$this->uploadAttachment($validatedData['image'], 'inventory', Inventory::class, $inventory->id, 'inventory');
}
});
notify()->success('Data berhasil diubah', 'Berhasil');
return back();
}
public function delete(Inventory $inventory): RedirectResponse
{
DB::transaction(function () use ($inventory) {
$this->deleteAttachment('inventory', $inventory);
$inventory->delete();
});
notify()->success('Data berhasil dihapus', 'Berhasil');
return back();
}
}