diff --git a/src/Api/Controller/APIAccountsController.php b/src/Api/Controller/APIAccountsController.php index 3ec62df..4ad3db7 100644 --- a/src/Api/Controller/APIAccountsController.php +++ b/src/Api/Controller/APIAccountsController.php @@ -11,7 +11,6 @@ use IQBall\Core\Http\HttpResponse; use IQBall\Core\Http\JsonHttpResponse; use IQBall\Core\Validation\DefaultValidators; use IQBall\Core\Model\AuthModel; -use IQBall\Core\Validation\FieldValidationFail; use IQBall\Core\Validation\ValidationFail; class APIAccountsController { @@ -35,8 +34,9 @@ class APIAccountsController { return Control::runCheckedFrom($request, [ 'start' => [DefaultValidators::isUnsignedInteger()], 'n' => [DefaultValidators::isUnsignedInteger()], + 'search' => [DefaultValidators::lenBetween(0, 256)], ], function (HttpRequest $req) { - $accounts = $this->accounts->listAccounts(intval($req['start']), intval($req['n'])); + $accounts = $this->accounts->searchAccounts(intval($req['start']), intval($req['n']), $req["search"]); $users = array_map(fn(Account $acc) => $acc->getUser(), $accounts); return new JsonHttpResponse([ "users" => $users, diff --git a/src/Core/Gateway/AccountGateway.php b/src/Core/Gateway/AccountGateway.php index d10fb14..44a8b58 100644 --- a/src/Core/Gateway/AccountGateway.php +++ b/src/Core/Gateway/AccountGateway.php @@ -133,12 +133,13 @@ class AccountGateway { * @param int $start starting index of the list content * @return Account[]|null */ - public function listAccounts(int $start, int $n): ?array { + public function searchAccounts(int $start, int $n, ?string $searchString): ?array { $res = $this->con->fetch( - "SELECT * FROM Account ORDER BY email LIMIT :offset, :n", + "SELECT * FROM Account WHERE username LIKE '%' || :search || '%' OR email LIKE '%' || :search || '%' ORDER BY username, email LIMIT :offset, :n", [ ":offset" => [$start, PDO::PARAM_INT], ":n" => [$n, PDO::PARAM_INT], + ":search" => [$searchString ?? "", PDO::PARAM_STR], ] ); return array_map(fn(array $acc) => new Account($acc["token"], new User($acc["email"], $acc["username"], $acc["id"], $acc["profile_picture"], $acc["is_admin"])), $res);