Add controllers for Anggota, Buku, Kategori, Peminjaman, and Dashboard with CRUD operations
This commit is contained in:
76
app/Http/Controllers/AnggotaController.php
Normal file
76
app/Http/Controllers/AnggotaController.php
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Anggota;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Inertia\Inertia;
|
||||||
|
|
||||||
|
class AnggotaController extends Controller
|
||||||
|
{
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$anggotas = Anggota::latest()->paginate(10);
|
||||||
|
|
||||||
|
return Inertia::render('Anggota/Index', [
|
||||||
|
'anggotas' => $anggotas,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create()
|
||||||
|
{
|
||||||
|
return Inertia::render('Anggota/Create');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'nama' => 'required|string|max:255',
|
||||||
|
'email' => 'required|email|unique:anggotas',
|
||||||
|
'no_telepon' => 'nullable|string|max:15',
|
||||||
|
'alamat' => 'nullable|string',
|
||||||
|
'tanggal_bergabung' => 'required|date',
|
||||||
|
'status' => 'required|in:aktif,nonaktif',
|
||||||
|
]);
|
||||||
|
|
||||||
|
Anggota::create($validated);
|
||||||
|
|
||||||
|
return redirect()->route('anggota.index')->with('success', 'Anggota berhasil ditambahkan!');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(Anggota $anggota)
|
||||||
|
{
|
||||||
|
return Inertia::render('Anggota/Show', [
|
||||||
|
'anggota' => $anggota->load('peminjamans.buku'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function edit(Anggota $anggota)
|
||||||
|
{
|
||||||
|
return Inertia::render('Anggota/Edit', [
|
||||||
|
'anggota' => $anggota,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(Request $request, Anggota $anggota)
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'nama' => 'required|string|max:255',
|
||||||
|
'email' => 'required|email|unique:anggotas,email,' . $anggota->id,
|
||||||
|
'no_telepon' => 'nullable|string|max:15',
|
||||||
|
'alamat' => 'nullable|string',
|
||||||
|
'tanggal_bergabung' => 'required|date',
|
||||||
|
'status' => 'required|in:aktif,nonaktif',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$anggota->update($validated);
|
||||||
|
|
||||||
|
return redirect()->route('anggota.index')->with('success', 'Anggota berhasil diupdate!');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(Anggota $anggota)
|
||||||
|
{
|
||||||
|
$anggota->delete();
|
||||||
|
return redirect()->route('anggota.index')->with('success', 'Anggota berhasil dihapus!');
|
||||||
|
}
|
||||||
|
}
|
||||||
88
app/Http/Controllers/BukuController.php
Normal file
88
app/Http/Controllers/BukuController.php
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Buku;
|
||||||
|
use App\Models\Kategori;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Inertia\Inertia;
|
||||||
|
|
||||||
|
class BukuController extends Controller
|
||||||
|
{
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$bukus = Buku::with('kategori')
|
||||||
|
->latest()
|
||||||
|
->paginate(10);
|
||||||
|
|
||||||
|
return Inertia::render('Buku/Index', [
|
||||||
|
'bukus' => $bukus,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create()
|
||||||
|
{
|
||||||
|
return Inertia::render('Buku/Create', [
|
||||||
|
'kategoris' => Kategori::all(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'kategori_id' => 'required|exists:kategoris,id',
|
||||||
|
'judul' => 'required|string|max:255',
|
||||||
|
'pengarang' => 'required|string|max:255',
|
||||||
|
'penerbit' => 'required|string|max:255',
|
||||||
|
'tahun_terbit' => 'required|digits:4',
|
||||||
|
'isbn' => 'required|string|max:20|unique:bukus',
|
||||||
|
'stok' => 'required|integer|min:0',
|
||||||
|
'sinopsis' => 'nullable|string',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$validated['stok_tersedia'] = $validated['stok'];
|
||||||
|
|
||||||
|
Buku::create($validated);
|
||||||
|
|
||||||
|
return redirect()->route('buku.index')->with('success', 'Buku berhasil ditambahkan!');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(Buku $buku)
|
||||||
|
{
|
||||||
|
return Inertia::render('Buku/Show', [
|
||||||
|
'buku' => $buku->load('kategori', 'peminjamans.anggota'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function edit(Buku $buku)
|
||||||
|
{
|
||||||
|
return Inertia::render('Buku/Edit', [
|
||||||
|
'buku' => $buku,
|
||||||
|
'kategoris' => Kategori::all(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(Request $request, Buku $buku)
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'kategori_id' => 'required|exists:kategoris,id',
|
||||||
|
'judul' => 'required|string|max:255',
|
||||||
|
'pengarang' => 'required|string|max:255',
|
||||||
|
'penerbit' => 'required|string|max:255',
|
||||||
|
'tahun_terbit' => 'required|digits:4',
|
||||||
|
'isbn' => 'required|string|max:20|unique:bukus,isbn,' . $buku->id,
|
||||||
|
'stok' => 'required|integer|min:0',
|
||||||
|
'sinopsis' => 'nullable|string',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$buku->update($validated);
|
||||||
|
|
||||||
|
return redirect()->route('buku.index')->with('success', 'Buku berhasil diupdate!');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(Buku $buku)
|
||||||
|
{
|
||||||
|
$buku->delete();
|
||||||
|
return redirect()->route('buku.index')->with('success', 'Buku berhasil dihapus!');
|
||||||
|
}
|
||||||
|
}
|
||||||
28
app/Http/Controllers/DashboardController.php
Normal file
28
app/Http/Controllers/DashboardController.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Anggota;
|
||||||
|
use App\Models\Buku;
|
||||||
|
use App\Models\Peminjaman;
|
||||||
|
use App\Models\Kategori;
|
||||||
|
use Inertia\Inertia;
|
||||||
|
|
||||||
|
class DashboardController extends Controller
|
||||||
|
{
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
return Inertia::render('Dashboard', [
|
||||||
|
'stats' => [
|
||||||
|
'total_buku' => Buku::count(),
|
||||||
|
'total_anggota' => Anggota::where('status', 'aktif')->count(),
|
||||||
|
'total_peminjaman' => Peminjaman::where('status', 'dipinjam')->count(),
|
||||||
|
'total_terlambat' => Peminjaman::where('status', 'terlambat')->count(),
|
||||||
|
],
|
||||||
|
'peminjaman_terbaru' => Peminjaman::with(['anggota', 'buku'])
|
||||||
|
->latest()
|
||||||
|
->take(5)
|
||||||
|
->get(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
64
app/Http/Controllers/KategoriController.php
Normal file
64
app/Http/Controllers/KategoriController.php
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class KategoriController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Display a listing of the resource.
|
||||||
|
*/
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show the form for creating a new resource.
|
||||||
|
*/
|
||||||
|
public function create()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a newly created resource in storage.
|
||||||
|
*/
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the specified resource.
|
||||||
|
*/
|
||||||
|
public function show(string $id)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show the form for editing the specified resource.
|
||||||
|
*/
|
||||||
|
public function edit(string $id)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the specified resource in storage.
|
||||||
|
*/
|
||||||
|
public function update(Request $request, string $id)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the specified resource from storage.
|
||||||
|
*/
|
||||||
|
public function destroy(string $id)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
88
app/Http/Controllers/PeminjamanController.php
Normal file
88
app/Http/Controllers/PeminjamanController.php
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Peminjaman;
|
||||||
|
use App\Models\Anggota;
|
||||||
|
use App\Models\Buku;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Inertia\Inertia;
|
||||||
|
|
||||||
|
class PeminjamanController extends Controller
|
||||||
|
{
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$peminjamans = Peminjaman::with(['anggota', 'buku'])
|
||||||
|
->latest()
|
||||||
|
->paginate(10);
|
||||||
|
|
||||||
|
return Inertia::render('Peminjaman/Index', [
|
||||||
|
'peminjamans' => $peminjamans,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create()
|
||||||
|
{
|
||||||
|
return Inertia::render('Peminjaman/Create', [
|
||||||
|
'anggotas' => Anggota::where('status', 'aktif')->get(),
|
||||||
|
'bukus' => Buku::where('stok_tersedia', '>', 0)->get(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'anggota_id' => 'required|exists:anggotas,id',
|
||||||
|
'buku_id' => 'required|exists:bukus,id',
|
||||||
|
'tanggal_pinjam' => 'required|date',
|
||||||
|
'tanggal_kembali_rencana' => 'required|date|after:tanggal_pinjam',
|
||||||
|
'catatan' => 'nullable|string',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$buku = Buku::findOrFail($validated['buku_id']);
|
||||||
|
|
||||||
|
if ($buku->stok_tersedia <= 0) {
|
||||||
|
return back()->withErrors(['buku_id' => 'Stok buku tidak tersedia.']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$buku->decrement('stok_tersedia');
|
||||||
|
Peminjaman::create($validated);
|
||||||
|
|
||||||
|
return redirect()->route('peminjaman.index')->with('success', 'Peminjaman berhasil dicatat!');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(Peminjaman $peminjaman)
|
||||||
|
{
|
||||||
|
return Inertia::render('Peminjaman/Show', [
|
||||||
|
'peminjaman' => $peminjaman->load('anggota', 'buku'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function kembalikan(Request $request, Peminjaman $peminjaman)
|
||||||
|
{
|
||||||
|
$tanggalKembali = $request->tanggal_kembali_aktual ?? now()->toDateString();
|
||||||
|
$denda = 0;
|
||||||
|
|
||||||
|
if ($tanggalKembali > $peminjaman->tanggal_kembali_rencana->toDateString()) {
|
||||||
|
$selisih = \Carbon\Carbon::parse($tanggalKembali)
|
||||||
|
->diffInDays($peminjaman->tanggal_kembali_rencana);
|
||||||
|
$denda = $selisih * 1000; // Rp 1.000 per hari
|
||||||
|
}
|
||||||
|
|
||||||
|
$peminjaman->update([
|
||||||
|
'tanggal_kembali_aktual' => $tanggalKembali,
|
||||||
|
'status' => 'dikembalikan',
|
||||||
|
'denda' => $denda,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$peminjaman->buku->increment('stok_tersedia');
|
||||||
|
|
||||||
|
return redirect()->route('peminjaman.index')->with('success', 'Buku berhasil dikembalikan!');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(Peminjaman $peminjaman)
|
||||||
|
{
|
||||||
|
$peminjaman->delete();
|
||||||
|
return redirect()->route('peminjaman.index')->with('success', 'Data peminjaman dihapus!');
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user