From fc6efddb1f0e2c6998ade780c9c656a7ebef163f Mon Sep 17 00:00:00 2001
From: Angela Livia Arumsari <16521177@mahasiswa.itb.ac.id>
Date: Mon, 13 Nov 2023 23:22:58 +0700
Subject: [PATCH] feat: add initial sync in search and filter

---
 src/base/BaseRepository.php         | 16 ++++++++++++++++
 src/repositories/FilmRepository.php | 12 ++++++++----
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/src/base/BaseRepository.php b/src/base/BaseRepository.php
index a8bc49d..49154a7 100644
--- a/src/base/BaseRepository.php
+++ b/src/base/BaseRepository.php
@@ -98,6 +98,7 @@ abstract class BaseRepository
     $pageNo = null,
     $pageSize = null,
     $sort = "asc",
+    $isInitialSync = "no"
   ) {
     $sql = "SELECT * FROM $this->tableName";
 
@@ -124,6 +125,21 @@ abstract class BaseRepository
       $sql .= " WHERE " . implode(" AND ", $conditions);
     }
 
+    if ($isInitialSync == "no") {
+      $pollingDurationMinutes = intval(getenv('POLLING_DURATION_MINUTES'));
+      $pollingSql = "";
+      if (count($where) == 0) {
+        $pollingSql .= " WHERE ";
+      }
+      if ($pollingDurationMinutes !== false && is_numeric($pollingDurationMinutes) && $pollingDurationMinutes) {
+          $pollingSql .= "last_updated >= DATE_SUB(NOW(), INTERVAL $pollingDurationMinutes MINUTE)";
+      } else {
+          $pollingSql .= "last_updated >= DATE_SUB(NOW(), INTERVAL 30 MINUTE)";
+      }
+      $sql .= $pollingSql;
+    }
+
+
     if ($order) {
       $sql .= " ORDER BY $order";
     }
diff --git a/src/repositories/FilmRepository.php b/src/repositories/FilmRepository.php
index ff7f274..2b8fb7f 100644
--- a/src/repositories/FilmRepository.php
+++ b/src/repositories/FilmRepository.php
@@ -36,7 +36,8 @@ class FilmRepository extends BaseRepository
     $genre = 'all',
     $released_year = 'all',
     $pageNo = 1,
-    $limit = 10
+    $limit = 10,
+    $isInitialSync = "no"
   ) {
     $where = [];
 
@@ -47,10 +48,13 @@ class FilmRepository extends BaseRepository
       $where['released_year'] = [$released_year, PDO::PARAM_INT];
     }
     if (isset($word) and !empty($word)) {
-      $where['title'] = [$genre, PDO::PARAM_STR, 'LIKE', ['director']];
+      $where['title'] = [$word, PDO::PARAM_STR, 'LIKE', ['director']];
     }
 
-    return $this->findAll($where, $order, $pageNo, $limit, $isDesc);
+    $data = $this->findAll($where, $order, $pageNo, $limit, $isDesc, $isInitialSync);
+    error_log(count($data));
+
+    return $this->findAll($where, $order, $pageNo, $limit, $isDesc, $isInitialSync);
   }
 
   public function countRowBySearchAndFilter($word, $genre = 'all', $released_year = 'all')
@@ -64,7 +68,7 @@ class FilmRepository extends BaseRepository
       $where['released_year'] = [$released_year, PDO::PARAM_INT];
     }
     if (isset($word) and !empty($word)) {
-      $where['title'] = [$genre, PDO::PARAM_STR, 'LIKE', ['director']];
+      $where['title'] = [$word, PDO::PARAM_STR, 'LIKE', ['director']];
     }
 
     return $this->countRow($where);
-- 
GitLab