From 89edda2c23d560aef01748c7fed2071cb1e171ec Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 13 Jan 2017 09:37:06 -0800 Subject: [PATCH] Manufacturers API --- .../Api/ManufacturersController.php | 24 ++++++++++- .../Controllers/ManufacturersController.php | 42 +------------------ resources/views/manufacturers/index.blade.php | 10 ++--- routes/api.php | 8 ++-- 4 files changed, 33 insertions(+), 51 deletions(-) diff --git a/app/Http/Controllers/Api/ManufacturersController.php b/app/Http/Controllers/Api/ManufacturersController.php index 04cdf0ae88..30e36a7d8f 100644 --- a/app/Http/Controllers/Api/ManufacturersController.php +++ b/app/Http/Controllers/Api/ManufacturersController.php @@ -6,6 +6,7 @@ use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Helpers\Helper; use App\Models\Manufacturer; +use App\Http\Transformers\DatatablesTransformer; class ManufacturersController extends Controller { @@ -16,10 +17,29 @@ class ManufacturersController extends Controller * @since [v4.0] * @return \Illuminate\Http\Response */ - public function index() + public function index(Request $request) { $this->authorize('view', Manufacturer::class); - $manufacturers = Manufacturer::all(); + $allowed_columns = ['id','name']; + + $manufacturers = Manufacturer::select( + array('id','name') + )->withCount('assets')->withCount('licenses')->withCount('consumables')->withCount('accessories'); + + + if ($request->has('search')) { + $manufacturers = $manufacturers->TextSearch($request->input('search')); + } + + $offset = request('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'; + $manufacturers->orderBy($sort, $order); + + $total = $manufacturers->count(); + $manufacturers = $manufacturers->skip($offset)->take($limit)->get(); + return (new DatatablesTransformer)->transformDatatables($manufacturers, $total); return $manufacturers; } diff --git a/app/Http/Controllers/ManufacturersController.php b/app/Http/Controllers/ManufacturersController.php index 9fb5320521..81579ce074 100755 --- a/app/Http/Controllers/ManufacturersController.php +++ b/app/Http/Controllers/ManufacturersController.php @@ -172,47 +172,7 @@ class ManufacturersController extends Controller return redirect()->route('manufacturers')->with('error', $error); } - /** - * Generates the JSON used to display the manufacturer listings. - * - * @author [A. Gianotto] [] - * @see ManufacturersController::getIndex() - * @since [v1.0] - * @param Request $request - * @return String JSON - */ - public function getDatatable(Request $request) - { - $manufacturers = Manufacturer::select(array('id','name'))->whereNull('deleted_at'); - - if ($request->has('search')) { - $manufacturers = $manufacturers->TextSearch(e($request->input('search'))); - } - $offset = request('offset', 0); - $limit = request('limit', 50); - - $allowed_columns = ['id','name']; - $order = $request->input('order') === 'asc' ? 'asc' : 'desc'; - $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; - - $manufacturers->orderBy($sort, $order); - - $manufacturersCount = $manufacturers->count(); - $manufacturers = $manufacturers->skip($offset)->take($limit)->get(); - - $rows = array(); - - foreach ($manufacturers as $manufacturer) { - $rows[] = $manufacturer->present()->forDataTable(); - } - - $data = array('total' => $manufacturersCount, 'rows' => $rows); - - return $data; - - } - - + /** * Generates the JSON used to display the manufacturer detail. * This JSON returns data on all of the assets with the specified diff --git a/resources/views/manufacturers/index.blade.php b/resources/views/manufacturers/index.blade.php index 128aba934a..f4e75344f7 100755 --- a/resources/views/manufacturers/index.blade.php +++ b/resources/views/manufacturers/index.blade.php @@ -24,7 +24,7 @@ name="manufacturers" class="table table-striped snipe-table" id="table" - data-url="{{route('api.manufacturers.list') }}" + data-url="{{route('api.manufacturers.index') }}" data-cookie="true" data-click-to-select="true" data-cookie-id-table="manufacturersTable-{{ config('version.hash_version') }}"> @@ -32,10 +32,10 @@ {{ trans('general.id') }} {{ trans('admin/manufacturers/table.name') }} - {{ trans('general.assets') }} - {{ trans('general.licenses') }} - {{ trans('general.accessories') }} - {{ trans('general.consumables') }} + {{ trans('general.assets') }} + {{ trans('general.licenses') }} + {{ trans('general.accessories') }} + {{ trans('general.consumables') }} {{ trans('table.actions') }} diff --git a/routes/api.php b/routes/api.php index 2c371a0954..d83a21a25d 100644 --- a/routes/api.php +++ b/routes/api.php @@ -199,11 +199,13 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () { ['names' => [ 'index' => 'api.manufacturers.index', - 'create' => 'api.manufacturers.create', + 'show' => 'api.manufacturers.show', + 'update' => 'api.manufacturers.update', + 'store' => 'api.manufacturers.store', 'destroy' => 'api.manufacturers.destroy' ], - 'parameters' => - ['manufacturer' => 'manufacturer_id'] + 'except' => ['edit'], + 'parameters' => ['manufacturer' => 'manufacturer_id'] ] );