From 73f094daf26a8cf75e39ebff1eeb7f9039276312 Mon Sep 17 00:00:00 2001
From: Yassine Doghri <yassine@doghri.fr>
Date: Wed, 19 Oct 2022 11:35:08 +0000
Subject: [PATCH] revert(install): redirect to install in homepage if no
 database was set

---
 app/Controllers/HomeController.php | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/app/Controllers/HomeController.php b/app/Controllers/HomeController.php
index b2f28b55e6..68816c0c54 100644
--- a/app/Controllers/HomeController.php
+++ b/app/Controllers/HomeController.php
@@ -13,27 +13,27 @@ namespace App\Controllers;
 use App\Models\PodcastModel;
 use CodeIgniter\HTTP\RedirectResponse;
 use Config\Services;
-use Exception;
 
 class HomeController extends BaseController
 {
     public function index(): RedirectResponse | string
     {
-        $sortOptions = ['activity', 'created_desc', 'created_asc'];
-        $sortBy = in_array($this->request->getGet('sort'), $sortOptions, true) ? $this->request->getGet(
-            'sort'
-        ) : 'activity';
-
-        try {
-            $allPodcasts = (new PodcastModel())->getAllPodcasts($sortBy);
-        } catch (Exception) {
-            // Database connection has not been set or could not find the podcasts table
+        $db = db_connect();
+        if ($db->getDatabase() === '' || ! $db->tableExists('podcasts')) {
+            // Database has not been set or could not find the podcasts table
             // Redirecting to install page because it is likely that Castopod has not been installed yet.
             // NB: as base_url wouldn't have been defined here, redirect to install wizard manually
             $route = Services::routes()->reverseRoute('install');
             return redirect()->to(rtrim(host_url(), '/') . $route);
         }
 
+        $sortOptions = ['activity', 'created_desc', 'created_asc'];
+        $sortBy = in_array($this->request->getGet('sort'), $sortOptions, true) ? $this->request->getGet(
+            'sort'
+        ) : 'activity';
+
+        $allPodcasts = (new PodcastModel())->getAllPodcasts($sortBy);
+
         // check if there's only one podcast to redirect user to it
         if (count($allPodcasts) === 1) {
             return redirect()->route('podcast-activity', [$allPodcasts[0]->handle]);
-- 
GitLab