From 2c63ed2a70a23b62081537fb878c6b85146fa306 Mon Sep 17 00:00:00 2001
From: debbyalmadea <almadeaputri@gmail.com>
Date: Mon, 25 Sep 2023 23:37:13 +0700
Subject: [PATCH] add: Catalog Domain and Repository

---
 .gitignore                                    |  3 +-
 src/migration/db.sql                          | 29 +++++++++-
 src/server/app/Domain/Catalog.php             | 16 ++++++
 src/server/app/Domain/User.php                |  4 +-
 .../app/Repository/CatalogRepository.php      | 55 +++++++++++++++++++
 src/server/app/Repository/UserRepository.php  | 14 +++--
 6 files changed, 114 insertions(+), 7 deletions(-)
 create mode 100644 src/server/app/Domain/Catalog.php
 create mode 100644 src/server/app/Repository/CatalogRepository.php

diff --git a/.gitignore b/.gitignore
index 89ee3e2..7e10641 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 db/
-.env
\ No newline at end of file
+.env
+.DS_Store
\ No newline at end of file
diff --git a/src/migration/db.sql b/src/migration/db.sql
index 334ae68..cea385c 100644
--- a/src/migration/db.sql
+++ b/src/migration/db.sql
@@ -1,11 +1,38 @@
+DO $$
+BEGIN
+    IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'role') THEN
+        CREATE TYPE role AS ENUM ('BASIC', 'ADMIN');
+    END IF;
+END
+$$;
+
 CREATE TABLE IF NOT EXISTS users (
     id VARCHAR(255) PRIMARY KEY,
     name VARCHAR(255) NOT NULL,
-    password VARCHAR(255) NOT NULL
+    password VARCHAR(255) NOT NULL,
+    email VARCHAR(255) UNIQUE NOT NULL,
+    role role DEFAULT 'BASIC'
 );
 
 CREATE TABLE IF NOT EXISTS sessions (
     id VARCHAR(255) PRIMARY KEY,
     user_id VARCHAR(255) NOT NULL,
     FOREIGN KEY (user_id) REFERENCES users(id)
+);
+
+DO $$
+BEGIN
+    IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'category') THEN
+        CREATE TYPE category AS ENUM ('ANIME', 'DRAMA');
+    END IF;
+END
+$$;
+
+CREATE TABLE IF NOT EXISTS catalogs (
+    id SERIAL PRIMARY KEY,
+    title VARCHAR(255) NOT NULL,
+    description VARCHAR(255),
+    poster VARCHAR(255) NOT NULL,
+    trailer VARCHAR(255),
+    category category NOT NULL
 );
\ No newline at end of file
diff --git a/src/server/app/Domain/Catalog.php b/src/server/app/Domain/Catalog.php
new file mode 100644
index 0000000..0240610
--- /dev/null
+++ b/src/server/app/Domain/Catalog.php
@@ -0,0 +1,16 @@
+<?php
+
+class Catalog
+{
+    public int $id;
+    public string $title;
+    public ?string $description = null;
+    public string $poster;
+    public ?string $trailer = null;
+    public string $category;
+
+    public function __toString()
+    {
+        return "Catalog: {id: $this->id, title: $this->title, description: $this->description, poster: $this->poster, trailer: $this->trailer, category: $this->category}";
+    }
+}
\ No newline at end of file
diff --git a/src/server/app/Domain/User.php b/src/server/app/Domain/User.php
index 68b3a7d..df35025 100644
--- a/src/server/app/Domain/User.php
+++ b/src/server/app/Domain/User.php
@@ -5,4 +5,6 @@ class User
     public string $id;
     public string $name;
     public string $password;
-}
+    public string $email;
+    public string $role;
+}
\ No newline at end of file
diff --git a/src/server/app/Repository/CatalogRepository.php b/src/server/app/Repository/CatalogRepository.php
new file mode 100644
index 0000000..088f388
--- /dev/null
+++ b/src/server/app/Repository/CatalogRepository.php
@@ -0,0 +1,55 @@
+<?php
+
+require_once __DIR__ . '/../Domain/Catalog.php';
+
+class CatalogRepository
+{
+    private \PDO $connection;
+
+    public function __construct(\PDO $connection)
+    {
+        $this->connection = $connection;
+    }
+
+    public function save(Catalog $catalog): Catalog
+    {
+        $statement = $this->connection->prepare("INSERT INTO catalogs(title, description, poster, trailer, category) VALUES (?, ?, ?, ?, ?)");
+        $statement->execute([
+            $catalog->title,
+            $catalog->description,
+            $catalog->poster,
+            $catalog->trailer,
+            $catalog->category,
+        ]);
+        return $catalog;
+    }
+
+    public function findById(string $id): ?Catalog
+    {
+        $statement = $this->connection->prepare("SELECT id, title, description, poster, trailer, category FROM catalogs WHERE id = ?");
+        $statement->execute([$id]);
+
+        try {
+            if ($row = $statement->fetch()) {
+                $catalog = new Catalog();
+                $catalog->id = $row['id'];
+                $catalog->title = $row['title'];
+                $catalog->description = $row['description'];
+                $catalog->poster = $row['poster'];
+                $catalog->trailer = $row['trailer'];
+                $catalog->category = $row['category'];
+
+                return $catalog;
+            } else {
+                return null;
+            }
+        } finally {
+            $statement->closeCursor();
+        }
+    }
+
+    public function deleteAll(): void
+    {
+        $this->connection->exec("DELETE FROM catalogs");
+    }
+}
\ No newline at end of file
diff --git a/src/server/app/Repository/UserRepository.php b/src/server/app/Repository/UserRepository.php
index e998831..9223010 100644
--- a/src/server/app/Repository/UserRepository.php
+++ b/src/server/app/Repository/UserRepository.php
@@ -13,16 +13,20 @@ class UserRepository
 
     public function save(User $user): User
     {
-        $statement = $this->connection->prepare("INSERT INTO users(id, name, password) VALUES (?, ?, ?)");
+        $statement = $this->connection->prepare("INSERT INTO users(id, name, password, email, role) VALUES (?, ?, ?)");
         $statement->execute([
-            $user->id, $user->name, $user->password,
+            $user->id,
+            $user->name,
+            $user->password,
+            $user->email,
+            $user->role,
         ]);
         return $user;
     }
 
     public function findById(string $id): ?User
     {
-        $statement = $this->connection->prepare("SELECT id, name, password FROM users WHERE id = ?");
+        $statement = $this->connection->prepare("SELECT id, name, password, email, role FROM users WHERE id = ?");
         $statement->execute([$id]);
 
         try {
@@ -31,6 +35,8 @@ class UserRepository
                 $user->id = $row['id'];
                 $user->name = $row['name'];
                 $user->password = $row['password'];
+                $user->email = $row['email'];
+                $user->role = $row['role'];
 
                 return $user;
             } else {
@@ -45,4 +51,4 @@ class UserRepository
     {
         $this->connection->exec("DELETE FROM users");
     }
-}
+}
\ No newline at end of file
-- 
GitLab