From 24c484303ee4db9e2300ef17f1b973ee681c1214 Mon Sep 17 00:00:00 2001 From: FliegenKLATSCH Date: Tue, 28 Sep 2021 13:53:59 +0200 Subject: [PATCH] Do not include deleted assets by default when doing lookup by serial This commit introduces a new query parameter `deleted`, which can be set to `true` to include deleted assets in the response. --- app/Http/Controllers/Api/AssetsController.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 5baaf60d8c..db1b1404ec 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -370,15 +370,21 @@ class AssetsController extends Controller public function showBySerial(Request $request, $serial) { $this->authorize('index', Asset::class); - if ($assets = Asset::with('assetstatus')->with('assignedTo') - ->withTrashed()->where('serial',$serial)->get()) { - return (new AssetsTransformer)->transformAssets($assets, $assets->count()); + + $assets = Asset::with('assetstatus')->with('assignedTo'); + + if ($request->input('deleted', 'false') === 'true') { + $assets = $assets->withTrashed(); } - return response()->json(Helper::formatStandardApiResponse('error', null, 'Asset not found'), 200); + $assets = $assets->where('serial', $serial)->get(); + if ($assets) { + return (new AssetsTransformer)->transformAssets($assets, $assets->count()); + } else { + return response()->json(Helper::formatStandardApiResponse('error', null, 'Asset not found'), 200); + } } - /** * Returns JSON with information about an asset for detail view. *