mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
109 lines
3.3 KiB
PHP
109 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
class Source extends Model
|
|
{
|
|
protected string $table = 'sources';
|
|
protected array $fillable = [
|
|
'name',
|
|
'display_name',
|
|
'api_url',
|
|
'api_key',
|
|
'config',
|
|
'is_active',
|
|
'last_sync_at'
|
|
];
|
|
|
|
public function __construct(\PDO $pdo, array $data = [])
|
|
{
|
|
parent::__construct($pdo, $data);
|
|
}
|
|
|
|
public function games(): array
|
|
{
|
|
$stmt = $this->pdo->prepare("SELECT * FROM games WHERE source_id = :source_id");
|
|
$stmt->execute(['source_id' => $this->id]);
|
|
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
public function movies(): array
|
|
{
|
|
$stmt = $this->pdo->prepare("SELECT * FROM movies WHERE source_id = :source_id");
|
|
$stmt->execute(['source_id' => $this->id]);
|
|
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
public function tvShows(): array
|
|
{
|
|
$stmt = $this->pdo->prepare("SELECT * FROM tv_shows WHERE source_id = :source_id");
|
|
$stmt->execute(['source_id' => $this->id]);
|
|
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
public function musicArtists(): array
|
|
{
|
|
$stmt = $this->pdo->prepare("SELECT * FROM music_artists WHERE source_id = :source_id");
|
|
$stmt->execute(['source_id' => $this->id]);
|
|
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
public function getSyncLogs(): array
|
|
{
|
|
$stmt = $this->pdo->prepare("SELECT * FROM sync_logs WHERE source_id = :source_id ORDER BY created_at DESC");
|
|
$stmt->execute(['source_id' => $this->id]);
|
|
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
public function createSyncLog(string $syncType, string $status): int
|
|
{
|
|
$data = [
|
|
'source_id' => $this->id,
|
|
'sync_type' => $syncType,
|
|
'status' => $status,
|
|
'total_items' => 0,
|
|
'processed_items' => 0,
|
|
'new_items' => 0,
|
|
'updated_items' => 0,
|
|
'deleted_items' => 0,
|
|
'started_at' => date('Y-m-d H:i:s')
|
|
];
|
|
|
|
$columns = array_keys($data);
|
|
$placeholders = array_map(fn($col) => ":$col", $columns);
|
|
$sql = "INSERT INTO sync_logs (" . implode(', ', $columns) . ") VALUES (" . implode(', ', $placeholders) . ")";
|
|
|
|
$stmt = $this->pdo->prepare($sql);
|
|
$stmt->execute($data);
|
|
|
|
return (int) $this->pdo->lastInsertId();
|
|
}
|
|
|
|
public function updateSyncLog(int $syncLogId, string $status, array $stats = []): bool
|
|
{
|
|
$data = [
|
|
'status' => $status,
|
|
'processed_items' => $stats['processed_items'] ?? 0,
|
|
'new_items' => $stats['new_items'] ?? 0,
|
|
'updated_items' => $stats['updated_items'] ?? 0,
|
|
'deleted_items' => $stats['deleted_items'] ?? 0,
|
|
'completed_at' => date('Y-m-d H:i:s')
|
|
];
|
|
|
|
if (!empty($stats['errors'])) {
|
|
$data['errors'] = json_encode($stats['errors']);
|
|
}
|
|
|
|
if (!empty($stats['message'])) {
|
|
$data['message'] = $stats['message'];
|
|
}
|
|
|
|
$setClause = array_map(fn($col) => "$col = :$col", array_keys($data));
|
|
$sql = "UPDATE sync_logs SET " . implode(', ', $setClause) . " WHERE id = :id";
|
|
$data['id'] = $syncLogId;
|
|
|
|
$stmt = $this->pdo->prepare($sql);
|
|
return $stmt->execute($data);
|
|
}
|
|
}
|