'from' => $from->toIso8601String(),
'to' => $to->toIso8601String(),
])->json();
- $this->purgeSchedule($event, $sgSchedule);
+ $this->purgeSchedule($event, $from, $to, $sgSchedule);
foreach ($sgSchedule as $sgEntry) {
try {
$this->syncSchedule($event, $sgEntry);
}
}
- private function purgeSchedule(Event $event, $sgSchedule) {
+ private function purgeSchedule(Event $event, $from, $to, $sgSchedule) {
$ext_ids = [];
foreach ($sgSchedule as $sgEntry) {
$ext_ids[] = 'sg:'.$sgEntry['id'];
}
- $event->episodes()->whereNotIn('ext_id', $ext_ids)->delete();
+ $to_purge = $event->episodes()
+ ->where('start', '>=', $from)
+ ->where('start', '<=', $to)
+ ->whereNotIn('ext_id', $ext_ids);
+ foreach ($to_purge->get() as $episode) {
+ $episode->players()->delete();
+ }
+ $to_purge->delete();
}
private function syncSchedule(Event $event, $sgEntry) {