diff --git a/src/Repositories/CategoryRepository.php b/src/Repositories/CategoryRepository.php new file mode 100644 index 0000000..e4d014e --- /dev/null +++ b/src/Repositories/CategoryRepository.php @@ -0,0 +1,59 @@ +db->select('SELECT * FROM categories ORDER BY name ASC'); + } + + public function find(int $id): ?array + { + return $this->db->selectOne('SELECT * FROM categories WHERE id = ?', [$id]); + } + + public function findBySlug(string $slug): ?array + { + return $this->db->selectOne('SELECT * FROM categories WHERE slug = ?', [$slug]); + } + + public function create(string $name, string $slug, string $description): int + { + return $this->db->insert( + 'INSERT INTO categories (name, slug, description) VALUES (?, ?, ?)', + [$name, $slug, $description] + ); + } + + public function update(int $id, string $name, string $slug, string $description): void + { + $this->db->statement( + 'UPDATE categories SET name = ?, slug = ?, description = ? WHERE id = ?', + [$name, $slug, $description, $id] + ); + } + + public function delete(int $id): void + { + $this->db->statement('DELETE FROM categories WHERE id = ?', [$id]); + } + + public function withProductCounts(): array + { + return $this->db->select( + 'SELECT c.*, COUNT(p.id) AS productCount + FROM categories c + LEFT JOIN products p ON p.categoryId = c.id + GROUP BY c.id + ORDER BY c.name ASC' + ); + } +} diff --git a/src/Repositories/UserRepository.php b/src/Repositories/UserRepository.php new file mode 100644 index 0000000..cc41e0a --- /dev/null +++ b/src/Repositories/UserRepository.php @@ -0,0 +1,63 @@ +db->selectOne( + 'SELECT * FROM users WHERE email = ?', + [strtolower(trim($email))] + ); + } + + public function findById(int $id): ?array + { + return $this->db->selectOne('SELECT * FROM users WHERE id = ?', [$id]); + } + + public function emailExists(string $email): bool + { + return (int) $this->db->scalar( + 'SELECT COUNT(*) FROM users WHERE email = ?', + [strtolower(trim($email))] + ) > 0; + } + + public function create(string $email, string $passwordHash, string $name, bool $isAdmin = false, string $role = 'customer'): int + { + if ($isAdmin) { + $role = 'admin'; + } + return $this->db->insert( + 'INSERT INTO users (email, passwordHash, name, isAdmin, role, createdAt) + VALUES (?, ?, ?, ?, ?, ?)', + [strtolower(trim($email)), $passwordHash, $name, $isAdmin ? 1 : 0, $role, gmdate('c')] + ); + } + + public function setRole(int $id, string $role): void + { + $this->db->statement( + 'UPDATE users SET role = ?, isAdmin = ? WHERE id = ?', + [$role, $role === 'admin' ? 1 : 0, $id] + ); + } + + public function all(): array + { + return $this->db->select('SELECT id, email, name, role, isAdmin, createdAt FROM users ORDER BY createdAt DESC, id DESC'); + } + + public function count(): int + { + return (int) $this->db->scalar('SELECT COUNT(*) FROM users'); + } +} diff --git a/src/Repositories/WishlistRepository.php b/src/Repositories/WishlistRepository.php new file mode 100644 index 0000000..5d5b3ad --- /dev/null +++ b/src/Repositories/WishlistRepository.php @@ -0,0 +1,77 @@ +db->scalar( + 'SELECT COUNT(*) FROM wishlistItems WHERE userId = ? AND productId = ?', + [$userId, $productId] + ) > 0; + } + + public function add(int $userId, int $productId): void + { + $this->db->statement( + 'INSERT INTO wishlistItems (userId, productId, createdAt) + VALUES (?, ?, ?) + ON CONFLICT(userId, productId) DO NOTHING', + [$userId, $productId, gmdate('c')] + ); + } + + public function remove(int $userId, int $productId): void + { + $this->db->statement( + 'DELETE FROM wishlistItems WHERE userId = ? AND productId = ?', + [$userId, $productId] + ); + } + + public function toggle(int $userId, int $productId): bool + { + if ($this->has($userId, $productId)) { + $this->remove($userId, $productId); + return false; + } + $this->add($userId, $productId); + return true; + } + + public function forUser(int $userId): array + { + return $this->db->select( + 'SELECT p.*, c.name AS categoryName, c.slug AS categorySlug, w.createdAt AS savedAt + FROM wishlistItems w + JOIN products p ON p.id = w.productId + LEFT JOIN categories c ON c.id = p.categoryId + WHERE w.userId = ? + ORDER BY w.createdAt DESC, w.id DESC', + [$userId] + ); + } + + public function productIdsForUser(int $userId): array + { + return array_map( + 'intval', + array_column( + $this->db->select('SELECT productId FROM wishlistItems WHERE userId = ?', [$userId]), + 'productId' + ) + ); + } + + public function count(int $userId): int + { + return (int) $this->db->scalar('SELECT COUNT(*) FROM wishlistItems WHERE userId = ?', [$userId]); + } +}