@ -2,7 +2,6 @@
namespace IQBall\Core\Gateway;
namespace IQBall\Core\Gateway;
use Cassandra\PreparedStatement;
use IQBall\Core\Connection;
use IQBall\Core\Connection;
use IQBall\Core\Data\Account;
use IQBall\Core\Data\Account;
use IQBall\Core\Data\User;
use IQBall\Core\Data\User;
@ -19,16 +18,26 @@ class AccountGateway {
}
}
public function insertAccount(string $name, string $email, string $token, string $hash, string $profilePicture): int {
public function insertAccount(string $name, string $email, string $token, string $hash, string $profilePicture): int {
$this->con->exec("INSERT INTO Account(username, hash, email, token,profile_picture) VALUES (:username,:hash,:email,:token,:profileP ic)", [
$this->con->exec("INSERT INTO Account(username, hash, email, token,profile_picture) VALUES (:username,:hash,:email,:token,:profile_p ic)", [
':username' => [$name, PDO::PARAM_STR],
':username' => [$name, PDO::PARAM_STR],
':hash' => [$hash, PDO::PARAM_STR],
':hash' => [$hash, PDO::PARAM_STR],
':email' => [$email, PDO::PARAM_STR],
':email' => [$email, PDO::PARAM_STR],
':token' => [$token, PDO::PARAM_STR],
':token' => [$token, PDO::PARAM_STR],
':profileP ic' => [$profilePicture, PDO::PARAM_STR],
':profile_p ic' => [$profilePicture, PDO::PARAM_STR],
]);
]);
return intval($this->con->lastInsertId());
return intval($this->con->lastInsertId());
}
}
public function updateAccount(int $id, string $name, string $email, string $token, bool $isAdmin): void {
$this->con->exec("UPDATE Account SET username = :username, email = :email, token = :token, is_admin = :is_admin WHERE id = :id", [
':username' => [$name, PDO::PARAM_STR],
':email' => [$email, PDO::PARAM_STR],
':token' => [$token, PDO::PARAM_STR],
':id' => [$id, PDO::PARAM_INT],
':is_admin' => [$isAdmin, PDO::PARAM_BOOL],
]);
}
/**
/**
* promote or demote a user to server administrator
* promote or demote a user to server administrator
@ -122,7 +131,7 @@ class AccountGateway {
*
*
* @param integer $n the number of accounts to retrieve
* @param integer $n the number of accounts to retrieve
* @param int $start starting index of the list content
* @param int $start starting index of the list content
* @return Account[]
* @return Account[]|null
*/
*/
public function listAccounts(int $start, int $n): ?array {
public function listAccounts(int $start, int $n): ?array {
$res = $this->con->fetch(
$res = $this->con->fetch(
@ -132,7 +141,27 @@ class AccountGateway {
":n" => [$n, PDO::PARAM_INT],
":n" => [$n, PDO::PARAM_INT],
]
]
);
);
return array_map(fn(array $acc) => new Account($acc["email"], new User($acc["username"], $acc["token"], $acc["id"], $acc["profile_picture"], $acc["is_admin"])), $res);
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);
}
/**
* returns the total amount of accounts in the database
* @return int
*/
public function totalCount(): int {
return $this->con->fetch("SELECT count(*) FROM Account", [])[0]['count(*)'];
}
/**
* remove a bunch of account identifiers
* @param int[] $accountIds
*/
public function removeAccounts(array $accountIds): void {
foreach ($accountIds as $accountId) {
$this->con->fetch("DELETE FROM Account WHERE id = :accountId", [
":accountId" => [$accountId, PDO::PARAM_INT],
]);
}
}
}
}
}