Load components in the assets API if components=true in API request

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2021-09-23 17:23:53 -07:00
parent 6e270c0ed2
commit 3b7ce0091c
3 changed files with 46 additions and 11 deletions

View file

@ -320,11 +320,24 @@ class AssetsController extends Controller
$total = $assets->count();
$assets = $assets->skip($offset)->take($limit)->get();
/**
* Include additional associated relationships
*/
if ($request->input('components')) {
$assets->load(['components' => function ($query) {
$query->orderBy('created_at', 'desc');
}]);
}
/**
* Here we're just determining which Transformer (via $transformer) to use based on the
* variables we set earlier on in this method - we default to AssetsTransformer.
*/
return (new $transformer)->transformAssets($assets, $total);
return (new $transformer)->transformAssets($assets, $total, $request);
}
@ -336,11 +349,11 @@ class AssetsController extends Controller
* @since [v4.2.1]
* @return JsonResponse
*/
public function showByTag($tag)
public function showByTag(Request $request, $tag)
{
if ($asset = Asset::with('assetstatus')->with('assignedTo')->where('asset_tag',$tag)->first()) {
$this->authorize('view', $asset);
return (new AssetsTransformer)->transformAsset($asset);
return (new AssetsTransformer)->transformAsset($asset, $request);
}
return response()->json(Helper::formatStandardApiResponse('error', null, 'Asset not found'), 200);
@ -354,7 +367,7 @@ class AssetsController extends Controller
* @since [v4.2.1]
* @return JsonResponse
*/
public function showBySerial($serial)
public function showBySerial(Request $request, $serial)
{
$this->authorize('index', Asset::class);
if ($assets = Asset::with('assetstatus')->with('assignedTo')
@ -374,17 +387,17 @@ class AssetsController extends Controller
* @since [v4.0]
* @return JsonResponse
*/
public function show($id)
public function show(Request $request, $id)
{
if ($asset = Asset::with('assetstatus')->with('assignedTo')->withTrashed()
->withCount('checkins as checkins_count', 'checkouts as checkouts_count', 'userRequests as user_requests_count')->findOrFail($id)) {
$this->authorize('view', $asset);
return (new AssetsTransformer)->transformAsset($asset);
return (new AssetsTransformer)->transformAsset($asset, $request->input('components') );
}
}
public function licenses($id)
public function licenses(Request $request, $id)
{
$this->authorize('view', Asset::class);
$this->authorize('view', License::class);

View file

@ -440,12 +440,12 @@ class UsersController extends Controller
* @param $userId
* @return string JSON
*/
public function assets($id)
public function assets(Request $request, $id)
{
$this->authorize('view', User::class);
$this->authorize('view', Asset::class);
$assets = Asset::where('assigned_to', '=', $id)->where('assigned_type', '=', User::class)->with('model')->get();
return (new AssetsTransformer)->transformAssets($assets, $assets->count());
return (new AssetsTransformer)->transformAssets($assets, $assets->count(), $request);
}
/**

View file

@ -134,6 +134,28 @@ class AssetsTransformer
}
if (request('components')=='true') {
if ($asset->components) {
$array['components'] = [];
foreach ($asset->components as $component) {
$array['components'][] = [
[
'id' => $component->id,
'name' => $component->name,
'qty' => $component->pivot->assigned_qty,
'price_cost' => $component->purchase_cost,
'purchase_total' => $component->purchase_cost * $component->pivot->assigned_qty,
'checkout_date' => Helper::getFormattedDateObject($component->pivot->created_at, 'datetime') ,
]
];
}
}
}
$array += $permissions_array;
return $array;
}