From f520e79b12fa27b02d7ba4f96113f27aa6e6674b Mon Sep 17 00:00:00 2001
From: Kenneth Ezekiel <88850771+KenEzekiel@users.noreply.github.com>
Date: Wed, 15 Nov 2023 15:49:41 +0700
Subject: [PATCH] feat: admin endpoint

---
 src/App.php                               |  2 ++
 src/base/BaseRepository.php               | 42 +++++++++++------------
 src/controllers/AdminController.php       | 27 +++++++++++++++
 src/controllers/SoapPremiumController.php |  1 -
 src/services/UserService.php              |  5 +++
 5 files changed, 55 insertions(+), 22 deletions(-)
 create mode 100644 src/controllers/AdminController.php

diff --git a/src/App.php b/src/App.php
index 1b49c61..030c9fa 100644
--- a/src/App.php
+++ b/src/App.php
@@ -14,6 +14,7 @@ use app\controllers\RegisterController;
 use app\controllers\UpdateFilmController;
 use app\controllers\UserDashboardController;
 use app\controllers\SoapPremiumController;
+use app\controllers\AdminController;
 use app\repositories\UserRepository;
 use app\repositories\ReviewRepository;
 use app\services\UserService;
@@ -55,5 +56,6 @@ class App
     $this->router->addRoute('/cancel-premium', SoapPremiumController::class);
     $this->router->addRoute('/approve-premium', SoapPremiumController::class);
     $this->router->addRoute('/reject-premium', SoapPremiumController::class);
+    $this->router->addRoute('/admins', AdminController::class);
   }
 }
diff --git a/src/base/BaseRepository.php b/src/base/BaseRepository.php
index 49154a7..c2bd62f 100644
--- a/src/base/BaseRepository.php
+++ b/src/base/BaseRepository.php
@@ -48,27 +48,27 @@ abstract class BaseRepository
 
     $conditions = [];
 
-      // Mapping where
-      if (count($where) > 0) {
-          foreach ($where as $key => $value) {
-              $columns = [$key];
-              if (isset($value[3]) and is_array(($value[3]))) {
-                  $columns = [$key] + $value[3];
-              }
-              $subConditions = [];
-              foreach ($columns as $column) {
-                  if (isset($value[2]) and $value[2] == 'LIKE') {
-                      $subConditions[] = "LOWER($column) LIKE LOWER(:$column)";
-                  } else {
-                      $subConditions[] = "$column = :$column";
-                  }
-              }
-              $conditions[] = "(" . implode(" OR ", $subConditions) . ")";
+    // Mapping where
+    if (count($where) > 0) {
+      foreach ($where as $key => $value) {
+        $columns = [$key];
+        if (isset($value[3]) and is_array(($value[3]))) {
+          $columns = [$key] + $value[3];
+        }
+        $subConditions = [];
+        foreach ($columns as $column) {
+          if (isset($value[2]) and $value[2] == 'LIKE') {
+            $subConditions[] = "LOWER($column) LIKE LOWER(:$column)";
+          } else {
+            $subConditions[] = "$column = :$column";
           }
-
-          $sql .= " WHERE " . implode(" AND ", $conditions);
+        }
+        $conditions[] = "(" . implode(" OR ", $subConditions) . ")";
       }
 
+      $sql .= " WHERE " . implode(" AND ", $conditions);
+    }
+
     // Hydrating statement, for sanitizing
     $stmt = $this->pdo->prepare($sql);
     // Bind values
@@ -98,7 +98,7 @@ abstract class BaseRepository
     $pageNo = null,
     $pageSize = null,
     $sort = "asc",
-    $isInitialSync = "no"
+    $isInitialSync = "yes"
   ) {
     $sql = "SELECT * FROM $this->tableName";
 
@@ -132,9 +132,9 @@ abstract class BaseRepository
         $pollingSql .= " WHERE ";
       }
       if ($pollingDurationMinutes !== false && is_numeric($pollingDurationMinutes) && $pollingDurationMinutes) {
-          $pollingSql .= "last_updated >= DATE_SUB(NOW(), INTERVAL $pollingDurationMinutes MINUTE)";
+        $pollingSql .= "last_updated >= DATE_SUB(NOW(), INTERVAL $pollingDurationMinutes MINUTE)";
       } else {
-          $pollingSql .= "last_updated >= DATE_SUB(NOW(), INTERVAL 30 MINUTE)";
+        $pollingSql .= "last_updated >= DATE_SUB(NOW(), INTERVAL 30 MINUTE)";
       }
       $sql .= $pollingSql;
     }
diff --git a/src/controllers/AdminController.php b/src/controllers/AdminController.php
new file mode 100644
index 0000000..ad80aec
--- /dev/null
+++ b/src/controllers/AdminController.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace app\controllers;
+
+use app\base\BaseController;
+use app\controllers\utils\response;
+use app\Request;
+use app\services\UserService;
+use Exception;
+
+class AdminController extends BaseController
+{
+  public function __construct()
+  {
+    parent::__construct(UserService::getInstance());
+  }
+
+  protected function get($urlParams)
+  {
+    $admins = $this->service->getAllAdmin();
+    $admin_emails = [];
+    foreach ($admins as $data) {
+      array_push($admin_emails, $data['email']);
+    }
+    response::send_json_response($admin_emails);
+  }
+}
diff --git a/src/controllers/SoapPremiumController.php b/src/controllers/SoapPremiumController.php
index 018fadf..2b46385 100644
--- a/src/controllers/SoapPremiumController.php
+++ b/src/controllers/SoapPremiumController.php
@@ -6,7 +6,6 @@ use app\base\BaseController;
 use app\controllers\utils\response;
 use app\client\SoapClient;
 use app\Request;
-use app\models\SoapPremiumModel;
 use Exception;
 
 class SoapPremiumController extends BaseController
diff --git a/src/services/UserService.php b/src/services/UserService.php
index ea48a8e..e616faf 100644
--- a/src/services/UserService.php
+++ b/src/services/UserService.php
@@ -272,4 +272,9 @@ class UserService extends BaseService
   {
     return $this->repository->deleteById($user_id);
   }
+
+  public function getAllAdmin()
+  {
+    return $this->repository->findAll(['role' => ['admin', PDO::PARAM_STR, "="]]);
+  }
 }
-- 
GitLab