mirror of
https://github.com/snipe/snipe-it.git
synced 2025-02-02 08:21:09 -08:00
Slightly different approach
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
ee589ca112
commit
9365c62d08
|
@ -11,6 +11,8 @@ use Illuminate\Http\Request;
|
|||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Requests\ImageUploadRequest;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Contracts\Support\Jsonable;
|
||||
|
||||
class CategoriesController extends Controller
|
||||
{
|
||||
|
@ -19,9 +21,8 @@ class CategoriesController extends Controller
|
|||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @since [v4.0]
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index(Request $request) : array
|
||||
public function index(Request $request) : JsonResponse
|
||||
{
|
||||
$this->authorize('view', Category::class);
|
||||
$allowed_columns = [
|
||||
|
@ -91,18 +92,12 @@ class CategoriesController extends Controller
|
|||
$categories->where('checkin_email', '=', $request->input('checkin_email'));
|
||||
}
|
||||
|
||||
// Make sure the offset and limit are actually integers and do not exceed system limits
|
||||
$offset = ($request->input('offset') > $categories->count()) ? $categories->count() : app('api_offset_value');
|
||||
$limit = app('api_limit_value');
|
||||
|
||||
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
||||
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'assets_count';
|
||||
$categories->orderBy($sort, $order);
|
||||
$paginator = $categories->paginate(app('per_page'));
|
||||
|
||||
$total = $categories->count();
|
||||
$categories = $categories->skip($offset)->take($limit)->get();
|
||||
|
||||
return (new CategoriesTransformer)->transformCategories($categories, $total);
|
||||
return response()->json((new CategoriesTransformer)->transformCategories($paginator));
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -5,22 +5,23 @@ namespace App\Http\Transformers;
|
|||
use App\Helpers\Helper;
|
||||
use App\Models\Category;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
class CategoriesTransformer
|
||||
{
|
||||
public function transformCategories(Collection $categorys, $total)
|
||||
public function transformCategories(LengthAwarePaginator $paginator)
|
||||
{
|
||||
$array = [];
|
||||
foreach ($categorys as $category) {
|
||||
foreach ($paginator->items() as $category) {
|
||||
$array[] = self::transformCategory($category);
|
||||
}
|
||||
|
||||
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
||||
return (new DatatablesTransformer)->transformDatatables($array, $paginator);
|
||||
}
|
||||
|
||||
public function transformCategory(Category $category = null)
|
||||
public function transformCategory(Category $category = null) : ?array
|
||||
|
||||
{
|
||||
|
||||
// We only ever use item_count for categories in this transformer, so it makes sense to keep it
|
||||
|
|
|
@ -4,11 +4,18 @@ namespace App\Http\Transformers;
|
|||
|
||||
class DatatablesTransformer
|
||||
{
|
||||
public function transformDatatables($objects, $total = null)
|
||||
public function transformDatatables($objects, $paginator)
|
||||
{
|
||||
(isset($total)) ? $objects_array['total'] = $total : $objects_array['total'] = count($objects);
|
||||
$objects_array['total'] = $paginator->total();
|
||||
$objects_array['rows'] = $objects;
|
||||
$objects_array['meta'] = [
|
||||
'per_page' => $paginator->perPage(),
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
'next_page_url' => $paginator->nextPageUrl(),
|
||||
'prev_page_url' => $paginator->previousPageUrl(),
|
||||
];
|
||||
|
||||
return $objects_array;
|
||||
return ($objects_array);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ class SettingsServiceProvider extends ServiceProvider
|
|||
|
||||
|
||||
// Make sure the limit is actually set, is an integer and does not exceed system limits
|
||||
\App::singleton('api_limit_value', function () {
|
||||
app()->singleton('api_limit_value', function () {
|
||||
$limit = config('app.max_results');
|
||||
$int_limit = intval(request('limit'));
|
||||
|
||||
|
@ -43,127 +43,136 @@ class SettingsServiceProvider extends ServiceProvider
|
|||
});
|
||||
|
||||
// Make sure the offset is actually set and is an integer
|
||||
\App::singleton('api_offset_value', function () {
|
||||
app()->singleton('api_offset_value', function () {
|
||||
$offset = intval(request('offset'));
|
||||
return $offset;
|
||||
});
|
||||
|
||||
|
||||
app()->singleton('per_page', function () {
|
||||
|
||||
if (request('limit')) {
|
||||
return (int) request('limit');
|
||||
}
|
||||
return app('api_limit_value');
|
||||
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Set some common variables so that they're globally available.
|
||||
* The paths should always be public (versus private uploads)
|
||||
*/
|
||||
|
||||
// Model paths and URLs
|
||||
|
||||
|
||||
\App::singleton('eula_pdf_path', function () {
|
||||
|
||||
app()->singleton('eula_pdf_path', function () {
|
||||
return 'eula_pdf_path/';
|
||||
});
|
||||
|
||||
\App::singleton('assets_upload_path', function () {
|
||||
app()->singleton('assets_upload_path', function () {
|
||||
return 'assets/';
|
||||
});
|
||||
|
||||
\App::singleton('accessories_upload_path', function () {
|
||||
app()->singleton('accessories_upload_path', function () {
|
||||
return 'public/uploads/accessories/';
|
||||
});
|
||||
|
||||
\App::singleton('models_upload_path', function () {
|
||||
app()->singleton('models_upload_path', function () {
|
||||
return 'models/';
|
||||
});
|
||||
|
||||
\App::singleton('models_upload_url', function () {
|
||||
app()->singleton('models_upload_url', function () {
|
||||
return 'models/';
|
||||
});
|
||||
|
||||
// Categories
|
||||
\App::singleton('categories_upload_path', function () {
|
||||
app()->singleton('categories_upload_path', function () {
|
||||
return 'categories/';
|
||||
});
|
||||
|
||||
\App::singleton('categories_upload_url', function () {
|
||||
app()->singleton('categories_upload_url', function () {
|
||||
return 'categories/';
|
||||
});
|
||||
|
||||
// Locations
|
||||
\App::singleton('locations_upload_path', function () {
|
||||
app()->singleton('locations_upload_path', function () {
|
||||
return 'locations/';
|
||||
});
|
||||
|
||||
\App::singleton('locations_upload_url', function () {
|
||||
app()->singleton('locations_upload_url', function () {
|
||||
return 'locations/';
|
||||
});
|
||||
|
||||
// Users
|
||||
\App::singleton('users_upload_path', function () {
|
||||
app()->singleton('users_upload_path', function () {
|
||||
return 'avatars/';
|
||||
});
|
||||
|
||||
\App::singleton('users_upload_url', function () {
|
||||
app()->singleton('users_upload_url', function () {
|
||||
return 'users/';
|
||||
});
|
||||
|
||||
// Manufacturers
|
||||
\App::singleton('manufacturers_upload_path', function () {
|
||||
app()->singleton('manufacturers_upload_path', function () {
|
||||
return 'manufacturers/';
|
||||
});
|
||||
|
||||
\App::singleton('manufacturers_upload_url', function () {
|
||||
app()->singleton('manufacturers_upload_url', function () {
|
||||
return 'manufacturers/';
|
||||
});
|
||||
|
||||
// Suppliers
|
||||
\App::singleton('suppliers_upload_path', function () {
|
||||
app()->singleton('suppliers_upload_path', function () {
|
||||
return 'suppliers/';
|
||||
});
|
||||
|
||||
\App::singleton('suppliers_upload_url', function () {
|
||||
app()->singleton('suppliers_upload_url', function () {
|
||||
return 'suppliers/';
|
||||
});
|
||||
|
||||
// Departments
|
||||
\App::singleton('departments_upload_path', function () {
|
||||
app()->singleton('departments_upload_path', function () {
|
||||
return 'departments/';
|
||||
});
|
||||
|
||||
\App::singleton('departments_upload_url', function () {
|
||||
app()->singleton('departments_upload_url', function () {
|
||||
return 'departments/';
|
||||
});
|
||||
|
||||
// Company paths and URLs
|
||||
\App::singleton('companies_upload_path', function () {
|
||||
app()->singleton('companies_upload_path', function () {
|
||||
return 'companies/';
|
||||
});
|
||||
|
||||
\App::singleton('companies_upload_url', function () {
|
||||
app()->singleton('companies_upload_url', function () {
|
||||
return 'companies/';
|
||||
});
|
||||
|
||||
// Accessories paths and URLs
|
||||
\App::singleton('accessories_upload_path', function () {
|
||||
app()->singleton('accessories_upload_path', function () {
|
||||
return 'accessories/';
|
||||
});
|
||||
|
||||
\App::singleton('accessories_upload_url', function () {
|
||||
app()->singleton('accessories_upload_url', function () {
|
||||
return 'accessories/';
|
||||
});
|
||||
|
||||
// Consumables paths and URLs
|
||||
\App::singleton('consumables_upload_path', function () {
|
||||
app()->singleton('consumables_upload_path', function () {
|
||||
return 'consumables/';
|
||||
});
|
||||
|
||||
\App::singleton('consumables_upload_url', function () {
|
||||
app()->singleton('consumables_upload_url', function () {
|
||||
return 'consumables/';
|
||||
});
|
||||
|
||||
// Components paths and URLs
|
||||
\App::singleton('components_upload_path', function () {
|
||||
app()->singleton('components_upload_path', function () {
|
||||
return 'components/';
|
||||
});
|
||||
|
||||
\App::singleton('components_upload_url', function () {
|
||||
app()->singleton('components_upload_url', function () {
|
||||
return 'components/';
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue