@ -36,6 +36,14 @@ class AccountGateway {
return $this->con->fetch("SELECT * FROM Account WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]])[0] ?? null;
}
/**
* @param int $id
* @return array< string , mixed > |null
*/
private function getRowsFromId(int $id): ?array {
return $this->con->fetch("SELECT * FROM Account WHERE id = :id", [':id' => [$id, PDO::PARAM_INT]])[0] ?? null;
}
/**
* @param string $email
* @return string|null the hashed user's password, or null if the given mail does not exist
@ -69,6 +77,19 @@ class AccountGateway {
return new Account($acc["token"], new User($email, $acc["username"], $acc["id"], $acc["profilePicture"]));
}
/**
* @param int $id
* @return Account|null
*/
public function getAccountFromId(int $id): ?Account {
$acc = $this->getRowsFromId($id);
if (empty($acc)) {
return null;
}
return new Account($acc["token"], new User($acc["email"], $acc["username"], $id, $acc["profilePicture"]));
}
/**
* @param string $token get an account from given token
* @return Account|null
@ -94,8 +115,8 @@ class AccountGateway {
]);
}
public function nameIsDifferent(string $email , string $name) : bool {
$nameExist = $this->con->fetch("SELECT username FROM Account WHERE email = :email", [':email' => [$email , PDO::PARAM_STR]]) ?? null;
public function nameIsDifferent(int $id , string $name) : bool {
$nameExist = $this->con->fetch("SELECT username FROM Account WHERE id = :id", [':id' => [$id , PDO::PARAM_STR]]) ?? null;
if (!empty($nameExist)) {
if ($name != $nameExist[0]["username"]) {
@ -105,16 +126,41 @@ class AccountGateway {
return false;
}
public function changeName(string $email, string $newName) {
error_log($email);
public function emailIsDifferent(int $id, string $email) : bool {
$emailExist = $this->con->fetch("SELECT email FROM Account WHERE id = :id", [':id' => [$id, PDO::PARAM_STR]]) ?? null;
if (!empty($emailExist)) {
if ($email != $emailExist[0]["email"]) {
return true;
}
}
return false;
}
public function emailExist(string $email) {
$res = $this->con->fetch("SELECT email FROM Account WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]) ?? null;
return $res != null;
}
public function changeName(int $id, string $newName) {
$this->con->exec("
UPDATE Account
SET username = :username
WHERE email = :email
WHERE id = :id
", [
':username' => [$newName, PDO::PARAM_STR],
':email' => [$email, PDO::PARAM_STR]
':id' => [$id, PDO::PARAM_STR]
]);
}
public function changeEmail(int $id, string $newEmail) {
$this->con->exec("
UPDATE Account
SET email = :email
WHERE id = :id
", [
':email' => [$newEmail, PDO::PARAM_STR],
':id' => [$id, PDO::PARAM_STR]
]);
}
}