Accessories API routes

Still a bit of a WIP
This commit is contained in:
snipe 2017-01-25 02:19:26 -08:00
parent 75d35273c9
commit 5f49e7c1a7
8 changed files with 146 additions and 35 deletions

View file

@ -6,6 +6,8 @@ use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Helpers\Helper;
use App\Models\Accessory;
use App\Http\Transformers\AccessoriesTransformer;
class AccessoriesController extends Controller
{
@ -16,11 +18,39 @@ class AccessoriesController extends Controller
* @since [v4.0]
* @return \Illuminate\Http\Response
*/
public function index()
public function index(Request $request)
{
$this->authorize('view', Accessory::class);
$accessories = Accessory::all();
return $accessories;
$allowed_columns = ['id','name','model_number','eol','notes','created_at','min_amt','company_id'];
$accessories = Accessory::whereNull('accessories.deleted_at')->with('category', 'company', 'manufacturer', 'users', 'location');
if ($request->has('search')) {
$accessories = $accessories->TextSearch($request->input('search'));
}
$offset = $request->input('offset', 0);
$limit = $request->input('limit', 50);
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
switch ($sort) {
case 'category':
$accessories = $accessories->OrderCategory($order);
break;
case 'companyName':
$accessories = $accessories->OrderCompany($order);
break;
default:
$accessories = $accessories->orderBy($sort, $order);
break;
}
$accessories->orderBy($sort, $order);
$total = $accessories->count();
$accessories = $accessories->skip($offset)->take($limit)->get();
return (new AccessoriesTransformer)->transformAccessories($accessories, $total);
}
@ -57,7 +87,25 @@ class AccessoriesController extends Controller
{
$this->authorize('view', Accessory::class);
$accessory = Accessory::findOrFail($id);
return $accessory;
return (new AccessoriesTransformer)->transformAccessory($accessory);
}
/**
* Display the specified resource.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0]
* @param int $id
* @return \Illuminate\Http\Response
*/
public function checkedout($id)
{
$this->authorize('view', Accessory::class);
$accessory = Accessory::findOrFail($id)->with('users')->first();
$accessories_users = $accessory->users;
$total = $accessories_users->count();
return (new AccessoriesTransformer)->transformCheckedoutAccessories($accessories_users, $total);
}

View file

@ -7,6 +7,7 @@ use App\Helpers\Helper;
use Illuminate\Http\Request;
use App\Http\Transformers\AssetModelsTransformer;
/**
* This class controls all actions related to asset models for
* the Snipe-IT Asset Management application.

View file

@ -0,0 +1,55 @@
<?php
namespace App\Http\Transformers;
use App\Models\Accessory;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
class AccessoriesTransformer
{
public function transformAccessories (Collection $accessories, $total)
{
$array = array();
foreach ($accessories as $accessory) {
$array[] = self::transformAccessory($accessory);
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
public function transformAccessory (Accessory $accessory)
{
$transformed = [
'id' => $accessory->id,
'name' => e($accessory->name),
'company' => ($accessory->company) ? $accessory->company : null,
'manufacturer' => ($accessory->manufacturer_id) ? $accessory->manufacturer : null,
'model_number' => ($accessory->model_number) ? e($accessory->model_number) : null,
'category' => ($accessory->category_id) ? $accessory->category : null,
'location' => ($accessory->location) ? $accessory->location : null,
'notes' => ($accessory->notes) ? e($accessory->notes) : null,
'qty' => ($accessory->qty) ? e($accessory->qty) : null,
'purchase_date' => ($accessory->purchase_date) ? e($accessory->purchase_date) : null,
'purchase_cost' => ($accessory->purchase_cost) ? e($accessory->purchase_cost) : null,
'order_number' => ($accessory->order_number) ? e($accessory->order_number) : null,
'min_qty' => ($accessory->min_amt) ? e($accessory->min_amt) : null,
'remaining_qty' => $accessory->numRemaining(),
];
return $transformed;
}
public function transformCheckedoutAccessories (Collection $accessories_users, $total)
{
$array = array();
foreach ($accessories_users as $user) {
$array[] = (new UsersTransformer)->transformUser($user);
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
}

View file

@ -25,42 +25,58 @@ class AccessoryPresenter extends Presenter
{
$layout = [
[
"field" => "companyName",
"field" => "company",
"searchable" => true,
"sortable" => true,
"switchable" => true,
"title" => trans('admin/companies/table.title'),
"visible" => false,
"formatter" => "companiesLinkObjFormatter"
], [
"field" => "name",
"searchable" => true,
"sortable" => true,
"title" => trans('admin/accessories/table.title'),
"formatter" => "accessoriesLinkFormatter"
], [
"field" => "category",
"searchable" => true,
"sortable" => true,
"title" => trans('admin/accessories/general.accessory_category'),
"formatter" => "categoriesLinkObjFormatter"
], [
"field" => "model_number",
"searchable" => true,
"sortable" => true,
"title" => trans('admin/models/table.modelnumber'),
"formatter" => "accessoriesLinkFormatter"
], [
"field" => "manufacturer",
"searchable" => true,
"sortable" => true,
"title" => trans('general.manufacturer'),
"formatter" => "manufacturersLinkObjFormatter",
], [
"field" => "location",
"searchable" => true,
"sortable" => true,
"title" => trans('general.location'),
"formatter" => "locationsLinkObjFormatter",
], [
"field" => "qty",
"searchable" => false,
"sortable" => false,
"title" => trans('admin/accessories/general.total'),
], [
"field" => "min_qty",
"searchable" => false,
"sortable" => true,
"title" => trans('general.min_amt'),
], [
"field" => "remaining_qty",
"searchable" => false,
"sortable" => false,
"title" => trans('admin/accessories/general.remaining'),
], [
"field" => "purchase_date",
"searchable" => true,
@ -78,22 +94,13 @@ class AccessoryPresenter extends Presenter
"sortable" => true,
"visible" => false,
"title" => trans('general.order_number'),
], [
"field" => "min_amt",
"searchable" => false,
"sortable" => true,
"title" => trans('general.min_amt'),
], [
"field" => "numRemaining",
"searchable" => false,
"sortable" => false,
"title" => trans('admin/accessories/general.remaining'),
], [
"field" => "actions",
"searchable" => false,
"sortable" => false,
"switchable" => false,
"title" => trans('table.actions'),
"formatter" => "accessoriesActionsFormatter",
]
];

View file

@ -25,7 +25,7 @@
name="accessories"
class="table table-striped snipe-table"
id="table"
data-url="{{route('api.accessories.list') }}"
data-url="{{route('api.accessories.index') }}"
data-cookie="true"
data-click-to-select="true"
data-cookie-id-table="accessoriesTable-{{ config('version.hash_version') }}">

View file

@ -57,14 +57,14 @@
name="accessory_users"
class="table table-striped snipe-table"
id="table"
data-url="{{ route('api.accessories.view', $accessory->id) }}"
data-url="{{ route('api.accessories.show', $accessory->id) }}"
data-cookie="true"
data-click-to-select="true"
data-cookie-id-table="accessoryUsersTable"
>
<thead>
<tr>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="name">{{ trans('general.user') }}</th>
<th data-switchable="false" data-searchable="false" data-formatter="userLinkObjFormatter" data-sortable="false" data-field="name">{{ trans('general.user') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="actions">{{ trans('table.actions') }}</th>
</tr>
</thead>

View file

@ -119,6 +119,7 @@ $('.snipe-table').bootstrapTable({
var formatters = [
'hardware',
'accessories',
'locations',
'users',
'manufacturers',

View file

@ -190,8 +190,6 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
Route::resource('consumables', 'ConsumablesController',
['names' =>
[
@ -219,15 +217,28 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
);
Route::group([ 'prefix' => 'accessories' ], function () {
Route::match(['DELETE'], '{id}', ['uses' => 'AccessoriessController@destroy','as' => 'api.accessories.destroy']);
Route::get(
'{id}/checkedout',
[ 'as' => 'api.accessories.checkedout', 'uses' => 'AccessoriesController@checkedout' ]
);
});
Route::resource('accessories', 'AccessoriesController',
['names' =>
[
'index' => 'api.accessories.index',
'create' => 'api.accessories.create',
'show' => 'api.accessories.show',
'update' => 'api.accessories.update',
'store' => 'api.accessories.store',
'destroy' => 'api.accessories.destroy'
],
'parameters' =>
['accessory' => 'accessory_id']
'except' => ['edit'],
'parameters' => ['accessory' => 'accessory_id']
]
);
@ -252,18 +263,6 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
]);
/*---Accessories API---*/
Route::group([ 'prefix' => 'accessories' ], function () {
Route::get('list', [ 'as' => 'api.accessories.list', 'uses' => 'AccessoriesController@getDatatable' ]);
Route::get(
'{accessoryID}/view',
[ 'as' => 'api.accessories.view', 'uses' => 'AccessoriesController@getDataView' ]
);
});
/*---Locations API---*/
Route::group(array('prefix'=>'locations'), function () {