Add more implemention of methods for the image repository
This commit is contained in:
parent
1d00f2bda4
commit
97a85512e4
@ -45,7 +45,6 @@ class Image
|
||||
$props['title'],
|
||||
$props['height'],
|
||||
$props['width'],
|
||||
|
||||
);
|
||||
|
||||
if ($props['id']) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user