diff --git a/app/Http/Controllers/Api/CategoriesController.php b/app/Http/Controllers/Api/CategoriesController.php new file mode 100644 index 0000000000..046b29b207 --- /dev/null +++ b/app/Http/Controllers/Api/CategoriesController.php @@ -0,0 +1,121 @@ +] + * @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] [] + * @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] [] + * @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] [] + * @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] [] + * @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'))); + + } +} diff --git a/app/Http/Controllers/CategoriesController.php b/app/Http/Controllers/CategoriesController.php index 84fcedfba7..04582258ec 100755 --- a/app/Http/Controllers/CategoriesController.php +++ b/app/Http/Controllers/CategoriesController.php @@ -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] [] - * @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) { diff --git a/resources/views/categories/index.blade.php b/resources/views/categories/index.blade.php index b566bb2783..4244a6c5f8 100755 --- a/resources/views/categories/index.blade.php +++ b/resources/views/categories/index.blade.php @@ -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 @@ {{ trans('general.id') }} {{ trans('admin/categories/table.title') }} {{ trans('general.type') }} - {{ trans('general.assets') }} - {{ trans('admin/categories/table.require_acceptance') }} - {{ trans('admin/categories/table.eula_text') }} + {{ trans('general.assets') }} + {{ trans('admin/categories/table.require_acceptance') }} + {{ trans('admin/categories/table.eula_text') }} {{ trans('table.actions') }} diff --git a/routes/api.php b/routes/api.php index 0c43a9eeaa..2c371a0954 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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',