Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Http/Controllers/BaseRestfulController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class BaseRestfulController extends Controller
*/
public static $transformer = null;

/**
* @var array|string|\Spatie\QueryBuilder\Sorts\Sort
*/
public static $allowedSorts = null;

/**
* RestfulController constructor.
*
Expand Down
13 changes: 11 additions & 2 deletions src/Http/Controllers/RestfulController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Spatie\QueryBuilder\QueryBuilder;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Cache;
Expand Down Expand Up @@ -47,9 +48,17 @@ public function getAll()
}), $this->getTransformer());
}

$query = $model::with($model::getCollectionWith());
$query = QueryBuilder::for($model::with($model::getCollectionWith()), Request());
$this->qualifyCollectionQuery($query);

if ($sorts = $model::getAllowedSorts()) {
$query = $query->allowedSorts($sorts);
}

if ($filters = $model::getAllowedFilters()) {
$query = $query->allowedFilters($filters);
}

// Handle pagination, if applicable
$perPage = $model->getPerPage();
if ($perPage) {
Expand All @@ -58,7 +67,7 @@ public function getAll()
$perPage = intval(request()->input('per_page'));
}

$paginator = $query->paginate($perPage);
$paginator = $query->paginate($perPage)->appends(request()->only(['filter', 'sort']));

return $this->response->paginator($paginator, $this->getTransformer());
} else {
Expand Down
18 changes: 18 additions & 0 deletions src/Models/RestfulModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class RestfulModel extends Model
*/
public static $transformer = null;

public static $allowedSorts = null;

public static $allowedFilters = null;

/**
* Return the validation rules for this model
*
Expand Down Expand Up @@ -201,6 +205,20 @@ public static function getCollectionWith()
}
}

public static function getAllowedSorts()
{
if (! is_null(static::$allowedSorts)) {
return static::$allowedSorts;
}
}

public static function getAllowedFilters()
{
if (! is_null(static::$allowedFilters)) {
return static::$allowedFilters;
}
}

/************************************************************
* Extending Laravel Functions Below
***********************************************************/
Expand Down
10 changes: 10 additions & 0 deletions test/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ class User extends BaseModel implements
*/
public static $localWith = ['primaryRole', 'roles'];

/**
* @var array Sortable attributes
*/
public static $allowedSorts = ['name'];

/**
* @var array attribute filters
*/
public static $allowedFilters = ['email'];

/**
* The attributes that are mass assignable.
*
Expand Down
29 changes: 29 additions & 0 deletions test/config/query-builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

return [

/*
* By default the package will use the `include`, `filter`, `sort`
* and `fields` query parameters as described in the readme.
*
* You can customize these query string parameters here.
*/
'parameters' => [
'include' => 'include',

'filter' => 'filter',

'sort' => 'sort',

'fields' => 'fields',

'append' => 'append',
],

/*
* Related model counts are included using the relationship name suffixed with this string.
* For example: GET /users?include=postsCount
*/
'count_suffix' => 'Count',

];
2 changes: 1 addition & 1 deletion test/copy-over-deps.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

$sourceDir = '/www/laravel/bp-demo';
$migrationsDir = './database/migrations';
$configsToCopy = ['api.php', 'auth.php', 'jwt.php'];
$configsToCopy = ['api.php', 'auth.php', 'jwt.php', 'query-builder.php'];

// Copy over database dir
echo `rm -rf ./database/*`;
Expand Down
4 changes: 4 additions & 0 deletions test/tests/Api/Get/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public function testGet()
$jsonResponse->assertStatus(200);


$jsonResponse = $this->actingAsAdmin()
->json('GET', '/users?sort=-name&filter[email]=admin.com');

$jsonResponse->assertStatus(200);

}
}
2 changes: 2 additions & 0 deletions test/tests/SetupTestApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ protected function getEnvironmentSetUp($app)
$app['config']->set('api', include __DIR__ . '/../config/api.php');
$app['config']->set('auth', include __DIR__ . '/../config/auth.php');
$app['config']->set('jwt', include __DIR__ . '/../config/jwt.php');
$app['config']->set('query-builder', include __DIR__ . '/../config/query-builder.php');
}

/**
Expand All @@ -74,6 +75,7 @@ protected function getPackageProviders($app)
\Specialtactics\L5Api\L5ApiServiceProvider::class,
\Specialtactics\L5Api\Test\Mocks\AppServiceProvider::class,
\Specialtactics\L5Api\Test\Mocks\RouteServiceProvider::class,
\Spatie\QueryBuilder\QueryBuilderServiceProvider::class,
];
}

Expand Down