implement SearchUsers use case
This commit is contained in:
parent
31a807f0ae
commit
d917e76f1b
5 changed files with 93 additions and 0 deletions
|
|
@ -43,6 +43,25 @@ class EloquentUserRepository implements UserRepository
|
|||
return $model === null ? null : $this->toDomain($model);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return User[]
|
||||
*/
|
||||
public function search(string $query): array
|
||||
{
|
||||
$like = strtolower($query).'%';
|
||||
$models = UserModel::query()
|
||||
->whereRaw('LOWER(display_name) LIKE ?', [$like])
|
||||
->orWhereRaw('LOWER(email) LIKE ?', [$like])
|
||||
->orderBy('display_name')
|
||||
->get();
|
||||
|
||||
return $models->map(
|
||||
function (UserModel $model) {
|
||||
return $this->toDomain($model);
|
||||
},
|
||||
)->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
|
|
|
|||
29
backend/app/User/UseCases/SearchUsers/SearchUsers.php
Normal file
29
backend/app/User/UseCases/SearchUsers/SearchUsers.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace App\User\UseCases\SearchUsers;
|
||||
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\User\User;
|
||||
use App\User\UserRepository;
|
||||
|
||||
class SearchUsers
|
||||
{
|
||||
public function __construct(
|
||||
private UserRepository $userRepo,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return User[]
|
||||
*
|
||||
* @throws BadRequestException
|
||||
*/
|
||||
public function execute(SearchUsersRequest $request): array
|
||||
{
|
||||
$query = trim($request->query);
|
||||
if ($query === '') {
|
||||
throw new BadRequestException('query is required');
|
||||
}
|
||||
|
||||
return $this->userRepo->search($query);
|
||||
}
|
||||
}
|
||||
10
backend/app/User/UseCases/SearchUsers/SearchUsersRequest.php
Normal file
10
backend/app/User/UseCases/SearchUsers/SearchUsersRequest.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace App\User\UseCases\SearchUsers;
|
||||
|
||||
class SearchUsersRequest
|
||||
{
|
||||
public function __construct(
|
||||
public string $query,
|
||||
) {}
|
||||
}
|
||||
|
|
@ -15,4 +15,9 @@ interface UserRepository
|
|||
public function findByDisplayName(string $displayName): ?User;
|
||||
|
||||
public function update(User $user): User;
|
||||
|
||||
/**
|
||||
* @return User[]
|
||||
*/
|
||||
public function search(string $query): array;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue