Add PHP Media API scaffold and Docker configs
Initial project scaffold for a PHP Media API including routing, controllers, models and services under api/ (Router, Media/Cast/Image/Settings controllers, models, database/bootstrap files and automatic docs service). Adds Docker support (Dockerfile, docker-compose.yml, DOCKER_README.md, php-custom.ini), .htaccess for pretty URLs, API documentation and example payloads (API_EXAMPLES.md, api/README.md, api_examples/*.json), image handling service and logging, plus a comprehensive .gitignore. This commit provides a runnable development environment and example requests to get the API up and tested quickly.
This commit is contained in:
256
api/controllers/CastController.php
Normal file
256
api/controllers/CastController.php
Normal file
@@ -0,0 +1,256 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../models/Cast.php';
|
||||
require_once __DIR__ . '/../models/AdultCast.php';
|
||||
require_once __DIR__ . '/../services/ApiLogger.php';
|
||||
|
||||
class CastController {
|
||||
private $cast;
|
||||
private $adultCast;
|
||||
private $logger;
|
||||
|
||||
public function __construct($pdo) {
|
||||
$this->cast = new Cast($pdo);
|
||||
$this->adultCast = new AdultCast($pdo);
|
||||
$this->logger = ApiLogger::getInstance();
|
||||
}
|
||||
|
||||
public function handleRequest($method, $segments) {
|
||||
$id = isset($segments[1]) ? (int)$segments[1] : null;
|
||||
$subResource = isset($segments[2]) ? $segments[2] : null;
|
||||
|
||||
$path = '/' . implode('/', $segments);
|
||||
$this->logger->logRequest($method, $path);
|
||||
// Adult-spezifische Endpunkte
|
||||
if ($id === 'adult' || $subResource === 'adult') {
|
||||
// die("adult");
|
||||
return $this->handleAdult($method, $id, $segments);
|
||||
}
|
||||
|
||||
switch ($method) {
|
||||
case 'GET':
|
||||
return $id ? $this->getOne($id, $segments) : $this->getAll();
|
||||
case 'POST':
|
||||
return $this->create();
|
||||
case 'PUT':
|
||||
return $this->update($id);
|
||||
case 'DELETE':
|
||||
return $this->delete($id);
|
||||
default:
|
||||
http_response_code(405);
|
||||
return ['success' => false, 'error' => 'Method not allowed'];
|
||||
}
|
||||
}
|
||||
|
||||
private function handleAdult($method, $id, $segments) {
|
||||
|
||||
switch ($method) {
|
||||
case 'GET':
|
||||
if ($id) {
|
||||
return $this->getAdultOne($id);
|
||||
}
|
||||
return $this->getAdultAll();
|
||||
case 'POST':
|
||||
return $this->createAdult();
|
||||
case 'PUT':
|
||||
return $this->updateAdult($id);
|
||||
case 'DELETE':
|
||||
return $this->deleteAdultSpecifics($id);
|
||||
default:
|
||||
http_response_code(405);
|
||||
return ['success' => false, 'error' => 'Method not allowed'];
|
||||
}
|
||||
}
|
||||
|
||||
private function getAdultAll() {
|
||||
$filters = [];
|
||||
if (isset($_GET['search'])) $filters['search'] = $_GET['search'];
|
||||
if (isset($_GET['ethnicity'])) $filters['ethnicity'] = $_GET['ethnicity'];
|
||||
if (isset($_GET['hair_color'])) $filters['hair_color'] = $_GET['hair_color'];
|
||||
|
||||
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
||||
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 20;
|
||||
|
||||
$result = $this->adultCast->searchAdultActors($filters, $page, $limit);
|
||||
return ['success' => true, 'data' => $result];
|
||||
}
|
||||
|
||||
private function getAdultOne($id) {
|
||||
$cast = $this->adultCast->getWithAdultSpecifics($id);
|
||||
if (!$cast) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Adult actor not found'];
|
||||
}
|
||||
return ['success' => true, 'data' => $cast];
|
||||
}
|
||||
|
||||
private function createAdult() {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Invalid JSON'];
|
||||
}
|
||||
|
||||
$name = $data['name'] ?? null;
|
||||
if (!$name) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Name is required'];
|
||||
}
|
||||
|
||||
// Prüfen ob bereits Eintrag mit diesem cleanname existiert
|
||||
$cleanname = generateCleanName($name);
|
||||
$existing = $this->cast->findByCleanName($cleanname);
|
||||
|
||||
if ($existing) {
|
||||
// Update existing cast member with new photo if provided
|
||||
if (isset($data['photo']) && !empty($data['photo'])) {
|
||||
$this->adultCast->updateWithAdultSpecifics($existing['id'], $data);
|
||||
}
|
||||
http_response_code(200);
|
||||
$this->logger->logRequest('POST', '/api/cast/adult', [], $data);
|
||||
$this->logger->logResponse('POST', '/api/cast/adult', 200, ['id' => $existing['id'], 'message' => 'Cast already exists']);
|
||||
return ['success' => true, 'data' => ['id' => $existing['id'], 'message' => 'Cast already exists']];
|
||||
}
|
||||
|
||||
$castId = $this->adultCast->createWithAdultSpecifics($data);
|
||||
http_response_code(201);
|
||||
$this->logger->logRequest('POST', '/api/cast/adult', [], $data);
|
||||
$this->logger->logResponse('POST', '/api/cast/adult', 201, ['id' => $castId]);
|
||||
return ['success' => true, 'data' => ['id' => $castId]];
|
||||
}
|
||||
|
||||
private function updateAdult($id) {
|
||||
if (!$id) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'ID required'];
|
||||
}
|
||||
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Invalid JSON'];
|
||||
}
|
||||
|
||||
$this->adultCast->updateWithAdultSpecifics($id, $data);
|
||||
$this->logger->logRequest('PUT', "/api/cast/adult/$id", [], $data);
|
||||
$this->logger->logResponse('PUT', "/api/cast/adult/$id", 200, ['id' => $id]);
|
||||
return ['success' => true, 'data' => ['id' => $id]];
|
||||
}
|
||||
|
||||
private function deleteAdultSpecifics($id) {
|
||||
if (!$id) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'ID required'];
|
||||
}
|
||||
|
||||
$deleted = $this->adultCast->deleteAdultSpecifics($id);
|
||||
if (!$deleted) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Adult specifics not found'];
|
||||
}
|
||||
$this->logger->logRequest('DELETE', "/api/cast/adult/$id", [], null);
|
||||
$this->logger->logResponse('DELETE', "/api/cast/adult/$id", 200, ['message' => 'Adult specifics deleted successfully']);
|
||||
return ['success' => true, 'message' => 'Adult specifics deleted successfully'];
|
||||
}
|
||||
private function getOne($id, $segments) {
|
||||
// Prüfen ob /media angehängt wurde
|
||||
if (isset($segments[2]) && $segments[2] === 'media') {
|
||||
return $this->getMedia($id);
|
||||
}
|
||||
|
||||
$cast = $this->cast->getWithFilmography($id);
|
||||
$cast['adult_specifics'] = $this->adultCast->getAdultSpecifics($id);
|
||||
|
||||
|
||||
if (!$cast) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Cast member not found'];
|
||||
}
|
||||
return ['success' => true, 'data' => $cast];
|
||||
}
|
||||
|
||||
private function getMedia($castId) {
|
||||
$media = $this->cast->getMediaForCast($castId);
|
||||
return ['success' => true, 'data' => ['items' => $media]];
|
||||
}
|
||||
|
||||
private function getAll() {
|
||||
$filters = [];
|
||||
if (isset($_GET['search'])) $filters['search'] = $_GET['search'];
|
||||
|
||||
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
||||
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 20;
|
||||
|
||||
$result = $this->cast->search($filters, $page, $limit);
|
||||
return ['success' => true, 'data' => $result];
|
||||
}
|
||||
|
||||
private function create() {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Invalid JSON'];
|
||||
}
|
||||
|
||||
$name = $data['name'] ?? null;
|
||||
if (!$name) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Name is required'];
|
||||
}
|
||||
|
||||
// Prüfen ob bereits Eintrag mit diesem cleanname existiert
|
||||
$cleanname = generateCleanName($name);
|
||||
$existing = $this->cast->findByCleanName($cleanname);
|
||||
|
||||
if ($existing) {
|
||||
// Update existing cast member with new photo if provided
|
||||
if (isset($data['photo']) && !empty($data['photo'])) {
|
||||
$this->cast->updateWithOccupations($existing['id'], $data);
|
||||
}
|
||||
http_response_code(200);
|
||||
$this->logger->logRequest('POST', '/api/cast', [], $data);
|
||||
$this->logger->logResponse('POST', '/api/cast', 200, ['id' => $existing['id'], 'message' => 'Cast already exists']);
|
||||
return ['success' => true, 'data' => ['id' => $existing['id'], 'message' => 'Cast already exists']];
|
||||
}
|
||||
|
||||
$castId = $this->cast->createWithOccupations($data);
|
||||
http_response_code(201);
|
||||
$this->logger->logRequest('POST', '/api/cast', [], $data);
|
||||
$this->logger->logResponse('POST', '/api/cast', 201, ['id' => $castId]);
|
||||
return ['success' => true, 'data' => ['id' => $castId]];
|
||||
}
|
||||
|
||||
private function update($id) {
|
||||
if (!$id) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'ID required'];
|
||||
}
|
||||
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Invalid JSON'];
|
||||
}
|
||||
|
||||
$this->cast->updateWithOccupations($id, $data);
|
||||
$this->logger->logRequest('PUT', "/api/cast/$id", [], $data);
|
||||
$this->logger->logResponse('PUT', "/api/cast/$id", 200, ['id' => $id]);
|
||||
return ['success' => true, 'data' => ['id' => $id]];
|
||||
}
|
||||
|
||||
private function delete($id) {
|
||||
if (!$id) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'ID required'];
|
||||
}
|
||||
|
||||
$deleted = $this->cast->delete($id);
|
||||
if (!$deleted) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Cast member not found'];
|
||||
}
|
||||
$this->logger->logRequest('DELETE', "/api/cast/$id", [], null);
|
||||
$this->logger->logResponse('DELETE', "/api/cast/$id", 200, ['message' => 'Cast member deleted successfully']);
|
||||
return ['success' => true, 'message' => 'Cast member deleted successfully'];
|
||||
}
|
||||
}
|
||||
68
api/controllers/ImageController.php
Normal file
68
api/controllers/ImageController.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
class ImageController {
|
||||
private $imageDir;
|
||||
|
||||
public function __construct() {
|
||||
$this->imageDir = __DIR__ . '/../public/images/';
|
||||
}
|
||||
|
||||
public function handleRequest($method, $pathSegments) {
|
||||
// Remove 'images' from path segments
|
||||
array_shift($pathSegments);
|
||||
|
||||
// Build file path
|
||||
$imagePath = implode('/', $pathSegments);
|
||||
$fullPath = $this->imageDir . $imagePath;
|
||||
|
||||
// Security check: ensure the path is within the images directory
|
||||
$realPath = realpath($fullPath);
|
||||
$realImageDir = realpath($this->imageDir);
|
||||
|
||||
if ($realPath === false || strpos($realPath, $realImageDir) !== 0) {
|
||||
http_response_code(403);
|
||||
return ['success' => false, 'error' => 'Access denied'];
|
||||
}
|
||||
|
||||
// Check if file exists
|
||||
if (!file_exists($realPath)) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Image not found'];
|
||||
}
|
||||
|
||||
// Check if it's actually a file
|
||||
if (!is_file($realPath)) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Not a file'];
|
||||
}
|
||||
|
||||
// Get file info
|
||||
$fileInfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$mimeType = finfo_file($fileInfo, $realPath);
|
||||
finfo_close($fileInfo);
|
||||
|
||||
if ($mimeType === false) {
|
||||
// Fallback to common image types
|
||||
$extension = strtolower(pathinfo($realPath, PATHINFO_EXTENSION));
|
||||
$mimeTypes = [
|
||||
'jpg' => 'image/jpeg',
|
||||
'jpeg' => 'image/jpeg',
|
||||
'png' => 'image/png',
|
||||
'gif' => 'image/gif',
|
||||
'webp' => 'image/webp',
|
||||
'svg' => 'image/svg+xml'
|
||||
];
|
||||
$mimeType = $mimeTypes[$extension] ?? 'application/octet-stream';
|
||||
}
|
||||
|
||||
// Set headers for image serving
|
||||
header('Content-Type: ' . $mimeType);
|
||||
header('Content-Length: ' . filesize($realPath));
|
||||
header('Cache-Control: public, max-age=31536000'); // Cache for 1 year
|
||||
header('Pragma: public');
|
||||
|
||||
// Output the image
|
||||
readfile($realPath);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
403
api/controllers/MediaController.php
Normal file
403
api/controllers/MediaController.php
Normal file
@@ -0,0 +1,403 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../models/Media.php';
|
||||
require_once __DIR__ . '/../models/Series.php';
|
||||
require_once __DIR__ . '/../models/Music.php';
|
||||
require_once __DIR__ . '/../models/Game.php';
|
||||
require_once __DIR__ . '/../services/ApiLogger.php';
|
||||
|
||||
class MediaController {
|
||||
private $media;
|
||||
private $series;
|
||||
private $music;
|
||||
private $game;
|
||||
private $logger;
|
||||
|
||||
public function __construct($pdo) {
|
||||
$this->media = new Media($pdo);
|
||||
$this->series = new Series($pdo);
|
||||
$this->music = new Music($pdo);
|
||||
$this->game = new Game($pdo);
|
||||
$this->logger = ApiLogger::getInstance();
|
||||
}
|
||||
|
||||
public function handleRequest($method, $segments) {
|
||||
$id = isset($segments[1]) ? (int)$segments[1] : null;
|
||||
$subResource = isset($segments[2]) ? $segments[2] : null;
|
||||
|
||||
// Sub-Endpunkte für Episoden und Tracks
|
||||
if ($id && $subResource) {
|
||||
if ($subResource === 'episodes') {
|
||||
return $this->handleEpisodes($method, $id, $segments);
|
||||
}
|
||||
if ($subResource === 'tracks') {
|
||||
return $this->handleTracks($method, $id, $segments);
|
||||
}
|
||||
}
|
||||
|
||||
switch ($method) {
|
||||
case 'GET':
|
||||
return $id ? $this->getOne($id) : $this->getAll();
|
||||
case 'POST':
|
||||
return $this->create();
|
||||
case 'PUT':
|
||||
return $this->update($id);
|
||||
case 'DELETE':
|
||||
return $this->delete($id);
|
||||
default:
|
||||
http_response_code(405);
|
||||
return ['success' => false, 'error' => 'Method not allowed'];
|
||||
}
|
||||
}
|
||||
|
||||
private function handleEpisodes($method, $mediaId, $segments) {
|
||||
$episodeId = isset($segments[3]) ? (int)$segments[3] : null;
|
||||
|
||||
switch ($method) {
|
||||
case 'GET':
|
||||
if ($episodeId) {
|
||||
return $this->getEpisode($episodeId);
|
||||
}
|
||||
return $this->getEpisodes($mediaId);
|
||||
case 'POST':
|
||||
return $this->addEpisode($mediaId);
|
||||
case 'PUT':
|
||||
return $this->updateEpisode($episodeId);
|
||||
case 'DELETE':
|
||||
return $this->deleteEpisode($episodeId);
|
||||
default:
|
||||
http_response_code(405);
|
||||
return ['success' => false, 'error' => 'Method not allowed'];
|
||||
}
|
||||
}
|
||||
|
||||
private function handleTracks($method, $mediaId, $segments) {
|
||||
$trackId = isset($segments[3]) ? (int)$segments[3] : null;
|
||||
|
||||
switch ($method) {
|
||||
case 'GET':
|
||||
if ($trackId) {
|
||||
return $this->getTrack($trackId);
|
||||
}
|
||||
return $this->getTracks($mediaId);
|
||||
case 'POST':
|
||||
return $this->addTrack($mediaId);
|
||||
case 'PUT':
|
||||
return $this->updateTrack($trackId);
|
||||
case 'DELETE':
|
||||
return $this->deleteTrack($trackId);
|
||||
default:
|
||||
http_response_code(405);
|
||||
return ['success' => false, 'error' => 'Method not allowed'];
|
||||
}
|
||||
}
|
||||
|
||||
private function getEpisodes($mediaId) {
|
||||
$season = isset($_GET['season']) ? (int)$_GET['season'] : null;
|
||||
$episodes = $this->series->getEpisodes($mediaId, $season);
|
||||
return ['success' => true, 'data' => ['items' => $episodes]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new episode to a series
|
||||
* @param int $mediaId Media ID
|
||||
* @return array Created episode ID
|
||||
*/
|
||||
private function addEpisode($mediaId) {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Invalid JSON'];
|
||||
}
|
||||
|
||||
$episodeId = $this->series->addEpisode($mediaId, $data);
|
||||
http_response_code(201);
|
||||
return ['success' => true, 'data' => ['id' => $episodeId]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing episode
|
||||
* @param int $episodeId Episode ID
|
||||
* @return array Updated episode ID
|
||||
*/
|
||||
private function updateEpisode($episodeId) {
|
||||
if (!$episodeId) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Episode ID required'];
|
||||
}
|
||||
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Invalid JSON'];
|
||||
}
|
||||
|
||||
$this->series->updateEpisode($episodeId, $data);
|
||||
return ['success' => true, 'data' => ['id' => $episodeId]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an episode
|
||||
* @param int $episodeId Episode ID
|
||||
* @return array Success message
|
||||
*/
|
||||
private function deleteEpisode($episodeId) {
|
||||
if (!$episodeId) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Episode ID required'];
|
||||
}
|
||||
|
||||
$deleted = $this->series->deleteEpisode($episodeId);
|
||||
if (!$deleted) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Episode not found'];
|
||||
}
|
||||
return ['success' => true, 'message' => 'Episode deleted successfully'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single episode by ID
|
||||
* @param int $episodeId Episode ID
|
||||
* @return array Episode data
|
||||
*/
|
||||
private function getEpisode($episodeId) {
|
||||
// Episode direkt aus Datenbank abrufen
|
||||
$stmt = $this->series->getConnection()->prepare("SELECT * FROM episodes WHERE id = ?");
|
||||
$stmt->execute([$episodeId]);
|
||||
$episode = $stmt->fetch();
|
||||
|
||||
if (!$episode) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Episode not found'];
|
||||
}
|
||||
return ['success' => true, 'data' => $episode];
|
||||
}
|
||||
|
||||
private function getTracks($mediaId) {
|
||||
$tracks = $this->music->getTracks($mediaId);
|
||||
return ['success' => true, 'data' => ['items' => $tracks]];
|
||||
}
|
||||
|
||||
private function addTrack($mediaId) {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Invalid JSON'];
|
||||
}
|
||||
|
||||
$trackId = $this->music->addTrack($mediaId, $data);
|
||||
http_response_code(201);
|
||||
return ['success' => true, 'data' => ['id' => $trackId]];
|
||||
}
|
||||
|
||||
private function updateTrack($trackId) {
|
||||
if (!$trackId) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Track ID required'];
|
||||
}
|
||||
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Invalid JSON'];
|
||||
}
|
||||
|
||||
$this->music->updateTrack($trackId, $data);
|
||||
return ['success' => true, 'data' => ['id' => $trackId]];
|
||||
}
|
||||
|
||||
private function deleteTrack($trackId) {
|
||||
if (!$trackId) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Track ID required'];
|
||||
}
|
||||
|
||||
$deleted = $this->music->deleteTrack($trackId);
|
||||
if (!$deleted) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Track not found'];
|
||||
}
|
||||
return ['success' => true, 'message' => 'Track deleted successfully'];
|
||||
}
|
||||
|
||||
private function getTrack($trackId) {
|
||||
// Track direkt aus Datenbank abrufen
|
||||
$stmt = $this->music->getConnection()->prepare("SELECT * FROM tracks WHERE id = ?");
|
||||
$stmt->execute([$trackId]);
|
||||
$track = $stmt->fetch();
|
||||
|
||||
if (!$track) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Track not found'];
|
||||
}
|
||||
return ['success' => true, 'data' => $track];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single media item by ID
|
||||
* @param int $id Media ID
|
||||
* @return array Media object with relations
|
||||
*/
|
||||
private function getOne($id) {
|
||||
// Zuerst Basis-Media abrufen um Typ zu bestimmen
|
||||
$baseMedia = $this->media->getBase($id);
|
||||
if (!$baseMedia) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Media not found'];
|
||||
}
|
||||
|
||||
// Typ-spezifisches Abrufen
|
||||
switch ($baseMedia['type']) {
|
||||
case 'TV':
|
||||
$media = $this->series->getWithEpisodes($id);
|
||||
break;
|
||||
case 'Album':
|
||||
$media = $this->music->getWithTracks($id);
|
||||
break;
|
||||
case 'Game':
|
||||
$media = $this->game->getWithGameInfo($id);
|
||||
break;
|
||||
default:
|
||||
$media = $this->media->getWithRelations($id);
|
||||
}
|
||||
|
||||
return ['success' => true, 'data' => $media];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all media items with filtering and pagination
|
||||
* @return array Paginated media list
|
||||
*/
|
||||
private function getAll() {
|
||||
$filters = [];
|
||||
if (isset($_GET['category'])) $filters['category'] = $_GET['category'];
|
||||
if (isset($_GET['type'])) $filters['type'] = $_GET['type'];
|
||||
if (isset($_GET['search'])) $filters['search'] = $_GET['search'];
|
||||
|
||||
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
||||
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 20;
|
||||
|
||||
$result = $this->media->search($filters, $page, $limit);
|
||||
|
||||
// Game-spezifische Daten für Games laden
|
||||
foreach ($result['items'] as &$item) {
|
||||
if ($item['type'] === 'Game') {
|
||||
$gameInfo = $this->game->getGameInfoForList($item['id']);
|
||||
if ($gameInfo) {
|
||||
$item = array_merge($item, $gameInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ['success' => true, 'data' => $result];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new media item
|
||||
* @return array Created media ID
|
||||
*/
|
||||
private function create() {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Invalid JSON'];
|
||||
}
|
||||
|
||||
error_log("MediaController::create - Data received, poster field exists: " . (isset($data['poster']) ? 'yes' : 'no'));
|
||||
if (isset($data['poster'])) {
|
||||
error_log("MediaController::create - Poster length: " . strlen($data['poster']));
|
||||
error_log("MediaController::create - Poster starts with: " . substr($data['poster'], 0, 50));
|
||||
}
|
||||
|
||||
$title = $data['title'] ?? null;
|
||||
if (!$title) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Title is required'];
|
||||
}
|
||||
|
||||
// Prüfen ob bereits Eintrag mit diesem cleanname existiert
|
||||
$cleanname = generateCleanName($title);
|
||||
$existing = $this->media->findByCleanName($cleanname);
|
||||
|
||||
if ($existing) {
|
||||
http_response_code(200);
|
||||
$this->logger->logRequest('POST', '/api/media', [], $data);
|
||||
$this->logger->logResponse('POST', '/api/media', 200, ['id' => $existing['id'], 'message' => 'Media already exists']);
|
||||
return ['success' => true, 'data' => ['id' => $existing['id'], 'message' => 'Media already exists']];
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Typ-spezifisches Erstellen
|
||||
$type = $data['type'] ?? null;
|
||||
if ($type === 'Game') {
|
||||
$mediaId = $this->game->createWithRelations($data);
|
||||
} elseif ($type === 'TV') {
|
||||
$mediaId = $this->series->createWithRelations($data);
|
||||
} elseif ($type === 'Album') {
|
||||
$mediaId = $this->music->createWithRelations($data);
|
||||
} else {
|
||||
$mediaId = $this->media->createWithRelations($data);
|
||||
}
|
||||
|
||||
http_response_code(201);
|
||||
$this->logger->logRequest('POST', '/api/media', [], $data);
|
||||
$this->logger->logResponse('POST', '/api/media', 201, ['id' => $mediaId]);
|
||||
return ['success' => true, 'data' => ['id' => $mediaId]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing media item
|
||||
* @param int $id Media ID
|
||||
* @return array Updated media ID
|
||||
*/
|
||||
private function update($id) {
|
||||
if (!$id) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'ID required'];
|
||||
}
|
||||
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Invalid JSON'];
|
||||
}
|
||||
|
||||
// Typ-spezifisches Aktualisieren
|
||||
$type = $data['type'] ?? null;
|
||||
if ($type === 'Game') {
|
||||
$this->game->updateWithRelations($id, $data);
|
||||
} elseif ($type === 'TV') {
|
||||
$this->series->updateWithRelations($id, $data);
|
||||
} elseif ($type === 'Album') {
|
||||
$this->music->updateWithRelations($id, $data);
|
||||
} else {
|
||||
$this->media->updateWithRelations($id, $data);
|
||||
}
|
||||
|
||||
$this->logger->logRequest('PUT', "/api/media/$id", [], $data);
|
||||
$this->logger->logResponse('PUT', "/api/media/$id", 200, ['id' => $id]);
|
||||
return ['success' => true, 'data' => ['id' => $id]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a media item
|
||||
* @param int $id Media ID
|
||||
* @return array Success message
|
||||
*/
|
||||
private function delete($id) {
|
||||
if (!$id) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'ID required'];
|
||||
}
|
||||
|
||||
$deleted = $this->media->delete($id);
|
||||
if (!$deleted) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Media not found'];
|
||||
}
|
||||
$this->logger->logRequest('DELETE', "/api/media/$id", [], null);
|
||||
$this->logger->logResponse('DELETE', "/api/media/$id", 200, ['message' => 'Media deleted successfully']);
|
||||
return ['success' => true, 'message' => 'Media deleted successfully'];
|
||||
}
|
||||
}
|
||||
61
api/controllers/SettingsController.php
Normal file
61
api/controllers/SettingsController.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../models/Settings.php';
|
||||
require_once __DIR__ . '/../services/ApiLogger.php';
|
||||
|
||||
class SettingsController {
|
||||
private $settings;
|
||||
private $logger;
|
||||
|
||||
public function __construct($pdo) {
|
||||
$this->settings = new Settings($pdo);
|
||||
$this->logger = ApiLogger::getInstance();
|
||||
}
|
||||
|
||||
public function handleRequest($method, $segments) {
|
||||
$path = '/' . implode('/', $segments);
|
||||
$this->logger->logRequest($method, $path);
|
||||
|
||||
switch ($method) {
|
||||
case 'GET':
|
||||
return $this->get();
|
||||
case 'PUT':
|
||||
return $this->update();
|
||||
default:
|
||||
http_response_code(405);
|
||||
return ['success' => false, 'error' => 'Method not allowed'];
|
||||
}
|
||||
}
|
||||
|
||||
private function get() {
|
||||
$settings = $this->settings->getSettings();
|
||||
|
||||
if (!$settings) {
|
||||
http_response_code(404);
|
||||
return ['success' => false, 'error' => 'Settings not found'];
|
||||
}
|
||||
|
||||
return ['success' => true, 'data' => $settings];
|
||||
}
|
||||
|
||||
private function update() {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
return ['success' => false, 'error' => 'Invalid JSON'];
|
||||
}
|
||||
|
||||
$settings = $this->settings->updateSettings($data);
|
||||
|
||||
if (!$settings) {
|
||||
http_response_code(500);
|
||||
return ['success' => false, 'error' => 'Failed to update settings'];
|
||||
}
|
||||
|
||||
$this->logger->logRequest('PUT', '/api/settings', [], $data);
|
||||
$this->logger->logResponse('PUT', '/api/settings', 200, $settings);
|
||||
|
||||
return ['success' => true, 'data' => $settings];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user