|
3 | 3 | namespace App\Repositories; |
4 | 4 |
|
5 | 5 | use App\Core\DbContext; |
6 | | -use PDO; |
7 | 6 |
|
8 | 7 | final class ItemRepository |
9 | 8 | { |
10 | 9 | public function __construct(private DbContext $ctx) {} |
| 10 | + |
11 | 11 | public function create(array $d): int |
12 | 12 | { |
13 | | - $stmt = $this->ctx->conn()->prepare("INSERT INTO items (sku,title,price) VALUES (?,?,?)"); |
14 | | - $stmt->execute([$d['sku'], $d['title'], $d['price']]); |
15 | | - return (int)$this->ctx->conn()->lastInsertId(); |
| 13 | + $conn = $this->ctx->conn(); // returns Swoole\Coroutine\MySQL |
| 14 | + |
| 15 | + $stmt = $conn->prepare("INSERT INTO items (sku, title, price) VALUES (?, ?, ?)"); |
| 16 | + if ($stmt === false) { |
| 17 | + throw new \RuntimeException("Failed to prepare statement: " . $conn->error); |
| 18 | + } |
| 19 | + |
| 20 | + $result = $stmt->execute([$d['sku'], $d['title'], (float)$d['price']]); |
| 21 | + if ($result === false) { |
| 22 | + throw new \RuntimeException("Insert failed: " . $stmt->error); |
| 23 | + } |
| 24 | + |
| 25 | + return (int)$conn->insert_id; |
16 | 26 | } |
| 27 | + |
17 | 28 | public function find(int $id): ?array |
18 | 29 | { |
19 | | - $stmt = $this->ctx->conn()->prepare("SELECT * FROM items WHERE id=?"); |
20 | | - $stmt->execute([$id]); |
21 | | - return $stmt->fetch(PDO::FETCH_ASSOC) ?: null; |
| 30 | + $conn = $this->ctx->conn(); |
| 31 | + |
| 32 | + $stmt = $conn->prepare("SELECT id, sku, title, price, created_at, updated_at FROM items WHERE id=? LIMIT 1"); |
| 33 | + if ($stmt === false) { |
| 34 | + throw new \RuntimeException("Failed to prepare statement: " . $conn->error); |
| 35 | + } |
| 36 | + |
| 37 | + $rows = $stmt->execute([$id]); |
| 38 | + if ($rows === false) { |
| 39 | + throw new \RuntimeException("Query failed: " . $stmt->error); |
| 40 | + } |
| 41 | + |
| 42 | + return $rows[0] ?? null; |
22 | 43 | } |
| 44 | + |
23 | 45 | public function list(): array |
24 | 46 | { |
25 | | - return $this->ctx->conn()->query("SELECT * FROM items ORDER BY id DESC limit 100")->fetchAll(PDO::FETCH_ASSOC); |
| 47 | + $conn = $this->ctx->conn(); |
| 48 | + |
| 49 | + $rows = $conn->query("SELECT id, sku, title, price, created_at, updated_at FROM items ORDER BY id DESC LIMIT 100"); |
| 50 | + if ($rows === false) { |
| 51 | + throw new \RuntimeException("Query failed: " . $conn->error); |
| 52 | + } |
| 53 | + |
| 54 | + return $rows; |
26 | 55 | } |
| 56 | + |
27 | 57 | public function update(int $id, array $d): bool |
28 | 58 | { |
29 | | - $stmt = $this->ctx->conn()->prepare("UPDATE items SET sku=?, title=?, price=? WHERE id=?"); |
30 | | - return $stmt->execute([$d['sku'], $d['title'], $d['price'], $id]); |
| 59 | + $conn = $this->ctx->conn(); |
| 60 | + |
| 61 | + $stmt = $conn->prepare("UPDATE items SET sku=?, title=?, price=? WHERE id=?"); |
| 62 | + if ($stmt === false) { |
| 63 | + throw new \RuntimeException("Failed to prepare statement: " . $conn->error); |
| 64 | + } |
| 65 | + |
| 66 | + $result = $stmt->execute([$d['sku'], $d['title'], (float)$d['price'], $id]); |
| 67 | + if ($result === false) { |
| 68 | + throw new \RuntimeException("Update failed: " . $stmt->error); |
| 69 | + } |
| 70 | + |
| 71 | + return (bool)($result['affected_rows'] ?? 0); |
31 | 72 | } |
| 73 | + |
32 | 74 | public function delete(int $id): bool |
33 | 75 | { |
34 | | - $stmt = $this->ctx->conn()->prepare("DELETE FROM items WHERE id=?"); |
35 | | - return $stmt->execute([$id]); |
| 76 | + $conn = $this->ctx->conn(); |
| 77 | + |
| 78 | + $stmt = $conn->prepare("DELETE FROM items WHERE id=?"); |
| 79 | + if ($stmt === false) { |
| 80 | + throw new \RuntimeException("Failed to prepare statement: " . $conn->error); |
| 81 | + } |
| 82 | + |
| 83 | + $result = $stmt->execute([$id]); |
| 84 | + if ($result === false) { |
| 85 | + throw new \RuntimeException("Delete failed: " . $stmt->error); |
| 86 | + } |
| 87 | + |
| 88 | + return (bool)($result['affected_rows'] ?? 0); |
36 | 89 | } |
37 | 90 | } |
0 commit comments