Initial commit: Struktur aplikasi ilycats
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Food;
|
||||
use App\Models\Category;
|
||||
use App\Traits\LogsActivity;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class FoodController extends Controller
|
||||
{
|
||||
use LogsActivity;
|
||||
|
||||
/**
|
||||
* Tampilkan daftar stok makanan.
|
||||
*/
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$query = Food::with('category');
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$query->where('nama_makanan', 'like', '%' . $request->search . '%');
|
||||
}
|
||||
|
||||
$foods = $query->latest()->paginate(10);
|
||||
return view('pages.inventory.foods.index', compact('foods'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tampilkan form tambah.
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('pages.inventory.foods.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Simpan data makanan baru.
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'nama_makanan' => 'required|string|max:255',
|
||||
'qty' => 'required|numeric|min:0',
|
||||
'unit' => 'required|string|max:50',
|
||||
'expired_date' => 'nullable|date|after_or_equal:today',
|
||||
'foto_produk' => 'nullable|image|max:1024',
|
||||
]);
|
||||
|
||||
if ($request->hasFile('foto_produk')) {
|
||||
$validated['foto_produk'] = $request->file('foto_produk')->store('foods', 'public');
|
||||
}
|
||||
|
||||
// Simpan data & tangkap objeknya untuk logging
|
||||
$food = Food::create($validated);
|
||||
|
||||
// Tambahkan Log Aktivitas (Sangat penting untuk audit stok)
|
||||
$this->logAction('CREATED_FOOD', $food, "Menambah stok pakan baru: {$food->nama_makanan}");
|
||||
|
||||
// FIX: Tambahkan .index agar tidak error RouteNotFound
|
||||
return redirect()->route('inventory.foods.index')->with('success', 'Stok pakan berhasil ditambahkan.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tampilkan form edit.
|
||||
*/
|
||||
public function edit(Food $food): View
|
||||
{
|
||||
$categories = Category::all();
|
||||
return view('pages.inventory.foods.edit', compact('food', 'categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update data makanan.
|
||||
*/
|
||||
public function update(Request $request, Food $food): RedirectResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'nama_makanan' => 'required|string|max:255',
|
||||
'qty' => 'required|numeric|min:0',
|
||||
'unit' => 'required|string|max:50',
|
||||
'category_id' => 'nullable|exists:categories,id',
|
||||
'expired_date' => 'nullable|date|after_or_equal:today',
|
||||
'foto_produk' => 'nullable|image|max:1024',
|
||||
]);
|
||||
|
||||
if ($request->hasFile('foto_produk')) {
|
||||
if ($food->foto_produk) {
|
||||
Storage::disk('public')->delete($food->foto_produk);
|
||||
}
|
||||
$validated['foto_produk'] = $request->file('foto_produk')->store('foods', 'public');
|
||||
}
|
||||
|
||||
$food->update($validated);
|
||||
|
||||
$this->logAction('UPDATED_FOOD', $food, "Memperbarui stok pakan: {$food->nama_makanan}");
|
||||
|
||||
// FIX: Tambahkan .index
|
||||
return redirect()->route('inventory.foods.index')->with('success', 'Data stok pakan berhasil diperbarui.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Hapus data makanan.
|
||||
*/
|
||||
public function destroy(Food $food): RedirectResponse
|
||||
{
|
||||
if (auth()->user()->role !== 'admin') {
|
||||
abort(403, 'Hanya Admin yang dapat menghapus data stok pakan.');
|
||||
}
|
||||
|
||||
$foodName = $food->nama_makanan;
|
||||
|
||||
if ($food->foto_produk) {
|
||||
Storage::disk('public')->delete($food->foto_produk);
|
||||
}
|
||||
|
||||
// Log dilakukan SEBELUM delete
|
||||
$this->logAction('DELETED_FOOD', $food, "Menghapus data pakan: {$foodName}");
|
||||
|
||||
$food->delete();
|
||||
|
||||
// FIX: Tambahkan .index
|
||||
return redirect()->route('inventory.foods.index')->with('success', 'Stok pakan berhasil dihapus.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user