Manufacturers API

This commit is contained in:
snipe 2017-01-13 09:37:06 -08:00
parent 73eee1b7b1
commit 89edda2c23
4 changed files with 33 additions and 51 deletions

View file

@ -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;
}

View file

@ -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] [<snipe@snipe.net>]
* @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

View file

@ -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 @@
<tr>
<th data-sortable="true" data-field="id" data-visible="false">{{ trans('general.id') }}</th>
<th data-sortable="true" data-field="name">{{ trans('admin/manufacturers/table.name') }}</th>
<th data-switchable="true" data-searchable="false" data-sortable="false" data-field="assets">{{ trans('general.assets') }}</th>
<th data-switchable="true" data-searchable="false" data-sortable="false" data-field="licenses">{{ trans('general.licenses') }}</th>
<th data-switchable="true" data-searchable="false" data-sortable="false" data-field="accessories">{{ trans('general.accessories') }}</th>
<th data-switchable="true" data-searchable="false" data-sortable="false" data-field="consumables">{{ trans('general.consumables') }}</th>
<th data-switchable="true" data-searchable="false" data-sortable="false" data-field="assets_count">{{ trans('general.assets') }}</th>
<th data-switchable="true" data-searchable="false" data-sortable="false" data-field="licenses_count">{{ trans('general.licenses') }}</th>
<th data-switchable="true" data-searchable="false" data-sortable="false" data-field="accessories_count">{{ trans('general.accessories') }}</th>
<th data-switchable="true" data-searchable="false" data-sortable="false" data-field="consumables_count">{{ trans('general.consumables') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="actions">{{ trans('table.actions') }}</th>
</tr>
</thead>

View file

@ -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']
]
);