Files
MediaCollectorLibary/app/Controllers/Api/BaseApiController.php
Lars Behrends eb1ec1153d Remove obsolete test scripts and add new API controllers for dashboard and game management
- Deleted test scripts: test_jellyfin_execution.php, test_stash.php, test_xbvr.php, test_xbvr_sync.php, vite.config.js
- Added DashboardController for fetching dashboard statistics and recent activity
- Added GameController for managing games, including fetching all games, game details, and games by category
- Introduced various check scripts to validate database structures and data integrity for adult videos, games, gender data, posters, and TV show actors
2026-01-18 01:42:03 +01:00

92 lines
2.7 KiB
PHP

<?php
namespace App\Controllers\Api;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Controllers\Controller;
class BaseApiController extends Controller
{
protected function getPdo(): \PDO
{
// Get PDO from the container - this assumes PDO is registered in the DI container
global $container;
if ($container && $container->has('pdo')) {
return $container->get('pdo');
}
// Fallback to creating a new PDO connection
$host = $_ENV['DB_HOST'] ?? 'localhost';
$dbname = $_ENV['DB_NAME'] ?? 'medialib';
$username = $_ENV['DB_USER'] ?? 'root';
$password = $_ENV['DB_PASS'] ?? '';
try {
return new \PDO(
"mysql:host=$host;dbname=$dbname;charset=utf8mb4",
$username,
$password,
[
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
]
);
} catch (\PDOException $e) {
throw new \Exception('Database connection failed: ' . $e->getMessage());
}
}
protected function success(Response $response, $data = null, int $status = 200): Response
{
$responseData = ['success' => true];
if ($data !== null) {
$responseData['data'] = $data;
}
return $this->json($response, $responseData, $status);
}
protected function error(Response $response, string $message, int $status = 400, array $errors = []): Response
{
$responseData = [
'success' => false,
'error' => [
'message' => $message,
'code' => $status
]
];
if (!empty($errors)) {
$responseData['error']['details'] = $errors;
}
return $this->json($response, $responseData, $status);
}
protected function getPaginationParams(Request $request): array
{
$params = $request->getQueryParams();
$page = max(1, (int)($params['page'] ?? 1));
$perPage = min(50, max(1, (int)($params['per_page'] ?? 20)));
return [
'page' => $page,
'per_page' => $perPage,
'offset' => ($page - 1) * $perPage
];
}
protected function getAuthUser(Request $request): ?array
{
return $request->getAttribute('user');
}
protected function isAdmin(Request $request): bool
{
$user = $this->getAuthUser($request);
return $user && ($user['is_admin'] ?? false);
}
}