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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,6 +63,36 @@ class FakeUserRepository implements UserRepository
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return User[]
|
||||
*/
|
||||
public function search(string $query): array
|
||||
{
|
||||
$needle = strtolower($query);
|
||||
$results = [];
|
||||
foreach ($this->existingUsers as $user) {
|
||||
$displayName = strtolower($user->getDisplayName());
|
||||
$email = strtolower($user->getEmail()->value());
|
||||
if (
|
||||
str_starts_with($displayName, $needle)
|
||||
|| str_starts_with($email, $needle)
|
||||
) {
|
||||
$results[] = $this->copy($user);
|
||||
}
|
||||
}
|
||||
usort(
|
||||
$results,
|
||||
function (User $left, User $right) {
|
||||
return strcmp(
|
||||
$left->getDisplayName(),
|
||||
$right->getDisplayName(),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue