From da0f0472819007e02e5da37399f2377772c618b9 Mon Sep 17 00:00:00 2001 From: Yassine Doghri Date: Thu, 2 Jul 2020 10:08:32 +0000 Subject: [PATCH] feat(cache): add podcast and episode pages to cache + clear them after insert or update - throw not found page error if no podcast in podcast controller - delete unnecessary unknownuseragents view --- app/Config/Routes.php | 4 ++-- app/Controllers/Episode.php | 7 +++++-- app/Controllers/Podcast.php | 14 +++++++++++--- app/Controllers/UnknownUserAgents.php | 11 ++--------- .../Migrations/2020-06-05-170000_add_episodes.php | 12 +----------- app/Models/EpisodeModel.php | 15 ++++++--------- app/Models/PodcastModel.php | 12 +++++++++--- app/Models/UnknownUserAgentsModel.php | 4 ++-- app/Views/json/unknownuseragents.php | 5 ----- 9 files changed, 38 insertions(+), 46 deletions(-) delete mode 100644 app/Views/json/unknownuseragents.php diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 4bb4f1cb..5db584e1 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -22,8 +22,8 @@ $routes->setDefaultMethod('index'); $routes->setTranslateURIDashes(false); $routes->set404Override(); $routes->setAutoRoute(false); -$routes->addPlaceholder('podcastName', '[a-z0-9\_]{1,191}'); -$routes->addPlaceholder('episodeSlug', '[a-z0-9\-]{1,191}'); +$routes->addPlaceholder('podcastName', '[a-zA-Z0-9\_]{1,191}'); +$routes->addPlaceholder('episodeSlug', '[a-zA-Z0-9\-]{1,191}'); /** * -------------------------------------------------------------------- diff --git a/app/Controllers/Episode.php b/app/Controllers/Episode.php index 8be56caf..fc14e895 100644 --- a/app/Controllers/Episode.php +++ b/app/Controllers/Episode.php @@ -49,7 +49,7 @@ class Episode extends BaseController 'image' => 'uploaded[image]|is_image[image]|ext_in[image,jpg,png]|permit_empty', 'title' => 'required', - 'slug' => 'required', + 'slug' => 'required|regex_match[[a-zA-Z0-9\-]{1,191}]', 'description' => 'required', 'type' => 'required', ]) @@ -105,7 +105,7 @@ class Episode extends BaseController 'image' => 'uploaded[image]|is_image[image]|ext_in[image,jpg,png]|permit_empty', 'title' => 'required', - 'slug' => 'required', + 'slug' => 'required|regex_match[[a-zA-Z0-9\-]{1,191}]', 'description' => 'required', 'type' => 'required', ]) @@ -164,6 +164,9 @@ class Episode extends BaseController public function view() { + // The page cache is set to a decade so it is deleted manually upon podcast update + $this->cachePage(DECADE); + self::triggerWebpageHit($this->podcast->id); $data = [ diff --git a/app/Controllers/Podcast.php b/app/Controllers/Podcast.php index 8e518508..081f7b9f 100644 --- a/app/Controllers/Podcast.php +++ b/app/Controllers/Podcast.php @@ -18,7 +18,12 @@ class Podcast extends BaseController { if (count($params) > 0) { $podcast_model = new PodcastModel(); - $this->podcast = $podcast_model->where('name', $params[0])->first(); + if ( + !($podcast = $podcast_model->where('name', $params[0])->first()) + ) { + throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound(); + } + $this->podcast = $podcast; } return $this->$method(); @@ -32,7 +37,7 @@ class Podcast extends BaseController if ( !$this->validate([ 'title' => 'required', - 'name' => 'required|regex_match[^[a-z0-9\_]{1,191}$]', + 'name' => 'required|regex_match[[a-zA-Z0-9\_]{1,191}]', 'description' => 'required|max_length[4000]', 'image' => 'uploaded[image]|is_image[image]|ext_in[image,jpg,png]', @@ -91,7 +96,7 @@ class Podcast extends BaseController if ( !$this->validate([ 'title' => 'required', - 'name' => 'required|regex_match[^[a-z0-9\_]{1,191}$]', + 'name' => 'required|regex_match[[a-zA-Z0-9\_]{1,191}]', 'description' => 'required|max_length[4000]', 'image' => 'uploaded[image]|is_image[image]|ext_in[image,jpg,png]|permit_empty', @@ -150,6 +155,9 @@ class Podcast extends BaseController public function view() { + // The page cache is set to a decade so it is deleted manually upon podcast update + $this->cachePage(DECADE); + self::triggerWebpageHit($this->podcast->id); $data = [ diff --git a/app/Controllers/UnknownUserAgents.php b/app/Controllers/UnknownUserAgents.php index 220daf86..82e1a298 100644 --- a/app/Controllers/UnknownUserAgents.php +++ b/app/Controllers/UnknownUserAgents.php @@ -3,17 +3,10 @@ use CodeIgniter\Controller; class UnknownUserAgents extends Controller { - public function index($p_id = 0) + public function index($last_known_id = 0) { $model = new \App\Models\UnknownUserAgentsModel(); - $data = [ - 'useragents' => $model->getUserAgents($p_id), - ]; - - $this->response->setContentType('application/json'); - $this->response->setStatusCode(\CodeIgniter\HTTP\Response::HTTP_OK); - - echo view('json/unknownuseragents', $data); + return $this->response->setJSON($model->getUserAgents($last_known_id)); } } diff --git a/app/Database/Migrations/2020-06-05-170000_add_episodes.php b/app/Database/Migrations/2020-06-05-170000_add_episodes.php index 11cf98d7..4a4fc528 100644 --- a/app/Database/Migrations/2020-06-05-170000_add_episodes.php +++ b/app/Database/Migrations/2020-06-05-170000_add_episodes.php @@ -58,13 +58,6 @@ class AddEpisodes extends Migration 'comment' => 'An episode description. Description is text containing one or more sentences describing your episode to potential listeners. You can specify up to 4000 characters. You can use rich text formatting and some HTML (

,

    ,