Files
mystuff_backend/api/models/Music.php
Lars Behrends e38a6e1f7b Add strict types and type hints across API
Apply strict_types and extensive type declarations throughout the API and models, improving type safety and error handling. Key changes: add declare(strict_types=1) to many files; convert properties, method parameters and return values to typed signatures (PDO, arrays, ints, strings, bools, nullables); switch exception handling to Throwable in index and Router; improve Router, controllers and model method signatures and nullability handling; refine file/image serving security checks and headers in ImageController; strengthen Database typing and initialization methods; return explicit types from BaseModel CRUD helpers and counting; update Media/Cast/Adult/Game/Console/Settings controllers and models to use typed methods, better validation, and clearer update/create return types. Also add AGENTS.md (agent skills index), update README with Swagger/OpenAPI usage instructions, and add /.windsurf to .gitignore. These changes aim to harden runtime correctness, make intended contracts explicit, and prepare the codebase for easier maintenance and static analysis.
2026-04-16 16:40:31 +02:00

115 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/MediaType.php';
class Music extends MediaType {
protected function getType(): string {
return 'Album';
}
protected function getTypeSpecificFields(): array {
return ['artist', 'album_type', 'release_date'];
}
protected function validateTypeSpecificFields(array $data): array {
// Music spezifische Validierung
return $data;
}
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
// Nur Music suchen
$filters['type'] = 'Album';
return parent::search($filters, $page, $limit);
}
public function getWithTracks(?int $id): ?array {
$media = $this->getWithRelations($id);
if (!$media) {
return null;
}
$media['tracks'] = $this->getTracks($id);
return $media;
}
public function getTracks(?int $mediaId): array {
$stmt = $this->pdo->prepare("
SELECT * FROM tracks WHERE media_id = ? ORDER BY track_number
");
$stmt->execute([$mediaId]);
return $stmt->fetchAll();
}
public function addTrack(?int $mediaId, array $trackData): int {
$stmt = $this->pdo->prepare("
INSERT INTO tracks (media_id, track_number, title, artist)
VALUES (?, ?, ?, ?)
");
$stmt->execute([
$mediaId,
$trackData['track_number'] ?? null,
$trackData['title'] ?? null,
//$trackData['duration'] ?? null,
$trackData['artist'] ?? null
]);
return (int)$this->pdo->lastInsertId();
}
public function updateTrack(?int $trackId, array $trackData): bool {
$fields = [];
$params = [];
foreach (['track_number', 'title', 'artist'] as $field) {
if (array_key_exists($field, $trackData)) {
$fields[] = "$field = ?";
$params[] = $trackData[$field];
}
}
if (!empty($fields)) {
$params[] = $trackId;
$stmt = $this->pdo->prepare("UPDATE tracks SET " . implode(', ', $fields) . " WHERE id = ?");
$stmt->execute($params);
return true;
}
return false;
}
public function deleteTrack(?int $trackId): bool {
$stmt = $this->pdo->prepare("DELETE FROM tracks WHERE id = ?");
$stmt->execute([$trackId]);
return $stmt->rowCount() > 0;
}
public function createWithRelations(array $data): int {
$mediaId = parent::createWithRelations($data);
// Tracks speichern
if (isset($data['tracks']) && is_array($data['tracks'])) {
foreach ($data['tracks'] as $track) {
$this->addTrack($mediaId, $track);
}
}
return $mediaId;
}
public function updateWithRelations(int $id, array $data): bool {
parent::updateWithRelations($id, $data);
// Tracks aktualisieren
if (isset($data['tracks']) && is_array($data['tracks'])) {
// Alle existierenden Tracks löschen
$this->pdo->prepare("DELETE FROM tracks WHERE media_id = ?")->execute([$id]);
// Neue Tracks hinzufügen
foreach ($data['tracks'] as $track) {
$this->addTrack($id, $track);
}
}
return true;
}
}