feat: repository crud
This commit is contained in:
1 parent
b7317bd8e1
commit
d654bb8511
1 file changed
+73
@@ -0,0 +1,73 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
final class LinkRepository
|
||||||
|
{
|
||||||
|
private PDO $pdo;
|
||||||
|
|
||||||
|
public function __construct(PDO $pdo)
|
||||||
|
{
|
||||||
|
$this->pdo = $pdo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function all(): array
|
||||||
|
{
|
||||||
|
return $this->pdo->query('SELECT * FROM links ORDER BY id DESC')->fetchAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function find(int $id): ?array
|
||||||
|
{
|
||||||
|
$stmt = $this->pdo->prepare('SELECT * FROM links WHERE id = :id');
|
||||||
|
$stmt->execute(['id' => $id]);
|
||||||
|
$row = $stmt->fetch();
|
||||||
|
|
||||||
|
return $row ?: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create(string $title, string $description, string $linkUrl, ?string $thumbnail): int
|
||||||
|
{
|
||||||
|
$stmt = $this->pdo->prepare(
|
||||||
|
'INSERT INTO links (title, description, link_url, thumbnail)
|
||||||
|
VALUES (:title, :description, :link_url, :thumbnail)'
|
||||||
|
);
|
||||||
|
$stmt->execute([
|
||||||
|
'title' => $title,
|
||||||
|
'description' => $description,
|
||||||
|
'link_url' => $linkUrl,
|
||||||
|
'thumbnail' => $thumbnail,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return (int) $this->pdo->lastInsertId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(int $id, string $title, string $description, string $linkUrl, ?string $thumbnail): bool
|
||||||
|
{
|
||||||
|
$stmt = $this->pdo->prepare(
|
||||||
|
'UPDATE links
|
||||||
|
SET title = :title,
|
||||||
|
description = :description,
|
||||||
|
link_url = :link_url,
|
||||||
|
thumbnail = :thumbnail,
|
||||||
|
updated_at = datetime(\'now\')
|
||||||
|
WHERE id = :id'
|
||||||
|
);
|
||||||
|
$stmt->execute([
|
||||||
|
'id' => $id,
|
||||||
|
'title' => $title,
|
||||||
|
'description' => $description,
|
||||||
|
'link_url' => $linkUrl,
|
||||||
|
'thumbnail' => $thumbnail,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $stmt->rowCount() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete(int $id): bool
|
||||||
|
{
|
||||||
|
$stmt = $this->pdo->prepare('DELETE FROM links WHERE id = :id');
|
||||||
|
$stmt->execute(['id' => $id]);
|
||||||
|
|
||||||
|
return $stmt->rowCount() > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user