moved insert from mock to working code

This commit is contained in:
tracer 2022-10-24 20:12:04 +02:00
parent 5e8c945170
commit 10efd6d1a6

View File

@ -15,7 +15,7 @@ use PDO;
use PDOException;
/**
* Handles CRUD od User class.
* Handles CRUD of User class.
*/
class UserRepository
{
@ -24,25 +24,21 @@ class UserRepository
// empty body
}
/**
* @return array
*/
public function findAll(string $orderBy = 'nick'): array
{
$users = [];
$sql = "
SELECT id, nick, first, last, is_admin
SELECT id, nick, password, first, last, is_admin
FROM " . DatabaseConnection::TABLE_USERS . "
ORDER BY :order";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':order', var: $order);
$statement->bindParam(param: ':order', var: $orderBy);
$statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$user = new User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']);
$user = new User(nick: $result['nick'], password: $result['password'], first: $result['first'], last: $result['last'], id: $result['id'], isAdmin: $result['is_admin']);
$users[] = $user;
}
return $users;
@ -55,7 +51,7 @@ class UserRepository
public function findByID(int $id): ?User
{
$sql = "
SELECT id, nick, first, last, is_admin
SELECT id, nick, password, first, last, is_admin
FROM " . DatabaseConnection::TABLE_USERS . "
WHERE id = :id";
@ -64,7 +60,7 @@ class UserRepository
$statement->bindParam(param: ':id', var: $id);
$statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']);
return new User(nick: $result['nick'], password: $result['password'], first: $result['first'], last: $result['last'], id: $result['id'], isAdmin: $result['is_admin']);
} else {
return null;
}
@ -76,7 +72,7 @@ class UserRepository
public function findByNick(string $nick): ?User
{
$sql = "
SELECT id, nick, first, last, is_admin
SELECT id, nick, password, first, last, is_admin
FROM " . DatabaseConnection::TABLE_USERS . "
WHERE nick = :nick";
@ -85,7 +81,7 @@ class UserRepository
$statement->bindParam(param: ':nick', var: $nick);
$statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']);
return new User(nick: $result['nick'], password: $result['password'], first: $result['first'], last: $result['last'], id: $result['id'], isAdmin: $result['is_admin']);
} else {
return null;
}
@ -96,56 +92,63 @@ class UserRepository
public function insert(User $user): bool|string
{
/*
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_USERS . " (name, panel)
VALUES (:name, :panel)";
INSERT INTO " . DatabaseConnection::TABLE_USERS . " (nick, password, first, last, is_admin)
VALUES (:nick, :password, :first, :last, :is_admin)";
try {
$name = $domain->getName();
$panel = $domain->getPanel();
$nick = $user->getNick();
$password = $user->getPassword();
$first = $user->getFirst();
$last = $user->getLast();
$isAdmin = $user->isAdmin() ? 1 : 0;
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':panel', var: $panel);
$statement->bindParam(param: ':nick', var: $nick);
$statement->bindParam(param: ':password', var: $password);
$statement->bindParam(param: ':first', var: $first);
$statement->bindParam(param: ':last', var: $last);
$statement->bindParam(param: ':is_admin', var: $isAdmin);
$statement->execute();
return $this->databaseConnection->getConnection()->lastInsertId();
} catch (PDOException $e) {
exit($e->getMessage());
}
*/
return false;
}
public function update(User $user): bool|int
{
$id = $user->getId();
$current = $this->findByID(id: $id);
$nick = $user->getNick();
$first = $user->getFirst();
$last = $user->getLast();
$isAdmin = $user->isAdmin() ? 1 : 0;
/*
if (empty($domain->getName())) {
$name = $current->getName();
if ($user->getPassword()) {
$password = $user->getPassword();
} else {
$name = $domain->getName();
}
if (empty($domain->getPanel())) {
$panel = $current->getPanel();
} else {
$panel = $domain->getPanel();
$current = $this->findByID(id: $id);
$password = $current->getPassword();
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_USER . " SET
name = :name,
panel = :panel
UPDATE " . DatabaseConnection::TABLE_USERS . " SET
nick = :nick,
password = :password,
first = :first,
last = :last,
is_admin = :is_admin
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id);
$statement->bindParam(param: 'name', var: $name);
$statement->bindParam(param: 'panel', var: $panel);
$statement->bindParam(param: 'nick', var: $nick);
$statement->bindParam(param: 'password', var: $password);
$statement->bindParam(param: 'first', var: $first);
$statement->bindParam(param: 'last', var: $last);
$statement->bindParam(param: 'is_admin', var: $isAdmin);
$statement->execute();
return $statement->rowCount();
@ -153,8 +156,6 @@ class UserRepository
echo $e->getMessage();
return false;
}
*/
return false;
}