Skip to content

Commit

Permalink
statsable
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanhSonITNIC committed Feb 23, 2024
1 parent b2142b0 commit cb01c96
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Here is a quick look at what you can do using API search method:
}
],
"gates": ["create", "view"],
"stats": true,
"page": 2,
"limit": 10
}
Expand Down
19 changes: 19 additions & 0 deletions src/Concerns/Statsable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Lomkit\Rest\Concerns;


trait Statsable
{
/**
* Custom stats resource.
*
* @param \Illuminate\Contracts\Database\Eloquent\Builder $query
*
* @return array
*/
public function stats(\Illuminate\Contracts\Database\Eloquent\Builder $query): array
{
return [];
}
}
2 changes: 2 additions & 0 deletions src/Http/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Lomkit\Rest\Concerns\Resource\Paginable;
use Lomkit\Rest\Concerns\Resource\Relationable;
use Lomkit\Rest\Concerns\Resource\Rulable;
use Lomkit\Rest\Concerns\Statsable;
use Lomkit\Rest\Http\Requests\RestRequest;
use Lomkit\Rest\Instructions\Instructionable;

Expand All @@ -28,6 +29,7 @@ class Resource implements \JsonSerializable
use Actionable;
use Instructionable;
use HasResourceHooks;
use Statsable;

/**
* The model the entry corresponds to.
Expand Down
33 changes: 21 additions & 12 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Lomkit\Rest\Contracts\QueryBuilder;
use Lomkit\Rest\Http\Requests\RestRequest;
use Lomkit\Rest\Relations\Relation;

Expand Down Expand Up @@ -127,11 +128,7 @@ public function toResponse($request)
$this->responsable->perPage(),
$this->responsable->currentPage(),
$this->responsable->getOptions(),
$this->resource->isGatingEnabled() && in_array('create', $request->input('search.gates', [])) ? [
config('rest.gates.key') => [
config('rest.gates.names.authorized_to_create') => $this->resource->authorizedTo('create', $this->resource::newModel()::class),
],
] : []
$this->meta($request),
);

$restLengthAwarePaginator->through(function ($model) use ($request) {
Expand All @@ -147,16 +144,28 @@ public function toResponse($request)

return [
'data' => $data ?? $this->map($this->responsable, $this->modelToResponse($this->responsable, $this->resource, $request->input('search', []))),
'meta' => array_merge(
$this->resource->isGatingEnabled() && in_array('create', $request->input('search.gates', [])) ? [
config('rest.gates.key') => [
config('rest.gates.names.authorized_to_create') => $this->resource->authorizedTo('create', $this->resource::newModel()::class),
],
] : []
),
'meta' => $this->meta($request),
];
}

protected function meta($request)
{
$meta = [];

if($this->resource->isGatingEnabled() && in_array('create', $request->input('search.gates', []))) {
$meta = array_merge($meta, [config('rest.gates.key') => [
config('rest.gates.names.authorized_to_create') => $this->resource->authorizedTo('create', $this->resource::newModel()::class),
]]);
}

if((bool) $request->input('search.stats')) {
$query = app()->make(QueryBuilder::class, ['resource' => $this->resource, 'query' => null])->search($request->input('search', []));
$meta = array_merge($meta, ['stats' => $this->resource->stats($query)]);
}

return $meta;
}

/**
* This map on each model returned by the API, use it at your ease.
*
Expand Down

0 comments on commit cb01c96

Please sign in to comment.