initial commit
This commit is contained in:
34
app/Services/SearchProvider/DatabaseSearchProvider.php
Normal file
34
app/Services/SearchProvider/DatabaseSearchProvider.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\SearchProvider;
|
||||
|
||||
use App\Models\Contact;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
final class DatabaseSearchProvider implements SearchProvider
|
||||
{
|
||||
/**
|
||||
* @return LengthAwarePaginator<int, \App\Models\Contact>
|
||||
*/
|
||||
public function search(string $query, int $paginate): LengthAwarePaginator
|
||||
{
|
||||
return Contact::query()
|
||||
->select('*')
|
||||
->selectRaw(
|
||||
"CASE
|
||||
WHEN email LIKE CONCAT('%', ?::text, '%')
|
||||
THEN 1.0
|
||||
ELSE ts_rank(ts_name, plainto_tsquery('simple', ?::text))
|
||||
END AS rank",
|
||||
[$query, $query]
|
||||
)
|
||||
->whereRaw(
|
||||
"ts_name @@ plainto_tsquery('simple', ?::text) OR email LIKE CONCAT('%', ?::text, '%')",
|
||||
[$query, $query]
|
||||
)
|
||||
->orderByDesc('rank')
|
||||
->paginate($paginate);
|
||||
}
|
||||
}
|
||||
16
app/Services/SearchProvider/SearchProvider.php
Normal file
16
app/Services/SearchProvider/SearchProvider.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\SearchProvider;
|
||||
|
||||
use App\Data\Contact;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
interface SearchProvider
|
||||
{
|
||||
/**
|
||||
* @return LengthAwarePaginator<int, \App\Models\Contact>
|
||||
*/
|
||||
public function search(string $query, int $paginate): LengthAwarePaginator;
|
||||
}
|
||||
Reference in New Issue
Block a user