mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-12 14:27:33 -08:00
Categories API
This commit is contained in:
parent
edca6ad19d
commit
14ddcc56a2
121
app/Http/Controllers/Api/CategoriesController.php
Normal file
121
app/Http/Controllers/Api/CategoriesController.php
Normal file
|
@ -0,0 +1,121 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Helpers\Helper;
|
||||
use App\Models\Category;
|
||||
use App\Http\Transformers\DatatablesTransformer;
|
||||
|
||||
class CategoriesController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @since [v4.0]
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$this->authorize('view', Category::class);
|
||||
$allowed_columns = ['id', 'name','category_type','use_default_eula','require_acceptance','checkin_email'];
|
||||
|
||||
$categories = Category::select(['id', 'name','category_type','use_default_eula','require_acceptance','checkin_email'])
|
||||
->withCount('assets', 'accessories', 'consumables', 'components');
|
||||
|
||||
if ($request->has('search')) {
|
||||
$categories = $categories->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';
|
||||
$categories->orderBy($sort, $order);
|
||||
|
||||
$total = $categories->count();
|
||||
$categories = $categories->skip($offset)->take($limit)->get();
|
||||
return (new DatatablesTransformer)->transformDatatables($categories, $total);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @since [v4.0]
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$this->authorize('create', Category::class);
|
||||
$category = new Category;
|
||||
$category->fill($request->all());
|
||||
|
||||
if ($category->save()) {
|
||||
return response()->json(Helper::formatStandardApiResponse('success', $category, trans('admin/categories/message.create.success')));
|
||||
}
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, $category->getErrors()));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @since [v4.0]
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$this->authorize('view', Category::class);
|
||||
$category = Category::findOrFail($id);
|
||||
return $category;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @since [v4.0]
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$this->authorize('edit', Category::class);
|
||||
$category = Category::findOrFail($id);
|
||||
$category->fill($request->all());
|
||||
|
||||
if ($category->save()) {
|
||||
return response()->json(Helper::formatStandardApiResponse('success', $category, trans('admin/categories/message.update.success')));
|
||||
}
|
||||
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, $category->getErrors()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @since [v4.0]
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$this->authorize('delete', Category::class);
|
||||
$category = Category::findOrFail($id);
|
||||
$this->authorize('delete', $category);
|
||||
$category->delete();
|
||||
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/categories/message.delete.success')));
|
||||
|
||||
}
|
||||
}
|
|
@ -200,49 +200,7 @@ class CategoriesController extends Controller
|
|||
// Redirect to the user management page
|
||||
return redirect()->route('categories.index')->with('error', $error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a JSON response with the data to populate the bootstrap table on the
|
||||
* category listing page.
|
||||
*
|
||||
* @todo Refactor this nastiness. Assets do not behave the same as accessories, etc.
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @see CategoriesController::getIndex() method that generates the view
|
||||
* @since [v1.8]
|
||||
* @param Request $request
|
||||
* @return String JSON
|
||||
*/
|
||||
public function getDatatable(Request $request)
|
||||
{
|
||||
// Grab all the categories
|
||||
$categories = Category::with('assets', 'accessories', 'consumables', 'components');
|
||||
|
||||
if (Input::has('search')) {
|
||||
$categories = $categories->TextSearch(e($request->input('search')));
|
||||
}
|
||||
|
||||
$offset = request('offset', 0);
|
||||
$limit = request('limit', 50);
|
||||
|
||||
$allowed_columns = ['id','name','category_type'];
|
||||
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
||||
$sort = in_array($request->input('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at';
|
||||
|
||||
$categories = $categories->orderBy($sort, $order);
|
||||
|
||||
$catCount = $categories->count();
|
||||
$categories = $categories->skip($offset)->take($limit)->get();
|
||||
|
||||
$rows = array();
|
||||
|
||||
foreach ($categories as $category) {
|
||||
$rows[] = $category->present()->forDataTable();
|
||||
}
|
||||
|
||||
$data = array('total' => $catCount, 'rows' => $rows);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
public function getDataViewAssets(Request $request, $categoryID)
|
||||
{
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
class="table table-striped snipe-table"
|
||||
name="categories"
|
||||
id="table"
|
||||
data-url="{{route('api.categories.list') }}"
|
||||
data-url="{{route('api.categories.index') }}"
|
||||
data-cookie="true"
|
||||
data-click-to-select="true"
|
||||
data-cookie-id-table="categoriesTable-{{ config('version.hash_version') }}"
|
||||
|
@ -34,9 +34,9 @@
|
|||
<th data-sortable="true" data-field="id" data-visible="false">{{ trans('general.id') }}</th>
|
||||
<th data-sortable="true" data-field="name">{{ trans('admin/categories/table.title') }}</th>
|
||||
<th data-sortable="true" data-field="category_type">{{ trans('general.type') }}</th>
|
||||
<th data-searchable="false" data-sortable="false" data-field="count">{{ trans('general.assets') }}</th>
|
||||
<th data-searchable="false" data-sortable="true" data-field="acceptance">{{ trans('admin/categories/table.require_acceptance') }}</th>
|
||||
<th data-searchable="false" data-sortable="true" data-field="eula">{{ trans('admin/categories/table.eula_text') }}</th>
|
||||
<th data-searchable="false" data-sortable="false" data-field="assets_count">{{ trans('general.assets') }}</th>
|
||||
<th data-searchable="false" data-sortable="true" data-field="require_acceptance">{{ trans('admin/categories/table.require_acceptance') }}</th>
|
||||
<th data-searchable="false" data-sortable="true" data-field="use_default_eula">{{ trans('admin/categories/table.eula_text') }}</th>
|
||||
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="actions">{{ trans('table.actions') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
|
|
@ -48,7 +48,19 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
|
|||
]
|
||||
);
|
||||
|
||||
|
||||
Route::resource('categories', 'CategoriesController',
|
||||
['names' =>
|
||||
[
|
||||
'index' => 'api.categories.index',
|
||||
'show' => 'api.categories.show',
|
||||
'update' => 'api.categories.update',
|
||||
'store' => 'api.categories.store',
|
||||
'destroy' => 'api.categories.destroy'
|
||||
],
|
||||
'except' => ['edit', 'create'],
|
||||
'parameters' => ['category' => 'category_id']
|
||||
]
|
||||
);
|
||||
|
||||
|
||||
Route::resource('companies', 'CompaniesController',
|
||||
|
|
Loading…
Reference in a new issue