mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
first commit
This commit is contained in:
103
app/Models/Source.php
Normal file
103
app/Models/Source.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?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 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user