From 97a85512e4acd0a3fb646c6c5116001038657cdb Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Fri, 8 Nov 2024 22:21:09 -0500 Subject: [PATCH] Add more implemention of methods for the image repository --- app/src/Domain/Entity/Image.php | 1 - .../Repository/Image/ImageRepository.php | 24 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/src/Domain/Entity/Image.php b/app/src/Domain/Entity/Image.php index ea07b20..1d8b0f8 100644 --- a/app/src/Domain/Entity/Image.php +++ b/app/src/Domain/Entity/Image.php @@ -45,7 +45,6 @@ class Image $props['title'], $props['height'], $props['width'], - ); if ($props['id']) { diff --git a/app/src/Domain/Repository/Image/ImageRepository.php b/app/src/Domain/Repository/Image/ImageRepository.php index 7ba6033..5e1f003 100644 --- a/app/src/Domain/Repository/Image/ImageRepository.php +++ b/app/src/Domain/Repository/Image/ImageRepository.php @@ -36,16 +36,38 @@ class ImageRepository implements ImageRepositoryInterface throw new EntityNotFoundException("Unable to find image."); } - return Image::fromArray($results); + return $this->createImageFromResults($results); } public function getFromUrl(string $url): Image { + $query = "SELECT * FROM images WHERE url = :url"; + $statement = $this->db->getConnection()->prepare($query); + $statement->execute([ ':url' => $url ]); + $results = $statement->fetch(\PDO::FETCH_ASSOC); + if (!is_array($results) || !count($results)) { + throw new EntityNotFoundException("Unabel to find image."); + } + + return $this->createImageFromResults($results); } public function create(Image $image): Image { + $query = "INSERT INTO + images (url, title, width, height) + VALUES (:url, :title, :width, :height)"; + + $this->db->getConnection()->exec($query, [ + ':url' => $image->getUrl(), + ':title' => $image->getTitle(), + ':width' => $image->getWidth(), + ':height' => $image->getHeight(), + ]); + + $insertId = $this->db->getConnection()->lastInsertId(); + $image->setId($insertId); return $image; }