From 99bb40b8bc17b8ee2cd8468a82e46ea280c92cb6 Mon Sep 17 00:00:00 2001
From: Yassine Doghri <yassine@doghri.fr>
Date: Sun, 23 Jan 2022 16:02:24 +0000
Subject: [PATCH] fix: clear cache when deleting podcast banner

---
 app/Models/MediaModel.php                     | 27 +++++++++++++++++++
 .../Admin/Controllers/PodcastController.php   |  3 +++
 2 files changed, 30 insertions(+)

diff --git a/app/Models/MediaModel.php b/app/Models/MediaModel.php
index bc61689d23..b3900618a3 100644
--- a/app/Models/MediaModel.php
+++ b/app/Models/MediaModel.php
@@ -65,6 +65,18 @@ class MediaModel extends Model
         'updated_by',
     ];
 
+    /**
+     * clear cache before update if by any chance, the podcast name changes, so will the podcast link
+     *
+     * @var string[]
+     */
+    protected $beforeUpdate = ['clearCache'];
+
+    /**
+     * @var string[]
+     */
+    protected $beforeDelete = ['clearCache'];
+
     /**
      * Model constructor.
      *
@@ -166,4 +178,19 @@ class MediaModel extends Model
 
         return $this->delete($media->id, true);
     }
+
+    /**
+     * @param mixed[] $data
+     *
+     * @return mixed[]
+     */
+    protected function clearCache(array $data): array
+    {
+        $mediaId = (is_array($data['id']) ? $data['id'][0] : $data['id']);
+
+        cache()
+            ->delete("media#{$mediaId}");
+
+        return $data;
+    }
 }
diff --git a/modules/Admin/Controllers/PodcastController.php b/modules/Admin/Controllers/PodcastController.php
index cf56486020..8cbbcb96c9 100644
--- a/modules/Admin/Controllers/PodcastController.php
+++ b/modules/Admin/Controllers/PodcastController.php
@@ -381,6 +381,9 @@ class PodcastController extends BaseController
                 ->withInput()
                 ->with('errors', $mediaModel->errors());
         }
+        (new PodcastModel())->clearCache([
+            'id' => $this->podcast->id,
+        ]);
 
         // remove banner url from actor
         $actor = (new ActorModel())->getActorById($this->podcast->actor_id);
-- 
GitLab