From 44be8c8f6048293079d3bd4001cd660c3ded2d61 Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 9 Jan 2023 19:54:21 -0800 Subject: [PATCH 1/5] Determine which transformer to use based on number of assets Signed-off-by: snipe --- app/Http/Controllers/Api/AssetsController.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index ee37c3ad04..6d84217a78 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -381,12 +381,21 @@ class AssetsController extends Controller $assets = $assets->get(); - if (($assets) && ($assets->count() > 0)) { + // If there is only one result, we should pull the first (and only) asset from the returned collection, since + // transformAsset() expects an Asset object, NOT a collection + if (($assets) && ($assets->count() == 1)) { + return (new AssetsTransformer)->transformAsset($assets->first()); + + // If there is more than one result - which would probably never show up in the UI, since it would only happen if + // there are multiple deleted assets with the same tag, pass the collection normally + } elseif (($assets) && ($assets->count() > 1)) { return (new AssetsTransformer)->transformAssets($assets, $assets->count()); - } else { - return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200); } + // If there are 0 results, return the "no such asset" response + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200); + + } /** From bf849128fce78557b987f85eddb91cc8afae3c49 Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 9 Jan 2023 19:57:47 -0800 Subject: [PATCH 2/5] Replicate the response behavior in the bySerial endpoint Signed-off-by: snipe --- app/Http/Controllers/Api/AssetsController.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 6d84217a78..5c5b3a6038 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -418,11 +418,20 @@ class AssetsController extends Controller $assets = $assets->get(); - if (($assets) && ($assets->count() > 0)) { + // If there is only one result, we should pull the first (and only) asset from the returned collection, since + // transformAsset() expects an Asset object, NOT a collection + if (($assets) && ($assets->count() == 1)) { + return (new AssetsTransformer)->transformAsset($assets->first()); + + // If there is more than one result - which would probably never show up in the UI, since it would only happen if + // there are multiple deleted assets with the same tag, pass the collection normally + } elseif (($assets) && ($assets->count() > 1)) { return (new AssetsTransformer)->transformAssets($assets, $assets->count()); - } else { - return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200); } + + // If there are 0 results, return the "no such asset" response + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200); + } /** From ae76d46f87c2e0a5a249fbeb8abeaf533742a640 Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 9 Jan 2023 20:23:05 -0800 Subject: [PATCH 3/5] Small refactor to ALWAYS return deleted assets in a collection transformer Signed-off-by: snipe --- app/Http/Controllers/Api/AssetsController.php | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 5c5b3a6038..bb222ecb68 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -379,17 +379,19 @@ class AssetsController extends Controller $assets = $assets->withTrashed(); } - $assets = $assets->get(); + if (($assets = $assets->get()) && ($assets->count()) > 0) { - // If there is only one result, we should pull the first (and only) asset from the returned collection, since - // transformAsset() expects an Asset object, NOT a collection - if (($assets) && ($assets->count() == 1)) { - return (new AssetsTransformer)->transformAsset($assets->first()); + // If there is only one result, we should pull the first (and only) asset from the returned collection, since + // transformAsset() expects an Asset object, NOT a collection + if ($assets->count() == 1) { + return (new AssetsTransformer)->transformAsset($assets->first()); + + // If there is more than one result OR if the endpoint is requesting deleted items (even if there is only one + // match, return the normal collection transformed. + } elseif (($assets->count() > 1) || ($request->input('deleted') == 'true')) { + return (new AssetsTransformer)->transformAssets($assets, $assets->count()); + } - // If there is more than one result - which would probably never show up in the UI, since it would only happen if - // there are multiple deleted assets with the same tag, pass the collection normally - } elseif (($assets) && ($assets->count() > 1)) { - return (new AssetsTransformer)->transformAssets($assets, $assets->count()); } // If there are 0 results, return the "no such asset" response @@ -418,15 +420,19 @@ class AssetsController extends Controller $assets = $assets->get(); - // If there is only one result, we should pull the first (and only) asset from the returned collection, since - // transformAsset() expects an Asset object, NOT a collection - if (($assets) && ($assets->count() == 1)) { - return (new AssetsTransformer)->transformAsset($assets->first()); + if (($assets = $assets->get()) && ($assets->count()) > 0) { + + // If there is exactly one result, we should pull the first (and only) asset from the returned collection, since + // transformAsset() expects an Asset object, NOT a collection + if ($assets->count() == 1) { + return (new AssetsTransformer)->transformAsset($assets->first()); + + // If there is more than one result OR if the endpoint is requesting deleted items (even if there is only one + // match, return the normal collection transformed. + } elseif (($assets->count() > 1) || ($request->input('deleted') == 'true')) { + return (new AssetsTransformer)->transformAssets($assets, $assets->count()); + } - // If there is more than one result - which would probably never show up in the UI, since it would only happen if - // there are multiple deleted assets with the same tag, pass the collection normally - } elseif (($assets) && ($assets->count() > 1)) { - return (new AssetsTransformer)->transformAssets($assets, $assets->count()); } // If there are 0 results, return the "no such asset" response From cc665d50ed216903939a5c72eaa79411ff90083d Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 9 Jan 2023 20:40:12 -0800 Subject: [PATCH 4/5] One more refactor, hopefully simplifying Signed-off-by: snipe --- app/Http/Controllers/Api/AssetsController.php | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index bb222ecb68..f8ae8914c1 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -381,23 +381,19 @@ class AssetsController extends Controller if (($assets = $assets->get()) && ($assets->count()) > 0) { - // If there is only one result, we should pull the first (and only) asset from the returned collection, since - // transformAsset() expects an Asset object, NOT a collection - if ($assets->count() == 1) { + // If there is exactly one result and the deleted parameter is not passed, we should pull the first (and only) + // asset from the returned collection, since transformAsset() expects an Asset object, NOT a collection + if (($assets->count() == 1) && ($request->input('deleted') != 'true')) { return (new AssetsTransformer)->transformAsset($assets->first()); - // If there is more than one result OR if the endpoint is requesting deleted items (even if there is only one - // match, return the normal collection transformed. - } elseif (($assets->count() > 1) || ($request->input('deleted') == 'true')) { + // If there is more than one result OR if the endpoint is requesting deleted items (even if there is only one + // match, return the normal collection transformed. + } else { return (new AssetsTransformer)->transformAssets($assets, $assets->count()); } } - // If there are 0 results, return the "no such asset" response - return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200); - - } /** @@ -422,14 +418,14 @@ class AssetsController extends Controller if (($assets = $assets->get()) && ($assets->count()) > 0) { - // If there is exactly one result, we should pull the first (and only) asset from the returned collection, since - // transformAsset() expects an Asset object, NOT a collection - if ($assets->count() == 1) { + // If there is exactly one result and the deleted parameter is not passed, we should pull the first (and only) + // asset from the returned collection, since transformAsset() expects an Asset object, NOT a collection + if (($assets->count() == 1) && ($request->input('deleted') != 'true')) { return (new AssetsTransformer)->transformAsset($assets->first()); - // If there is more than one result OR if the endpoint is requesting deleted items (even if there is only one - // match, return the normal collection transformed. - } elseif (($assets->count() > 1) || ($request->input('deleted') == 'true')) { + // If there is more than one result OR if the endpoint is requesting deleted items (even if there is only one + // match, return the normal collection transformed. + } else { return (new AssetsTransformer)->transformAssets($assets, $assets->count()); } From 7846251d25d91617f36961e5b7f8fe68eb0b9ca8 Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 9 Jan 2023 20:43:57 -0800 Subject: [PATCH 5/5] Re-added return for no results (derp) Signed-off-by: snipe --- app/Http/Controllers/Api/AssetsController.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index f8ae8914c1..039d5b174e 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -394,6 +394,9 @@ class AssetsController extends Controller } + // If there are 0 results, return the "no such asset" response + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200); + } /**