Compare commits

...

4 Commits

Author SHA1 Message Date
snipe
276950072f This may be more work than we want to do here tbh
Signed-off-by: snipe <snipe@snipe.net>
2024-07-18 14:14:40 +01:00
snipe
52344c5574 Better
Signed-off-by: snipe <snipe@snipe.net>
2024-07-18 13:02:22 +01:00
snipe
8cfca8bff7 Ack
Signed-off-by: snipe <snipe@snipe.net>
2024-07-18 12:56:28 +01:00
snipe
f400b38c9c First stab
Signed-off-by: snipe <snipe@snipe.net>
2024-07-18 11:53:30 +01:00
9 changed files with 313 additions and 24 deletions

View File

@@ -4,22 +4,37 @@ namespace App\Http\Controllers\Api;
use App\Helpers\Helper; use App\Helpers\Helper;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Transformers\CategoriesTransformer;
use App\Http\Transformers\SelectlistTransformer; use App\Http\Transformers\SelectlistTransformer;
use App\Models\Category; use App\Models\Category;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use App\Http\Requests\ImageUploadRequest; use App\Http\Requests\ImageUploadRequest;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Models\Traits\ApiResponder;
use App\Http\Serializers\BootstrapTablesSerializer;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Collection;
use League\Fractal\Serializer\DataArraySerializer;
use League\Fractal\Serializer\ArraySerializer;
use App\Http\Transformers\CategoriesTransformer;
use League\Fractal\Manager;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use Spatie\Fractalistic\Fractal;
use function Illuminate\Events\queueable;
class CategoriesController extends Controller class CategoriesController extends Controller
{ {
use ApiResponder;
/** /**
* Display a listing of the resource. * Display a listing of the resource.
* *
* @author [A. Gianotto] [<snipe@snipe.net>] * @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0] * @since [v4.0]
* @return \Illuminate\Http\Response
*/ */
public function index(Request $request) : array public function index(Request $request) : array
{ {
@@ -91,18 +106,20 @@ class CategoriesController extends Controller
$categories->where('checkin_email', '=', $request->input('checkin_email')); $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'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'assets_count'; $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'assets_count';
$categories->orderBy($sort, $order); $categories->orderBy($sort, $order);
$total = $categories->count(); $paginator = $categories->paginate(app('page_number'));
$categories = $categories->skip($offset)->take($limit)->get(); $total_results = $paginator->total();
$results = $paginator->getCollection();
return (new CategoriesTransformer)->transformCategories($categories, $total); return Fractal::create()
->collection($results, new CategoriesTransformer())
->serializeWith(new BootstrapTablesSerializer())
->addMeta(['total' => $total_results])
->paginateWith(new IlluminatePaginatorAdapter($paginator))
->toArray();
} }
@@ -141,7 +158,8 @@ class CategoriesController extends Controller
{ {
$this->authorize('view', Category::class); $this->authorize('view', Category::class);
$category = Category::withCount('assets as assets_count', 'accessories as accessories_count', 'consumables as consumables_count', 'components as components_count', 'licenses as licenses_count')->findOrFail($id); $category = Category::withCount('assets as assets_count', 'accessories as accessories_count', 'consumables as consumables_count', 'components as components_count', 'licenses as licenses_count')->findOrFail($id);
return (new CategoriesTransformer)->transformCategory($category); $transformer = $category->first()->transformer;
return $this->transformData($category, $transformer);
} }

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Http\Serializers;
use League\Fractal\Pagination\CursorInterface;
use League\Fractal\Serializer\SerializerAbstract;
use League\Fractal\Resource\ResourceInterface;
use League\Fractal\Pagination\PaginatorInterface;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Serializer\JsonApiSerializer;
class BootstrapTablesSerializer extends JsonApiSerializer
{
public function collection($resourceKey, array $data): array
{
return [
'total' => count($data),
'rows' => $data
];
}
public function item($resourceKey, array $data): array
{
if ($resourceKey) {
return [$resourceKey => $data];
}
return $data;
}
}

View File

@@ -7,20 +7,11 @@ use App\Models\Category;
use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Gate;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use League\Fractal\TransformerAbstract;
class CategoriesTransformer class CategoriesTransformer extends TransformerAbstract
{ {
public function transformCategories(Collection $categorys, $total) public function transform(Category $category = null)
{
$array = [];
foreach ($categorys as $category) {
$array[] = self::transformCategory($category);
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
public function transformCategory(Category $category = null)
{ {
// We only ever use item_count for categories in this transformer, so it makes sense to keep it // We only ever use item_count for categories in this transformer, so it makes sense to keep it
@@ -76,4 +67,6 @@ class CategoriesTransformer
return $array; return $array;
} }
} }
} }

View File

@@ -2,6 +2,8 @@
namespace App\Http\Transformers; namespace App\Http\Transformers;
use Illuminate\Pagination\LengthAwarePaginator;
class DatatablesTransformer class DatatablesTransformer
{ {
public function transformDatatables($objects, $total = null) public function transformDatatables($objects, $total = null)

View File

@@ -11,6 +11,8 @@ use Illuminate\Support\Facades\Gate;
use Watson\Validating\ValidatingTrait; use Watson\Validating\ValidatingTrait;
use App\Helpers\Helper; use App\Helpers\Helper;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use App\Http\Transformers\CategoriesTransformer;
use App\Presenters\CategoryPresenter;
/** /**
* Model for Categories. Categories are a higher-level group * Model for Categories. Categories are a higher-level group
@@ -24,7 +26,9 @@ class Category extends SnipeModel
{ {
use HasFactory; use HasFactory;
protected $presenter = \App\Presenters\CategoryPresenter::class; protected $presenter = CategoryPresenter::class;
public $transformer = CategoriesTransformer::class;
use Presentable; use Presentable;
use SoftDeletes; use SoftDeletes;

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Models\Traits;
trait ApiResponder
{
protected function transformData($data, $transformer)
{
$transformation = fractal($data, new $transformer);
return $transformation->toArray();
}
}

View File

@@ -48,6 +48,24 @@ class SettingsServiceProvider extends ServiceProvider
return $offset; return $offset;
}); });
// Make sure the offset is actually set and is an integer
\App::singleton('page_number', function ($results) {
if (request('page_number')) {
return (int) request('page_number');
}
$offset = app('api_offset_value');
\Log::error('offset is: '.$offset);
$limit = app('api_limit_value');
\Log::error('limit is: '.$limit);
$page_number = (intval($offset / $limit) + 1);
\Log::error('page number is: '.$page_number);
return $page_number;
});
/** /**
* Set some common variables so that they're globally available. * Set some common variables so that they're globally available.

View File

@@ -62,6 +62,7 @@
"pragmarx/google2fa-laravel": "^1.3", "pragmarx/google2fa-laravel": "^1.3",
"rollbar/rollbar-laravel": "^8.0", "rollbar/rollbar-laravel": "^8.0",
"spatie/laravel-backup": "^8.8", "spatie/laravel-backup": "^8.8",
"spatie/laravel-fractal": "^6.2",
"spatie/laravel-ignition": "^2.0", "spatie/laravel-ignition": "^2.0",
"tecnickcom/tc-lib-barcode": "^1.15", "tecnickcom/tc-lib-barcode": "^1.15",
"tecnickcom/tcpdf": "^6.5", "tecnickcom/tcpdf": "^6.5",

214
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "51e716db4ccd70bf942062789f7303ad", "content-hash": "26ebaa5d840f6fdffd729c34ce0890d3",
"packages": [ "packages": [
{ {
"name": "alek13/slack", "name": "alek13/slack",
@@ -4058,6 +4058,76 @@
}, },
"time": "2024-05-06T20:05:52+00:00" "time": "2024-05-06T20:05:52+00:00"
}, },
{
"name": "league/fractal",
"version": "0.20.1",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/fractal.git",
"reference": "8b9d39b67624db9195c06f9c1ffd0355151eaf62"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/fractal/zipball/8b9d39b67624db9195c06f9c1ffd0355151eaf62",
"reference": "8b9d39b67624db9195c06f9c1ffd0355151eaf62",
"shasum": ""
},
"require": {
"php": ">=7.4"
},
"require-dev": {
"doctrine/orm": "^2.5",
"illuminate/contracts": "~5.0",
"mockery/mockery": "^1.3",
"pagerfanta/pagerfanta": "~1.0.0",
"phpstan/phpstan": "^1.4",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "~3.4",
"vimeo/psalm": "^4.22",
"zendframework/zend-paginator": "~2.3"
},
"suggest": {
"illuminate/pagination": "The Illuminate Pagination component.",
"pagerfanta/pagerfanta": "Pagerfanta Paginator",
"zendframework/zend-paginator": "Zend Framework Paginator"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "0.20.x-dev"
}
},
"autoload": {
"psr-4": {
"League\\Fractal\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Phil Sturgeon",
"email": "me@philsturgeon.uk",
"homepage": "http://philsturgeon.uk/",
"role": "Developer"
}
],
"description": "Handle the output of complex data structures ready for API output.",
"homepage": "http://fractal.thephpleague.com/",
"keywords": [
"api",
"json",
"league",
"rest"
],
"support": {
"issues": "https://github.com/thephpleague/fractal/issues",
"source": "https://github.com/thephpleague/fractal/tree/0.20.1"
},
"time": "2022-04-11T12:47:17+00:00"
},
{ {
"name": "league/mime-type-detection", "name": "league/mime-type-detection",
"version": "1.15.0", "version": "1.15.0",
@@ -8071,6 +8141,67 @@
], ],
"time": "2024-06-12T14:39:14+00:00" "time": "2024-06-12T14:39:14+00:00"
}, },
{
"name": "spatie/fractalistic",
"version": "2.9.5",
"source": {
"type": "git",
"url": "https://github.com/spatie/fractalistic.git",
"reference": "6f12686a03d035f4558d166989c62aa93bde2151"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/fractalistic/zipball/6f12686a03d035f4558d166989c62aa93bde2151",
"reference": "6f12686a03d035f4558d166989c62aa93bde2151",
"shasum": ""
},
"require": {
"league/fractal": "^0.20.1",
"php": "^7.4|^8.0"
},
"require-dev": {
"illuminate/pagination": "~5.3.0|~5.4.0",
"phpunit/phpunit": "^9.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Spatie\\Fractalistic\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"description": "A developer friendly wrapper around Fractal",
"homepage": "https://github.com/spatie/fractalistic",
"keywords": [
"api",
"fractal",
"fractalistic",
"spatie",
"transform"
],
"support": {
"issues": "https://github.com/spatie/fractalistic/issues",
"source": "https://github.com/spatie/fractalistic/tree/2.9.5"
},
"funding": [
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2022-04-21T12:26:22+00:00"
},
{ {
"name": "spatie/ignition", "name": "spatie/ignition",
"version": "1.15.0", "version": "1.15.0",
@@ -8253,6 +8384,87 @@
], ],
"time": "2024-06-04T11:31:33+00:00" "time": "2024-06-04T11:31:33+00:00"
}, },
{
"name": "spatie/laravel-fractal",
"version": "6.2.1",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-fractal.git",
"reference": "0a30630d2ab49590af118172c03c99c0d838dab1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-fractal/zipball/0a30630d2ab49590af118172c03c99c0d838dab1",
"reference": "0a30630d2ab49590af118172c03c99c0d838dab1",
"shasum": ""
},
"require": {
"illuminate/contracts": "^8.0|^9.0|^10.0|^11.0",
"illuminate/support": "^8.0|^9.0|^10.0|^11.0",
"league/fractal": "^0.20.1|^0.20",
"nesbot/carbon": "^2.63|^3.0",
"php": "^8.0",
"spatie/fractalistic": "^2.9.5|^2.9",
"spatie/laravel-package-tools": "^1.11"
},
"require-dev": {
"ext-json": "*",
"orchestra/testbench": "^7.0|^8.0|^9.0",
"pestphp/pest": "^1.22|^2.34"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Spatie\\Fractal\\FractalServiceProvider"
],
"aliases": {
"Fractal": "Spatie\\Fractal\\Facades\\Fractal"
}
}
},
"autoload": {
"files": [
"src/helpers.php"
],
"psr-4": {
"Spatie\\Fractal\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"description": "An easy to use Fractal integration for Laravel applications",
"homepage": "https://github.com/spatie/laravel-fractal",
"keywords": [
"api",
"fractal",
"laravel",
"laravel-fractal",
"lumen",
"spatie",
"transform"
],
"support": {
"source": "https://github.com/spatie/laravel-fractal/tree/6.2.1"
},
"funding": [
{
"url": "https://spatie.be/open-source/support-us",
"type": "custom"
}
],
"time": "2024-06-04T09:33:08+00:00"
},
{ {
"name": "spatie/laravel-ignition", "name": "spatie/laravel-ignition",
"version": "2.8.0", "version": "2.8.0",