pdo = $this->getConnection(); } /** * Get database connection */ private function getConnection(): \PDO { $config = include __DIR__ . '/../../Config/database.php'; $connection = $config['connections'][$config['default']]; $dsn = "mysql:host={$connection['host']};port={$connection['port']};dbname={$connection['database']};charset={$connection['charset']}"; return new \PDO($dsn, $connection['username'], $connection['password'], $connection['options']); } /** * Find user by ID */ public function findById(int $id): ?array { $stmt = $this->pdo->prepare("SELECT * FROM users WHERE id = ?"); $stmt->execute([$id]); $user = $stmt->fetch(); return $user ?: null; } /** * Find user by email */ public function findByEmail(string $email): ?array { $stmt = $this->pdo->prepare("SELECT * FROM users WHERE email = ?"); $stmt->execute([$email]); $user = $stmt->fetch(); return $user ?: null; } /** * Get all users */ public function all(): array { $stmt = $this->pdo->query("SELECT id, name, email, created_at, updated_at FROM users ORDER BY created_at DESC"); return $stmt->fetchAll(); } /** * Create new user */ public function create(array $data): int { $stmt = $this->pdo->prepare(" INSERT INTO users (name, email, password, created_at, updated_at) VALUES (?, ?, ?, NOW(), NOW()) "); $stmt->execute([ $data['name'], $data['email'], password_hash($data['password'], PASSWORD_ARGON2ID) ]); return $this->pdo->lastInsertId(); } /** * Update user */ public function update(int $id, array $data): bool { $fields = []; $values = []; foreach ($data as $key => $value) { if ($key !== 'id') { $fields[] = "{$key} = ?"; $values[] = $value; } } if (empty($fields)) { return false; } $values[] = $id; $sql = "UPDATE users SET " . implode(', ', $fields) . ", updated_at = NOW() WHERE id = ?"; $stmt = $this->pdo->prepare($sql); return $stmt->execute($values); } /** * Delete user */ public function delete(int $id): bool { $stmt = $this->pdo->prepare("DELETE FROM users WHERE id = ?"); return $stmt->execute([$id]); } /** * Check if email exists */ public function emailExists(string $email, ?int $excludeId = null): bool { $sql = "SELECT COUNT(*) FROM users WHERE email = ?"; $params = [$email]; if ($excludeId) { $sql .= " AND id != ?"; $params[] = $excludeId; } $stmt = $this->pdo->prepare($sql); $stmt->execute($params); return $stmt->fetchColumn() > 0; } /** * Get users with pagination */ public function paginate(int $page = 1, int $perPage = 10): array { $offset = ($page - 1) * $perPage; $stmt = $this->pdo->prepare(" SELECT id, name, email, created_at, updated_at FROM users ORDER BY created_at DESC LIMIT ? OFFSET ? "); $stmt->execute([$perPage, $offset]); $users = $stmt->fetchAll(); // Get total count $countStmt = $this->pdo->query("SELECT COUNT(*) FROM users"); $total = $countStmt->fetchColumn(); return [ 'data' => $users, 'total' => $total, 'per_page' => $perPage, 'current_page' => $page, 'last_page' => ceil($total / $perPage) ]; } /** * Search users */ public function search(string $query): array { $stmt = $this->pdo->prepare(" SELECT id, name, email, created_at, updated_at FROM users WHERE name LIKE ? OR email LIKE ? ORDER BY created_at DESC "); $searchTerm = "%{$query}%"; $stmt->execute([$searchTerm, $searchTerm]); return $stmt->fetchAll(); } }