Skip to content

Commit 86ae79a

Browse files
committed
Patch negation filter
1 parent 6ab82a7 commit 86ae79a

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/Http/Controllers/ProfileController.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
77
use Binaryk\LaravelRestify\Repositories\Repository;
88
use Binaryk\LaravelRestify\Services\Search\RepositorySearchService;
9+
use Illuminate\Support\Facades\Auth;
910

1011
class ProfileController extends RepositoryController
1112
{
@@ -28,7 +29,7 @@ public function __invoke(RestifyRequest $request)
2829
$meta = [];
2930

3031
if (method_exists($user, 'profile')) {
31-
$meta = (array) call_user_func([$user, 'profile'], $request);
32+
$meta = (array)call_user_func([$user, 'profile'], $request);
3233
}
3334

3435
return $this->response()
@@ -40,12 +41,12 @@ public function guessRepository(RestifyRequest $request): ?Repository
4041
{
4142
$repository = $request->repository('users');
4243

43-
if (! $repository) {
44+
if (!$repository) {
4445
return null;
4546
}
4647

4748
if (method_exists($repository, 'canUseForProfile')) {
48-
if (! call_user_func([$repository, 'canUseForProfile'], $request)) {
49+
if (!call_user_func([$repository, 'canUseForProfile'], $request)) {
4950
return null;
5051
}
5152
}
@@ -54,7 +55,7 @@ public function guessRepository(RestifyRequest $request): ?Repository
5455
$request, $repository
5556
), function ($query) use ($request, $repository) {
5657
$repository::indexQuery($request, $query);
57-
})->firstOrFail();
58+
})->whereKey(Auth::id())->firstOrFail();
5859

5960
return $repository->withResource($user);
6061
}

src/Services/Search/RepositorySearchService.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = [])
3131
foreach ($this->repository->getMatchByFields() as $key => $type) {
3232
$negation = false;
3333

34-
if (! $request->has($key) && ! data_get($extra, "match.$key")) {
34+
if ($request->has('-' . $key)) {
35+
$negation = true;
36+
}
37+
38+
if (!$request->has($negation ? '-' . $key : $key) && !data_get($extra, "match.$key")) {
3539
continue;
3640
}
3741

38-
$match = $request->input($key, data_get($extra, "match.$key"));
42+
$match = $request->input($negation ? '-' . $key : $key, data_get($extra, "match.$key"));
3943

40-
if (substr($key, 0, 1) === '-') {
41-
$negation = true;
44+
if ($negation) {
4245
$key = Str::after($key, '-');
4346
}
4447

@@ -49,7 +52,11 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = [])
4952
$field = $model->qualifyColumn($key);
5053

5154
if ($match === 'null') {
52-
$query->whereNull($field);
55+
if ($negation) {
56+
$query->whereNotNull($field);
57+
} else {
58+
$query->whereNull($field);
59+
}
5360
} else {
5461
switch ($this->repository->getMatchByFields()[$key]) {
5562
case RestifySearchable::MATCH_TEXT:
@@ -69,7 +76,7 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = [])
6976
case RestifySearchable::MATCH_INTEGER:
7077
case 'number':
7178
case 'int':
72-
$query->where($field, $negation ? '!=' : '=', (int) $match);
79+
$query->where($field, $negation ? '!=' : '=', (int)$match);
7380
break;
7481
case RestifySearchable::MATCH_DATETIME:
7582
$query->whereDate($field, $negation ? '!=' : '=', $match);
@@ -107,7 +114,7 @@ public function prepareOrders(RestifyRequest $request, $query, $extra = [])
107114
}
108115

109116
if (empty($params) === true) {
110-
$this->setOrder($query, '+'.$this->repository->newModel()->getKeyName());
117+
$this->setOrder($query, '+' . $this->repository->newModel()->getKeyName());
111118
}
112119

113120
return $query;
@@ -151,7 +158,7 @@ public function prepareSearchFields(RestifyRequest $request, $query, $extra = []
151158
$likeOperator = $connectionType == 'pgsql' ? 'ilike' : 'like';
152159

153160
foreach ($this->repository->getSearchableFields() as $column) {
154-
$query->orWhere($model->qualifyColumn($column), $likeOperator, '%'.$search.'%');
161+
$query->orWhere($model->qualifyColumn($column), $likeOperator, '%' . $search . '%');
155162
}
156163
});
157164

@@ -204,12 +211,12 @@ public function setOrder($query, $param)
204211

205212
protected function applyIndexQuery(RestifyRequest $request, Repository $repository)
206213
{
207-
return fn ($query) => $repository::indexQuery($request, $query->with($repository::$with));
214+
return fn($query) => $repository::indexQuery($request, $query->with($repository::$with));
208215
}
209216

210217
protected function applyFilters(RestifyRequest $request, Repository $repository, $query)
211218
{
212-
if (! empty($request->filters)) {
219+
if (!empty($request->filters)) {
213220
$filters = json_decode(base64_decode($request->filters), true);
214221

215222
collect($filters)
@@ -235,7 +242,7 @@ protected function applyFilters(RestifyRequest $request, Repository $repository,
235242
return $matchingFilter;
236243
})
237244
->filter()
238-
->each(fn (Filter $filter) => $filter->filter($request, $query, $filter->value));
245+
->each(fn(Filter $filter) => $filter->filter($request, $query, $filter->value));
239246
}
240247

241248
return $query;

0 commit comments

Comments
 (0)