126 lines
3.6 KiB
PHP
126 lines
3.6 KiB
PHP
<?php
|
|
|
|
use App\Models\Inventory;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
Route::get("/", function () {
|
|
Log::channel('api')->info('API accessed by user.', [ "GET", "/api", Inventory::get() ]);
|
|
return Inventory::get();
|
|
});
|
|
|
|
Route::post('/', function (Request $request) {
|
|
Log::channel('api')->info('API accessed by user.', [ "POST", "/api", $request->all() ]);
|
|
$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();
|
|
Log::channel('api')->info('API accessed by user.', [ "PUT", "/api", $items ]);
|
|
|
|
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() {
|
|
Log::channel('api')->info('API accessed by user.', [ "DELETE", "/api" ]);
|
|
Inventory::query()->delete();
|
|
});
|
|
|
|
|
|
|
|
Route::get("/{id}", function ($id) {
|
|
Log::channel('api')->info('API accessed by user.', [ "GET", "/api", Inventory::where('id', $id)->get() ]);
|
|
return Inventory::where('id', $id)->get();
|
|
});
|
|
|
|
Route::put('/{id}', function(Request $request, $id) {
|
|
$item = $request->all();
|
|
Log::channel('api')->info('API accessed by user', [ "PUT", "/api", $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",
|
|
'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) {
|
|
Log::channel('api')->info('API accessed by user.', [ "DELETE", "/api" ]);
|
|
Inventory::where('id', $id)->query()->delete();
|
|
});
|