]> git.localhorst.tv Git - alttp.git/commitdiff
properly purge episodes
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 19 Feb 2023 17:11:26 +0000 (18:11 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 19 Feb 2023 17:11:26 +0000 (18:11 +0100)
app/Console/Commands/SyncSpeedGaming.php

index 177ee1947675b67485ffa8ca1148439a94572ce0..656a079cf87ad2c843b9131c0c6a42bc6e104776 100644 (file)
@@ -59,7 +59,7 @@ class SyncSpeedGaming extends Command {
                        '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);
@@ -69,12 +69,19 @@ class SyncSpeedGaming extends Command {
                }
        }
 
-       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) {