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.
141 lines
4.4 KiB
PHP
141 lines
4.4 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/MediaType.php';
|
|
|
|
class Series extends MediaType {
|
|
protected function getType() {
|
|
return 'TV';
|
|
}
|
|
|
|
protected function getTypeSpecificFields() {
|
|
return ['runtime', 'director', 'writer'];
|
|
}
|
|
|
|
protected function validateTypeSpecificFields($data) {
|
|
// Series Validierung
|
|
if (isset($data['runtime']) && !is_numeric($data['runtime'])) {
|
|
throw new Exception('Runtime must be a number');
|
|
}
|
|
}
|
|
|
|
public function search($filters = [], $page = 1, $limit = 20) {
|
|
// Nur Series suchen
|
|
$filters['type'] = 'TV';
|
|
return parent::search($filters, $page, $limit);
|
|
}
|
|
|
|
public function getWithEpisodes($id) {
|
|
$media = $this->getWithRelations($id);
|
|
if (!$media) {
|
|
return null;
|
|
}
|
|
|
|
$media['episodes'] = $this->getEpisodes($id);
|
|
$media['seasons'] = $this->getSeasons($id);
|
|
|
|
return $media;
|
|
}
|
|
|
|
public function getEpisodes($mediaId, $season = null) {
|
|
$query = "SELECT * FROM episodes WHERE media_id = ?";
|
|
$params = [$mediaId];
|
|
|
|
if ($season !== null) {
|
|
$query .= " AND season = ?";
|
|
$params[] = $season;
|
|
}
|
|
|
|
$query .= " ORDER BY season, episode_number";
|
|
|
|
$stmt = $this->pdo->prepare($query);
|
|
$stmt->execute($params);
|
|
return $stmt->fetchAll();
|
|
}
|
|
|
|
public function getSeasons($mediaId) {
|
|
$stmt = $this->pdo->prepare("
|
|
SELECT DISTINCT season, COUNT(*) as episode_count,
|
|
MIN(air_date) as first_air_date, MAX(air_date) as last_air_date
|
|
FROM episodes
|
|
WHERE media_id = ?
|
|
GROUP BY season
|
|
ORDER BY season
|
|
");
|
|
$stmt->execute([$mediaId]);
|
|
return $stmt->fetchAll();
|
|
}
|
|
|
|
public function addEpisode($mediaId, $episodeData) {
|
|
$stmt = $this->pdo->prepare("
|
|
INSERT INTO episodes (media_id, season, episode_number, title, description, air_date, duration, thumbnail)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
");
|
|
$stmt->execute([
|
|
$mediaId,
|
|
$episodeData['season'] ?? 1,
|
|
$episodeData['episode_number'] ?? null,
|
|
$episodeData['title'] ?? null,
|
|
$episodeData['description'] ?? null,
|
|
$episodeData['air_date'] ?? null,
|
|
$episodeData['duration'] ?? null,
|
|
$episodeData['thumbnail'] ?? null
|
|
]);
|
|
return $this->pdo->lastInsertId();
|
|
}
|
|
|
|
public function updateEpisode($episodeId, $episodeData) {
|
|
$fields = [];
|
|
$params = [];
|
|
|
|
foreach (['season', 'episode_number', 'title', 'description', 'air_date', 'duration', 'thumbnail'] as $field) {
|
|
if (array_key_exists($field, $episodeData)) {
|
|
$fields[] = "$field = ?";
|
|
$params[] = $episodeData[$field];
|
|
}
|
|
}
|
|
|
|
if (!empty($fields)) {
|
|
$params[] = $episodeId;
|
|
$stmt = $this->pdo->prepare("UPDATE episodes SET " . implode(', ', $fields) . " WHERE id = ?");
|
|
$stmt->execute($params);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function deleteEpisode($episodeId) {
|
|
$stmt = $this->pdo->prepare("DELETE FROM episodes WHERE id = ?");
|
|
$stmt->execute([$episodeId]);
|
|
return $stmt->rowCount() > 0;
|
|
}
|
|
|
|
public function createWithRelations($data) {
|
|
$mediaId = parent::createWithRelations($data);
|
|
|
|
// Episoden speichern
|
|
if (isset($data['episodes']) && is_array($data['episodes'])) {
|
|
foreach ($data['episodes'] as $episode) {
|
|
$this->addEpisode($mediaId, $episode);
|
|
}
|
|
}
|
|
|
|
return $mediaId;
|
|
}
|
|
|
|
public function updateWithRelations($id, $data) {
|
|
parent::updateWithRelations($id, $data);
|
|
|
|
// Episoden aktualisieren
|
|
if (isset($data['episodes']) && is_array($data['episodes'])) {
|
|
// Alle existierenden Episoden löschen
|
|
$this->pdo->prepare("DELETE FROM episodes WHERE media_id = ?")->execute([$id]);
|
|
// Neue Episoden hinzufügen
|
|
foreach ($data['episodes'] as $episode) {
|
|
$this->addEpisode($id, $episode);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|