From 2393419e2fa687a593cd85ffac565930a0d87e34 Mon Sep 17 00:00:00 2001
From: debbyalmadea <almadeaputri@gmail.com>
Date: Sat, 30 Sep 2023 20:09:35 +0700
Subject: [PATCH] add: update method in base repository

---
 src/server/app/App/Repository.php             | 36 +++++++++++++++++++
 .../app/Controller/CatalogController.php      | 21 +++++++----
 .../app/Repository/CatalogRepository.php      | 13 -------
 src/server/app/Service/CatalogService.php     | 11 ++++++
 4 files changed, 61 insertions(+), 20 deletions(-)

diff --git a/src/server/app/App/Repository.php b/src/server/app/App/Repository.php
index cb47c61..061c84a 100644
--- a/src/server/app/App/Repository.php
+++ b/src/server/app/App/Repository.php
@@ -173,6 +173,42 @@ abstract class Repository
         }
     }
 
+    public function update(Domain $domain)
+    {
+        $domainKeyLength = count($domain->toArray());
+        $query = "UPDATE {$this->table} SET ";
+
+        $countKey = 0;
+        foreach ($domain->toArray() as $key => $value) {
+            if ($key != 'id') {
+                $query .= "$key = :$key";
+            }
+
+            if ($countKey < $domainKeyLength - 1) {
+                $query .= ", ";
+            }
+
+            $countKey += 1;
+        }
+
+        $query .= " WHERE id = :id";
+
+        $statement = $this->connection->prepare($query);
+        foreach ($domain->toArray() as $key => $value) {
+            if ($key != 'id') {
+                $statement->bindValue(":$key", $value);
+            }
+        }
+
+        $statement->execute();
+
+        try {
+            return $domain;
+        } finally {
+            $statement->closeCursor();
+        }
+    }
+
     public function deleteAll(): void
     {
         $this->connection->exec("DELETE FROM {$this->table}");
diff --git a/src/server/app/Controller/CatalogController.php b/src/server/app/Controller/CatalogController.php
index 50fa483..2bd2af9 100644
--- a/src/server/app/Controller/CatalogController.php
+++ b/src/server/app/Controller/CatalogController.php
@@ -67,18 +67,25 @@ class CatalogController
 
     public function detail(): void
     {
+        $uuid = '6517b94da6b8c';
+        $catalog = $this->catalogService->findByUUID($uuid);
+
+        if (!$catalog) {
+            View::render('catalog/not-found', [
+                'title' => 'Catalog Not Found',
+                'styles' => [
+                    '/css/catalog-not-found.css',
+                ],
+            ]);
+            return;
+        }
+
         View::render('catalog/detail', [
             'title' => 'Catalog Detail',
             'styles' => [
                 '/css/catalog-detail.css',
             ],
-            'data' => [
-                'title' => 'Snowdrop',
-                'category' => 'ANIME',
-                'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
-                'poster' => 'jihu-13.jpg',
-                'trailer' => 'the-journey-of-elaina.mp4'
-            ]
+            'data' => $catalog->toArray()
         ]);
     }
 
diff --git a/src/server/app/Repository/CatalogRepository.php b/src/server/app/Repository/CatalogRepository.php
index e781a50..a76ebe9 100644
--- a/src/server/app/Repository/CatalogRepository.php
+++ b/src/server/app/Repository/CatalogRepository.php
@@ -67,17 +67,4 @@ class CatalogRepository extends Repository
             return null;
         }
     }
-
-    public function deleteAll(): void
-    {
-        $this->connection->exec("DELETE FROM catalogs");
-    }
-
-    public function deleteById(int $id): void
-    {
-        $statement = $this->connection->prepare("DELETE FROM catalogs WHERE id = ?");
-        $statement->execute([$id]);
-
-        $statement->closeCursor();
-    }
 }
\ No newline at end of file
diff --git a/src/server/app/Service/CatalogService.php b/src/server/app/Service/CatalogService.php
index c826613..9e79fa5 100644
--- a/src/server/app/Service/CatalogService.php
+++ b/src/server/app/Service/CatalogService.php
@@ -35,6 +35,17 @@ class CatalogService
         return $catalogs;
     }
 
+    public function findByUUID(string $uuid): ?Catalog
+    {
+        $catalog = $this->catalogRepository->findOne('uuid', $uuid);
+        return $catalog;
+    }
+
+    public function deleteByUUID(string $uuid): void
+    {
+        $this->catalogRepository->deleteBy('uuid', $uuid);
+    }
+
     public function create(CatalogCreateRequest $request): CatalogCreateResponse
     {
         $this->validateCatalogCreateRequest($request);
-- 
GitLab