added find self

This commit is contained in:
tracer 2022-09-17 15:40:44 +02:00
parent e12a226b4f
commit d8ad733d2c
1 changed files with 54 additions and 16 deletions

View File

@ -3,6 +3,7 @@
namespace App\Repository;
use App\Controller\DatabaseConnection;
use App\Entity\Domain;
use App\Entity\Panel;
use PDO;
use PDOException;
@ -12,9 +13,31 @@ use PDOException;
*/
class PanelRepository
{
public function __construct(private DatabaseConnection $databaseConnection)
public function __construct(private readonly DatabaseConnection $databaseConnection)
{}
public function findSelf(): array
{
$sql = "
SELECT id, name, a, aaaa, apikey, self
FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE self = 1";
$panels = [];
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
$panels[] = $panel;
}
return $panels;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @return array
@ -23,7 +46,7 @@ class PanelRepository
{
$panels = [];
$sql = "
SELECT id, name, a, aaaa, apikey
SELECT id, name, a, aaaa, apikey, self
FROM " . DatabaseConnection::TABLE_PANELS . "
ORDER BY name";
@ -31,7 +54,7 @@ class PanelRepository
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']);
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
$panels[] = $panel;
}
return $panels;
@ -44,12 +67,12 @@ class PanelRepository
/**
* @param int $id
*
* @return \App\Entity\Panel
* @return null|\App\Entity\Panel
*/
public function findByID(int $id): Panel
public function findByID(int $id): ?Panel
{
$sql = "
SELECT id, name, a, aaaa, apikey
SELECT id, name, a, aaaa, apikey, self
FROM . " . DatabaseConnection::TABLE_PANELS . "
WHERE id = :id";
@ -57,8 +80,11 @@ class PanelRepository
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':id', var: $id);
$statement->execute();
$result = $statement->fetch(mode: PDO::FETCH_ASSOC);
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']);
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
} else {
return null;
}
} catch (PDOException $e) {
exit($e->getMessage());
}
@ -73,7 +99,7 @@ class PanelRepository
public function findByName(string $name): Panel|bool
{
$sql = "
SELECT id, name, a, aaaa, apikey
SELECT id, name, a, aaaa, apikey, self
FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE name = :name";
@ -82,7 +108,7 @@ class PanelRepository
$statement->bindParam(param: ':name', var: $name);
$statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey']);
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
} else {
return false;
}
@ -100,11 +126,11 @@ class PanelRepository
*
* @return string|false
*/
public function insert(string $name, string $a, string $aaaa, String $apikey): bool|string
public function insert(string $name, string $a, string $aaaa, String $apikey, int $self): bool|string
{
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey)
VALUES (:name, :a, :aaaa, :apikey)";
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey, self)
VALUES (:name, :a, :aaaa, :apikey, :self)";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
@ -112,6 +138,7 @@ class PanelRepository
$statement->bindParam(param: ':a', var: $a);
$statement->bindParam(param: ':aaaa', var: $aaaa);
$statement->bindParam(param: ':apikey', var: $apikey);
$statement->bindParam(param: ':self', var: $self);
$statement->execute();
return $this->databaseConnection->getConnection()->lastInsertId();
@ -130,7 +157,7 @@ class PanelRepository
*
* @return false|int
*/
public function update(int $id, string $name, string $a, string $aaaa, String $apikey): bool|int
public function update(int $id, string $name, string $a, string $aaaa, String $apikey, int $self): bool|int
{
$current = $this->findByID(id: $id);
@ -147,12 +174,22 @@ class PanelRepository
$apikey = $current->getApikey();
}
if (empty($self)) {
echo "self is empty";
$self = $current->getSelf();
} else {
if ($self == -1) {
$self = 0;
}
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_PANELS . " SET
name = :name,
a = :a,
aaaa = :aaaa,
apikey = :apikey
apikey = :apikey,
self = :self
WHERE id = :id";
try {
@ -162,6 +199,7 @@ class PanelRepository
$statement->bindParam(param: 'a', var: $a);
$statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->bindParam(param: 'apikey', var: $apikey);
$statement->bindParam(param: 'self', var: $self);
$statement->execute();
return $statement->rowCount();