Skip to content
Merged
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
3 changes: 1 addition & 2 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Route::get('/search', '\\'.GlobalSearchController::class);

// Filters
Route::get('/{repository}/filters', '\\' . RepositoryFilterController::class);
Route::get('/{repository}/filters', '\\'.RepositoryFilterController::class);

// API CRUD
Route::get('/{repository}', '\\'.RepositoryIndexController::class);
Expand All @@ -22,4 +22,3 @@
Route::patch('/{repository}/{repositoryId}', '\\'.RepositoryUpdateController::class);
Route::put('/{repository}/{repositoryId}', '\\'.RepositoryUpdateController::class);
Route::delete('/{repository}/{repositoryId}', '\\'.RepositoryDestroyController::class);

1 change: 0 additions & 1 deletion src/BooleanFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ abstract class BooleanFilter extends Filter
{
public $type = 'boolean';


public function resolve(RestifyRequest $request, $filter)
{
$keyValues = collect($this->options($request))->mapWithKeys(function ($key) use ($filter) {
Expand Down
4 changes: 1 addition & 3 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ public function filters(RestifyRequest $request)
return [];
}



/**
* @param RestifyRequest $request
* @return FieldCollection
Expand Down Expand Up @@ -695,7 +693,7 @@ public static function uriTo(Model $model)

public function availableFilters(RestifyRequest $request)
{
return collect($this->filter($this->filters($request)))->each(fn(Filter $filter) => $filter->authorizedToSee($request))
return collect($this->filter($this->filters($request)))->each(fn (Filter $filter) => $filter->authorizedToSee($request))
->values();
}
}
1 change: 0 additions & 1 deletion src/SelectFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
abstract class SelectFilter extends Filter
{
public $type = 'select';

}
17 changes: 8 additions & 9 deletions src/Services/Search/RepositorySearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = [])
{
$model = $query->getModel();
foreach ($this->repository->getMatchByFields() as $key => $type) {
if (!$request->has($key) && !data_get($extra, "match.$key")) {
if (! $request->has($key) && ! data_get($extra, "match.$key")) {
continue;
}

Expand Down Expand Up @@ -60,7 +60,7 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = [])
case RestifySearchable::MATCH_INTEGER:
case 'number':
case 'int':
$query->where($field, '=', (int)$match);
$query->where($field, '=', (int) $match);
break;
}
}
Expand Down Expand Up @@ -125,7 +125,7 @@ public function prepareSearchFields(RestifyRequest $request, $query, $extra = []
$likeOperator = $connectionType == 'pgsql' ? 'ilike' : 'like';

foreach ($this->repository->getSearchableFields() as $column) {
$query->orWhere($model->qualifyColumn($column), $likeOperator, '%' . $search . '%');
$query->orWhere($model->qualifyColumn($column), $likeOperator, '%'.$search.'%');
}
});

Expand Down Expand Up @@ -176,12 +176,12 @@ public function setOrder($query, $param)

protected function applyIndexQuery(RestifyRequest $request, Repository $repository)
{
return fn($query) => $repository::indexQuery($request, $query);
return fn ($query) => $repository::indexQuery($request, $query);
}

protected function applyFilters(RestifyRequest $request, Repository $repository, $query)
{
if (!empty($request->filters)) {
if (! empty($request->filters)) {
$filters = json_decode(base64_decode($request->filters), true);

collect($filters)
Expand All @@ -191,21 +191,20 @@ protected function applyFilters(RestifyRequest $request, Repository $repository,
return $filter['class'] === $availableFilter->key();
});


if (is_null($matchingFilter)) {
return false;
}

if ($matchingFilter->invalidPayloadValue($request, $filter['value'])) {
if ($matchingFilter->invalidPayloadValue($request, $filter['value'])) {
return false;
};
}

$matchingFilter->resolve($request, $filter['value']);

return $matchingFilter;
})
->filter()
->each(fn(Filter $filter) => $filter->filter($request, $query, $filter->value));
->each(fn (Filter $filter) => $filter->filter($request, $query, $filter->value));
}

return $query;
Expand Down
26 changes: 13 additions & 13 deletions tests/Controllers/RepositoryFilterControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ public function test_can_get_available_filters()

public function test_the_boolean_filter_is_applied()
{
factory(Post::class)->create(['is_active' => false,]);
factory(Post::class)->create(['is_active' => true,]);
factory(Post::class)->create(['is_active' => false]);
factory(Post::class)->create(['is_active' => true]);

$filters = base64_encode(json_encode([
[
'class' => ActiveBooleanFilter::class,
'value' => [
'is_active' => false,
],
]
],
]));

$response = $this
->withoutExceptionHandling()
->getJson('restify-api/posts?filters=' . $filters)
->getJson('restify-api/posts?filters='.$filters)
->dump()
->assertStatus(200);

Expand All @@ -47,28 +47,28 @@ public function test_the_boolean_filter_is_applied()

public function test_the_select_filter_is_applied()
{
factory(Post::class)->create(['category' => 'movie',]);
factory(Post::class)->create(['category' => 'article',]);
factory(Post::class)->create(['category' => 'movie']);
factory(Post::class)->create(['category' => 'article']);

$filters = base64_encode(json_encode([
[
'class' => SelectCategoryFilter::class,
'value' => 'article'
]
'value' => 'article',
],
]));

$response = $this
->withExceptionHandling()
->getJson('restify-api/posts?filters=' . $filters)
->getJson('restify-api/posts?filters='.$filters)
->assertStatus(200);

$this->assertCount(1, $response->json('data'));
}

public function test_the_timestamp_filter_is_applied()
{
factory(Post::class)->create(['created_at' => now()->addYear(),]);
factory(Post::class)->create(['created_at' => now()->subYear(),]);
factory(Post::class)->create(['created_at' => now()->addYear()]);
factory(Post::class)->create(['created_at' => now()->subYear()]);

$filters = base64_encode(json_encode([
[
Expand All @@ -78,12 +78,12 @@ public function test_the_timestamp_filter_is_applied()
[
'class' => CreatedAfterDateFilter::class,
'value' => now()->addWeek()->timestamp,
]
],
]));

$response = $this
->withExceptionHandling()
->getJson('restify-api/posts?filters=' . $filters)
->getJson('restify-api/posts?filters='.$filters)
->dump()
->assertStatus(200);

Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Post/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function fieldsForStore(RestifyRequest $request)
public function filters(RestifyRequest $request)
{
return [
ActiveBooleanFilter::new()->canSee(fn() => true),
ActiveBooleanFilter::new()->canSee(fn () => true),
SelectCategoryFilter::new(),
CreatedAfterDateFilter::new(),
];
Expand Down