Add more implemention of methods for the image repository

This commit is contained in:
Dave Smith-Hayes 2024-11-08 22:21:09 -05:00
parent 1d00f2bda4
commit 97a85512e4
2 changed files with 23 additions and 2 deletions

View File

@ -45,7 +45,6 @@ class Image
$props['title'], $props['title'],
$props['height'], $props['height'],
$props['width'], $props['width'],
); );
if ($props['id']) { if ($props['id']) {

View File

@ -36,16 +36,38 @@ class ImageRepository implements ImageRepositoryInterface
throw new EntityNotFoundException("Unable to find image."); throw new EntityNotFoundException("Unable to find image.");
} }
return Image::fromArray($results); return $this->createImageFromResults($results);
} }
public function getFromUrl(string $url): Image 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 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; return $image;
} }