From df1de5ff32f85a4d79acc7630282a2e0b8de7739 Mon Sep 17 00:00:00 2001 From: tomivt Date: Wed, 30 Oct 2024 23:21:57 +0100 Subject: [PATCH] Add Image Gateway & Model --- src/Entity/ImageEntity.php | 53 ++++++++++++++++++++++ src/Entity/imageEntity.php | 53 ---------------------- src/Gateway/ImageGateway.php | 88 ++++++++++++++++++++++++++++++++++++ src/Model/ImageModel.php | 80 ++++++++++++++++++++++++++++++++ 4 files changed, 221 insertions(+), 53 deletions(-) create mode 100644 src/Entity/ImageEntity.php delete mode 100644 src/Entity/imageEntity.php create mode 100644 src/Gateway/ImageGateway.php create mode 100644 src/Model/ImageModel.php diff --git a/src/Entity/ImageEntity.php b/src/Entity/ImageEntity.php new file mode 100644 index 0000000..e1a0979 --- /dev/null +++ b/src/Entity/ImageEntity.php @@ -0,0 +1,53 @@ +idImg = $idImg; + $this->imgPath = $imgPath; + $this->isImgProfile = $isImgProfile; + } + + public function getIdImg(): int + { + return $this->idImg; + } + + public function setIdImg(int $idImg): void + { + $this->idImg = $idImg; + } + + public function getImgPath(): string + { + return $this->imgPath; + } + + public function setImgPath(string $imgPath): void + { + $this->imgPath = $imgPath; + } + + public function getIsImgProfile(): string + { + return $this->isImgProfile; + } + + public function setIsImgProfile(string $isImgProfile): void + { + $this->isImgProfile = $isImgProfile; + } + + +} diff --git a/src/Entity/imageEntity.php b/src/Entity/imageEntity.php deleted file mode 100644 index 7dc9735..0000000 --- a/src/Entity/imageEntity.php +++ /dev/null @@ -1,53 +0,0 @@ -id_img = $id_img; - $this->img_path = $img_path; - $this->isImgProfil = $isImgProfil; - } - - public function getIdImg(): int - { - return $this->id_img; - } - - public function setIdImg(int $id_img): void - { - $this->id_img = $id_img; - } - - public function getImgPath(): string - { - return $this->img_path; - } - - public function setImgPath(string $img_path): void - { - $this->img_path = $img_path; - } - - public function getIsImgProfil(): string - { - return $this->isImgProfil; - } - - public function setIsImgProfil(string $isImgProfil): void - { - $this->isImgProfil = $isImgProfil; - } - - -} diff --git a/src/Gateway/ImageGateway.php b/src/Gateway/ImageGateway.php new file mode 100644 index 0000000..f2de8e4 --- /dev/null +++ b/src/Gateway/ImageGateway.php @@ -0,0 +1,88 @@ + co = $co; + } + + public function createImgGateway(int $idImg, string $imgPath, bool $isImgProfile) : bool + { + $query = " + INSERT INTO Images + VALUES (:id_img, :img_path, :is_img_profile) + "; + + return $this -> co -> executeQuery($query, [ + 'id_img' => array($idImg, PDO::PARAM_INT), + 'img_path' => array($idImg, PDO::PARAM_STR), + 'is_img_profile' => array($idImg, PDO::PARAM_BOOL), + ]); + } + + public function findImgById(int $idImg) : array + { + $query = " + SELECT * FROM Images + WHERE id_image = :id_img + "; + + $this -> co -> executeQuery($query, ['id_img' => array($idImg, PDO::PARAM_INT)]); + + return $this -> co -> getResults(); + } + + public function findAllImg() : array + { + $query = " + SELECT * FROM Images + "; + + $this -> co -> executeQuery($query); + + return $this -> co -> getResults(); + } + + public function findAllImgProfile() : array + { + $query = " + SELECT * FROM Images + WHERE is_img_prfl + "; + + $this -> co -> executeQuery($query); + + return $this -> co -> getResults(); + } + + public function deleteImgGateway(int $idImg) : bool + { + $query = " + DELETE FROM Images + WHERE id_image = :id_img + "; + + return $this -> co -> executeQuery($query, ['id_img' => array($idImg, PDO::PARAM_INT)]); + } + + public function updateImgGateway(int $idImg, string $imgPath) : bool + { + $query = " + UPDATE Images + SET img_path = :img_path + WHERE id_image = :id_img + "; + + return $this -> co -> executeQuery($query, [ + 'id_img' => array($idImg, PDO::PARAM_INT), + 'img_path' => array($imgPath, PDO::PARAM_STR) + ]); + } +} \ No newline at end of file diff --git a/src/Model/ImageModel.php b/src/Model/ImageModel.php new file mode 100644 index 0000000..5a3dafa --- /dev/null +++ b/src/Model/ImageModel.php @@ -0,0 +1,80 @@ + gw = $gw; + } + + public function createImgModel(int $idImg, string $imgPath, bool $isImgProfile) : bool + { + return $this -> gw -> createImgGateway($idImg, $imgPath, $isImgProfile); + } + + public function getImgById(int $idImg) : ?ImageEntity + { + $res = $this -> gw -> findImgById($idImg); + + if ($res) + { + return new ImageEntity( + $res[0]['id_image'], + $res[0]['img_path'], + $res[0]['is_img_prfl'] + ); + } + return null; + } + + public function getAllImg() : array + { + $res = $this -> gw -> findAllImg(); + + $images = []; + + foreach ($res as $img) + { + $images[] = new ImageEntity( + $img['id_image'], + $img['img_path'], + $img['is_img_prfl'] + ); + } + return $images; + } + + public function getAllImgProfile() : array + { + $res = $this -> gw -> findAllImgProfile(); + + $images = []; + + foreach ($res as $img) + { + $images[] = new ImageEntity( + $img['id_image'], + $img['img_path'], + $img['is_img_prfl'] + ); + } + return $images; + } + + public function deleteImgModel(int $idImg) : bool + { + return $this -> gw -> deleteImgGateway($idImg); + } + + public function updateImgModel(int $idImg, string $imgPath, bool $isImgProfile) : bool + { + return $this -> gw -> updateImgGateway($idImg, $imgPath, $isImgProfile); + } +} \ No newline at end of file