100 lines
2.1 KiB
PHP
100 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Nampan;
|
|
use App\Models\Item;
|
|
use Illuminate\Http\Request;
|
|
|
|
class NampanController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
return response()->json(
|
|
Nampan::with('items.produk.foto')->withCount('items')->get()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'nama' => 'required|string|max:100',
|
|
],
|
|
[
|
|
'nama' => 'Nama nampan harus diisi.'
|
|
]);
|
|
|
|
Nampan::create($validated);
|
|
|
|
|
|
return response()->json([
|
|
'message' => 'Nampan berhasil dibuat'
|
|
],201);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(int $id)
|
|
{
|
|
return response()->json(
|
|
Nampan::with('items.produk.foto')->find($id)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, int $id)
|
|
{
|
|
$validated = $request->validate([
|
|
'nama' => 'required|string|max:100',
|
|
],
|
|
[
|
|
'nama' => 'Nama nampan harus diisi.'
|
|
]);
|
|
|
|
$nampan = Nampan::findOrFail($id);
|
|
|
|
$nampan->update($validated);
|
|
|
|
return response()->json([
|
|
'message' => 'Nampan berhasil diupdate'
|
|
],200);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(int $id)
|
|
{
|
|
$nampan = Nampan::findOrFail($id);
|
|
|
|
$nampan->items()->each(function ($item) {
|
|
$item->update(['id_nampan' => null]);
|
|
});
|
|
|
|
$nampan->delete();
|
|
|
|
return response()->json([
|
|
'message' => 'Nampan berhasil dihapus'
|
|
], 204);
|
|
}
|
|
|
|
public function kosongkan()
|
|
{
|
|
Item::query()->update(['id_nampan' => null]);
|
|
|
|
return response()->json([
|
|
'message' => 'Semua nampan berhasil dikosongkan'
|
|
], 200);
|
|
}
|
|
|
|
}
|