mirror of
https://github.com/snipe/snipe-it.git
synced 2025-03-05 20:52:15 -08:00
Added API methods and UI to view assets associated with a particular model
This commit is contained in:
parent
8d50bb19af
commit
99d837fa50
|
@ -2,10 +2,12 @@
|
|||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Models\AssetModel;
|
||||
use App\Models\Asset;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Helpers\Helper;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Transformers\AssetModelsTransformer;
|
||||
use App\Http\Transformers\AssetsTransformer;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -81,10 +83,25 @@ class AssetModelsController extends Controller
|
|||
public function show($id)
|
||||
{
|
||||
$this->authorize('view', AssetModel::class);
|
||||
$assetmodel = AssetModel::findOrFail($id);
|
||||
$assetmodel = AssetModel::withCount('assets')->findOrFail($id);
|
||||
return (new AssetModelsTransformer)->transformAssetModel($assetmodel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource's assets
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @since [v4.0]
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function assets($id)
|
||||
{
|
||||
$this->authorize('view', AssetModel::class);
|
||||
$assets = Asset::where('model_id','=',$id)->get();
|
||||
return (new AssetsTransformer)->transformAssets($assets, $assets->count());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
|
|
|
@ -85,7 +85,7 @@ class LocationsController extends Controller
|
|||
{
|
||||
$this->authorize('view', Location::class);
|
||||
$location = Location::findOrFail($id);
|
||||
return $location;
|
||||
return (new LocationsTransformer)->transformLocation($location);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ class AssetModelsTransformer
|
|||
|
||||
public function transformAssetModel (AssetModel $assetmodel)
|
||||
{
|
||||
|
||||
$transformed = [
|
||||
'id' => $assetmodel->id,
|
||||
'name' => e($assetmodel->name),
|
||||
|
@ -29,12 +30,13 @@ class AssetModelsTransformer
|
|||
'category' => ($assetmodel->category_id) ? $assetmodel->category : null,
|
||||
'fieldset' => ($assetmodel->fieldset) ? $assetmodel->fieldset : null,
|
||||
'eol' => ($assetmodel->eol > 0) ? $assetmodel->eol .' months': 'None',
|
||||
'notes' => e($assetmodel->notes),
|
||||
'notes' => e($assetmodel->notes)
|
||||
|
||||
];
|
||||
return $transformed;
|
||||
}
|
||||
|
||||
|
||||
public function transformAssetModelsDatatable($assetmodels) {
|
||||
return (new DatatablesTransformer)->transformDatatables($assetmodels);
|
||||
}
|
||||
|
|
|
@ -33,14 +33,13 @@ class AssetsTransformer
|
|||
'notes' => $asset->notes,
|
||||
'expected_checkin' => $asset->expected_checkin,
|
||||
'order_number' => $asset->order_number,
|
||||
'companyName' => $asset->companyName,
|
||||
'company' => ($asset->company) ? $asset->company : null,
|
||||
'location' => ($asset->assetLoc) ? $asset->assetLoc : null,
|
||||
'image' => ($asset->getImageUrl()) ? $asset->getImageUrl() : null,
|
||||
'assigned_to' => ($asset->assigneduser) ? (new UsersTransformer)->transformUser($asset->assigneduser) : null,
|
||||
'created_at' => $asset->created_at,
|
||||
'purchase_date' => $asset->purchase_date,
|
||||
'purchase_cost' => $asset->purchase_cost,
|
||||
'company' => ($asset->company) ? $asset->company->name: '',
|
||||
|
||||
];
|
||||
|
||||
|
|
|
@ -19,6 +19,12 @@ class LocationsTransformer
|
|||
public function transformLocation (Location $location = null)
|
||||
{
|
||||
if ($location) {
|
||||
|
||||
$assets_arr = [];
|
||||
foreach($location->assets() as $asset) {
|
||||
$assets_arr = ['id' => $asset->id];
|
||||
}
|
||||
|
||||
$transformed = [
|
||||
'id' => e($location->id),
|
||||
'name' => e($location->name),
|
||||
|
@ -28,7 +34,12 @@ class LocationsTransformer
|
|||
'assets_checkedout' => $location->assets()->count(),
|
||||
'assets_default' => $location->assignedassets()->count(),
|
||||
'country' => e($location->country),
|
||||
'assets' => $assets_arr,
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
return $transformed;
|
||||
}
|
||||
|
||||
|
|
|
@ -74,7 +74,7 @@
|
|||
<th data-checkbox="true" data-field="checkbox"></th>
|
||||
@endif
|
||||
<th data-sortable="true" data-field="id" data-visible="false">{{ trans('general.id') }}</th>
|
||||
<th data-field="companyName" data-formatter="companiesLinkObjFormatter" data-searchable="true" data-sortable="true" data-switchable="true" data-visible="false">{{ trans('general.company') }}</th>
|
||||
<th data-field="company" data-formatter="companiesLinkObjFormatter" data-searchable="true" data-sortable="true" data-switchable="true" data-visible="false">{{ trans('general.company') }}</th>
|
||||
<th data-sortable="true" data-field="image" data-formatter="imageFormatter" data-visible="false">{{ trans('admin/hardware/table.image') }}</th>
|
||||
<th data-sortable="true" data-field="name" data-visible="false" data-formatter="hardwareLinkFormatter">{{ trans('admin/hardware/form.name') }}</th>
|
||||
<th data-sortable="true" data-field="asset_tag" data-formatter="hardwareLinkFormatter">{{ trans('admin/hardware/table.asset_tag') }}</th>
|
||||
|
|
|
@ -44,19 +44,19 @@
|
|||
name="modelassets"
|
||||
id="table"
|
||||
class="snipe-table"
|
||||
data-url="{{route('api.models.show', $model->id)}}"
|
||||
data-url="{{route('api.models.assets', $model->id)}}"
|
||||
data-cookie="true"
|
||||
data-click-to-select="true"
|
||||
data-cookie-id-table="modeldetailsViewTable">
|
||||
<thead>
|
||||
<tr>
|
||||
|
||||
<th data-sortable="false" data-field="companyName" data-searchable="false" data-visible="false">{{ trans('admin/companies/table.title') }}</th>
|
||||
<th data-sortable="false" data-field="company" data-searchable="false" data-visible="false" data-formatter="companiesLinkObjFormatter">{{ trans('admin/companies/table.title') }}</th>
|
||||
<th data-sortable="true" data-field="id" data-searchable="false" data-visible="false">{{ trans('general.id') }}</th>
|
||||
<th data-sortable="true" data-field="name" data-searchable="true">{{ trans('general.name') }}</th>
|
||||
<th data-sortable="true" data-field="asset_tag">{{ trans('general.asset_tag') }}</th>
|
||||
<th data-sortable="true" data-field="serial">{{ trans('admin/hardware/table.serial') }}</th>
|
||||
<th data-sortable="false" data-field="assigned_to">{{ trans('general.user') }}</th>
|
||||
<th data-sortable="true" data-field="name" data-searchable="true" data-formatter="hardwareLinkFormatter">{{ trans('general.name') }}</th>
|
||||
<th data-sortable="true" data-field="asset_tag" data-formatter="hardwareLinkFormatter">{{ trans('general.asset_tag') }}</th>
|
||||
<th data-sortable="true" data-field="serial" data-formatter="hardwareLinkFormatter">{{ trans('admin/hardware/table.serial') }}</th>
|
||||
<th data-sortable="false" data-field="assigned_to" data-formatter="usersLinkObjFormatter">{{ trans('general.user') }}</th>
|
||||
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="actions">{{ trans('table.actions') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
|
|
@ -63,6 +63,13 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
|
|||
);
|
||||
|
||||
|
||||
Route::get('models/{id}/assets',
|
||||
[
|
||||
'as' => 'api.models.assets',
|
||||
'uses'=> 'AssetModelsController@assets'
|
||||
]);
|
||||
|
||||
|
||||
Route::resource('models', 'AssetModelsController',
|
||||
['names' =>
|
||||
[
|
||||
|
|
Loading…
Reference in a new issue