setup API routes
This commit is contained in:
parent
26536c22e7
commit
fd0c0a7e5d
118
routes/api.php
118
routes/api.php
@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Inventory;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
Route::get("/", function () {
|
||||
return Inventory::get();
|
||||
});
|
||||
|
||||
Route::post('/', function (Request $request) {
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'required|string',
|
||||
'cost' => 'required|numeric|min:0',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'status' => 'Missing or Invalid request body data',
|
||||
], 422);
|
||||
}
|
||||
|
||||
// Validated input
|
||||
$data = $validator->validated();
|
||||
|
||||
$item = new Inventory;
|
||||
$item->name = $data["name"];
|
||||
$item->description = $data["description"];
|
||||
$item->cost = $data["cost"];
|
||||
$item->save();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'Item inserted successfully'
|
||||
]);
|
||||
})->middleware('auth:sanctum')->fallback();
|
||||
|
||||
Route::put('/', function(Request $request) {
|
||||
$items = $request->all();
|
||||
|
||||
if (!is_array($items)) {
|
||||
return response()->json(['status' => 'Invalid request format: Expected an array'], 422);
|
||||
}
|
||||
|
||||
foreach ($items as $index => $item) {
|
||||
$validator = Validator::make($item, [
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'required|string',
|
||||
'cost' => 'required|numeric|min:0',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'status' => "Invalid item at index $index",
|
||||
'errors' => $validator->errors()
|
||||
], 422);
|
||||
}
|
||||
}
|
||||
|
||||
Inventory::query()->delete();
|
||||
|
||||
foreach ($items as $item) {
|
||||
Inventory::create([
|
||||
'name' => $item['name'],
|
||||
'description' => $item['description'],
|
||||
'cost' => $item['cost'],
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json(['status' => 'Collection Replaced']);
|
||||
})->middleware('auth:sanctum');
|
||||
|
||||
Route::delete('/', function() {
|
||||
Inventory::query()->delete();
|
||||
});
|
||||
|
||||
|
||||
|
||||
Route::get("/{id}", function ($id) {
|
||||
return Inventory::where('id', $id)->get();
|
||||
});
|
||||
|
||||
Route::put('/{id}', function(Request $request, $id) {
|
||||
$item = $request->all();
|
||||
|
||||
$validator = Validator::make($item, [
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'required|string',
|
||||
'cost' => 'required|numeric|min:0',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'status' => "Invalid item",
|
||||
'errors' => $validator->errors()
|
||||
], 422);
|
||||
}
|
||||
|
||||
$inventory = Inventory::find($id);
|
||||
if (!$inventory) {
|
||||
return response()->json(['status' => 'Item not found'], 404);
|
||||
}
|
||||
|
||||
$inventory->update([
|
||||
'name' => $item['name'],
|
||||
'description' => $item['description'],
|
||||
'cost' => $item['cost'],
|
||||
]);
|
||||
|
||||
return response()->json(['status' => 'Item Updated']);
|
||||
})->middleware('auth:sanctum');
|
||||
|
||||
|
||||
Route::delete('/{id}', function($id) {
|
||||
Inventory::where('id', $id)->query()->delete();
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user