Skip to content

Commit

Permalink
search both columns in journal instead of fuzzy search - too many jou…
Browse files Browse the repository at this point in the history
…rnals for Fuzzy
  • Loading branch information
vincentauger committed Feb 27, 2023
1 parent 76f78d0 commit 4736f9b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
38 changes: 38 additions & 0 deletions app/Filters/MultiColumnFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Filters;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Spatie\QueryBuilder\Filters\Filter;

class MultiColumnFilter implements Filter
{
/**
* @var array
*/
private $columns;

public function __construct(...$columns)
{
$this->columns = $columns;
}

public function __invoke(Builder $query, $value, string $property)
{
$attributes = $this->columns;

// is database engine postgresql? if so, use ILIKE as it is case insensitive
$like = config('database.default') === 'pgsql' ? 'ILIKE' : 'LIKE';

$query->where(function (Builder $query) use ($attributes, $value, $like) {
foreach (Arr::wrap($attributes) as $attribute) {
$query->orWhere(function ($query) use ($attribute, $value, $like) {
$query->orWhere($attribute, $like, "%{$value}%");
});
}
});

return $this;
}
}
10 changes: 5 additions & 5 deletions app/Http/Resources/AuthorResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
use Auth;
use Illuminate\Http\Resources\Json\JsonResource;

/*
* Resource for Author model
* @extends JsonResource<\App\Models\Author>
*/
/**
* Resource for Author model.
*
* @extends JsonResource<\App\Models\Author>
*/
class AuthorResource extends JsonResource
{
/**
Expand Down
4 changes: 2 additions & 2 deletions app/Queries/JournalListQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Queries;

use App\Filters\FuzzyFilter;
use App\Filters\MultiColumnFilter;
use App\Models\Journal;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
Expand All @@ -20,7 +20,7 @@ public function __construct()
AllowedFilter::exact('id'),
AllowedFilter::partial('title_en'),
AllowedFilter::partial('title_fr'),
AllowedFilter::custom('search', new FuzzyFilter('title_en', 'title_fr')),
AllowedFilter::custom('search', new MultiColumnFilter('title_en', 'title_fr')),
AllowedFilter::scope('dfo_series'),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ parameters:
# excludePaths:
# - ./*/*/FileToBeExcluded.php
#
# checkMissingIterableValueType: false
# checkMissingIterableValueType: false
5 changes: 4 additions & 1 deletion resources/src/models/Journal/components/JournalSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ onMounted(async () => {
}
});
const filterJournals = async (val: string, update, abort) => {
const filterJournals = async (
val: string,
update: (arg: () => Promise<void>) => void
) => {
lastSearchTerm.value = val;
update(async () => {
if (val !== '') {
Expand Down

0 comments on commit 4736f9b

Please sign in to comment.