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