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