parent
3ace793578
commit
9cde3af510
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace IQBall\Api\Controller;
|
||||
|
||||
use IQBall\App\Control;
|
||||
use IQBall\Core\Data\Account;
|
||||
use IQBall\Core\Gateway\AccountGateway;
|
||||
use IQBall\Core\Http\HttpCodes;
|
||||
use IQBall\Core\Http\HttpRequest;
|
||||
use IQBall\Core\Http\HttpResponse;
|
||||
use IQBall\Core\Http\JsonHttpResponse;
|
||||
use IQBall\Core\Validation\DefaultValidators;
|
||||
use IQBall\Core\Validation\ValidationFail;
|
||||
|
||||
class APIAccountsController {
|
||||
private AccountGateway $accounts;
|
||||
|
||||
/**
|
||||
* @param AccountGateway $accounts
|
||||
*/
|
||||
public function __construct(AccountGateway $accounts) {
|
||||
$this->accounts = $accounts;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $request
|
||||
* @return HttpResponse
|
||||
*/
|
||||
public function listUsers(array $request): HttpResponse {
|
||||
return Control::runCheckedFrom($request, [
|
||||
'start' => [DefaultValidators::isUnsignedInteger()],
|
||||
'n' => [DefaultValidators::isUnsignedInteger()],
|
||||
], function (HttpRequest $req) {
|
||||
$accounts = $this->accounts->listAccounts(intval($req['start']), intval($req['n']));
|
||||
$response = array_map(fn(Account $acc) => $acc->getUser(), $accounts);
|
||||
return new JsonHttpResponse($response);
|
||||
}, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @return HttpResponse given user information.
|
||||
*/
|
||||
public function getUser(int $userId): HttpResponse {
|
||||
$acc = $this->accounts->getAccount($userId);
|
||||
|
||||
if ($acc == null) {
|
||||
return new JsonHttpResponse([ValidationFail::notFound("User not found")], HttpCodes::NOT_FOUND);
|
||||
}
|
||||
|
||||
return new JsonHttpResponse($acc->getUser());
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace IQBall\Api\Controller;
|
||||
|
||||
use IQBall\Core\Http\HttpResponse;
|
||||
use IQBall\Core\Http\JsonHttpResponse;
|
||||
|
||||
class APIServerController {
|
||||
private string $basePath;
|
||||
private \PDO $pdo;
|
||||
|
||||
/**
|
||||
* @param string $basePath
|
||||
* @param \PDO $pdo
|
||||
*/
|
||||
public function __construct(string $basePath, \PDO $pdo) {
|
||||
$this->basePath = $basePath;
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
private function countLines(string $table): int {
|
||||
$stmnt = $this->pdo->prepare("SELECT count(*) FROM $table");
|
||||
$stmnt->execute();
|
||||
$res = $stmnt->fetch(\PDO::FETCH_BOTH);
|
||||
return $res[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return HttpResponse some (useless) information about the server
|
||||
*/
|
||||
public function getServerInfo(): HttpResponse {
|
||||
|
||||
return new JsonHttpResponse([
|
||||
'base_path' => $this->basePath,
|
||||
'date' => (int) gettimeofday(true) * 1000,
|
||||
'database' => [
|
||||
'accounts' => $this->countLines("Account") . " line(s)",
|
||||
'tactics' => $this->countLines("Tactic") . " line(s)",
|
||||
'teams' => $this->countLines("Team") . " line(s)",
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue