From 5f49e7c1a78eb9c4b9291b1cacc2fb9e2443e39f Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 25 Jan 2017 02:19:26 -0800 Subject: [PATCH] Accessories API routes Still a bit of a WIP --- .../Controllers/Api/AccessoriesController.php | 56 +++++++++++++++++-- .../Controllers/Api/AssetModelsController.php | 1 + .../Transformers/AccessoriesTransformer.php | 55 ++++++++++++++++++ app/Presenters/AccessoryPresenter.php | 29 ++++++---- resources/views/accessories/index.blade.php | 2 +- resources/views/accessories/view.blade.php | 4 +- .../views/partials/bootstrap-table.blade.php | 1 + routes/api.php | 33 ++++++----- 8 files changed, 146 insertions(+), 35 deletions(-) create mode 100644 app/Http/Transformers/AccessoriesTransformer.php diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index 6ad76448a0..22e34c1e59 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -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] [] + * @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); } diff --git a/app/Http/Controllers/Api/AssetModelsController.php b/app/Http/Controllers/Api/AssetModelsController.php index 23502b32b7..408f3aa162 100644 --- a/app/Http/Controllers/Api/AssetModelsController.php +++ b/app/Http/Controllers/Api/AssetModelsController.php @@ -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. diff --git a/app/Http/Transformers/AccessoriesTransformer.php b/app/Http/Transformers/AccessoriesTransformer.php new file mode 100644 index 0000000000..95317a8b8b --- /dev/null +++ b/app/Http/Transformers/AccessoriesTransformer.php @@ -0,0 +1,55 @@ +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); + } + + + +} diff --git a/app/Presenters/AccessoryPresenter.php b/app/Presenters/AccessoryPresenter.php index 7605483df3..2319973f5e 100644 --- a/app/Presenters/AccessoryPresenter.php +++ b/app/Presenters/AccessoryPresenter.php @@ -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", ] ]; diff --git a/resources/views/accessories/index.blade.php b/resources/views/accessories/index.blade.php index 63871a6b97..819932dcd8 100755 --- a/resources/views/accessories/index.blade.php +++ b/resources/views/accessories/index.blade.php @@ -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') }}"> diff --git a/resources/views/accessories/view.blade.php b/resources/views/accessories/view.blade.php index d6d9795412..4ce9fb843d 100644 --- a/resources/views/accessories/view.blade.php +++ b/resources/views/accessories/view.blade.php @@ -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" > - {{ trans('general.user') }} + {{ trans('general.user') }} {{ trans('table.actions') }} diff --git a/resources/views/partials/bootstrap-table.blade.php b/resources/views/partials/bootstrap-table.blade.php index d98699831d..b506a6772e 100644 --- a/resources/views/partials/bootstrap-table.blade.php +++ b/resources/views/partials/bootstrap-table.blade.php @@ -119,6 +119,7 @@ $('.snipe-table').bootstrapTable({ var formatters = [ 'hardware', + 'accessories', 'locations', 'users', 'manufacturers', diff --git a/routes/api.php b/routes/api.php index d157640795..1543126f16 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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 () {