From a0e18283d74deec8b815ebe45fc6bcc2e9a4e645 Mon Sep 17 00:00:00 2001
From: debbyalmadea <almadeaputri@gmail.com>
Date: Tue, 26 Sep 2023 10:14:46 +0700
Subject: [PATCH] fix: user schema

---
 src/migration/db.sql                            | 6 +++---
 src/server/app/Domain/User.php                  | 2 +-
 src/server/app/Model/UserRegisterRequest.php    | 7 +++++--
 src/server/app/Repository/CatalogRepository.php | 2 +-
 src/server/app/Repository/UserRepository.php    | 2 +-
 src/server/app/Service/UserService.php          | 9 +++++----
 6 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/src/migration/db.sql b/src/migration/db.sql
index cea385c..ca25431 100644
--- a/src/migration/db.sql
+++ b/src/migration/db.sql
@@ -7,7 +7,7 @@ END
 $$;
 
 CREATE TABLE IF NOT EXISTS users (
-    id VARCHAR(255) PRIMARY KEY,
+    id SERIAL PRIMARY KEY,
     name VARCHAR(255) NOT NULL,
     password VARCHAR(255) NOT NULL,
     email VARCHAR(255) UNIQUE NOT NULL,
@@ -15,8 +15,8 @@ CREATE TABLE IF NOT EXISTS users (
 );
 
 CREATE TABLE IF NOT EXISTS sessions (
-    id VARCHAR(255) PRIMARY KEY,
-    user_id VARCHAR(255) NOT NULL,
+    id SERIAL PRIMARY KEY,
+    user_id INT NOT NULL,
     FOREIGN KEY (user_id) REFERENCES users(id)
 );
 
diff --git a/src/server/app/Domain/User.php b/src/server/app/Domain/User.php
index df35025..9cb6679 100644
--- a/src/server/app/Domain/User.php
+++ b/src/server/app/Domain/User.php
@@ -2,7 +2,7 @@
 
 class User
 {
-    public string $id;
+    public int $id;
     public string $name;
     public string $password;
     public string $email;
diff --git a/src/server/app/Model/UserRegisterRequest.php b/src/server/app/Model/UserRegisterRequest.php
index 402b8af..e637486 100644
--- a/src/server/app/Model/UserRegisterRequest.php
+++ b/src/server/app/Model/UserRegisterRequest.php
@@ -2,7 +2,10 @@
 
 class UserRegisterRequest
 {
-    public ?string $id = null;
+    public ?int $id = null;
     public ?string $name = null;
     public ?string $password = null;
-}
+    public ?string $confirm_password = null;
+    public ?string $email = null;
+    public ?string $role = null;
+}
\ No newline at end of file
diff --git a/src/server/app/Repository/CatalogRepository.php b/src/server/app/Repository/CatalogRepository.php
index 088f388..5d73852 100644
--- a/src/server/app/Repository/CatalogRepository.php
+++ b/src/server/app/Repository/CatalogRepository.php
@@ -24,7 +24,7 @@ class CatalogRepository
         return $catalog;
     }
 
-    public function findById(string $id): ?Catalog
+    public function findById(int $id): ?Catalog
     {
         $statement = $this->connection->prepare("SELECT id, title, description, poster, trailer, category FROM catalogs WHERE id = ?");
         $statement->execute([$id]);
diff --git a/src/server/app/Repository/UserRepository.php b/src/server/app/Repository/UserRepository.php
index 9223010..057d41d 100644
--- a/src/server/app/Repository/UserRepository.php
+++ b/src/server/app/Repository/UserRepository.php
@@ -24,7 +24,7 @@ class UserRepository
         return $user;
     }
 
-    public function findById(string $id): ?User
+    public function findById(int $id): ?User
     {
         $statement = $this->connection->prepare("SELECT id, name, password, email, role FROM users WHERE id = ?");
         $statement->execute([$id]);
diff --git a/src/server/app/Service/UserService.php b/src/server/app/Service/UserService.php
index 3ee9382..e309e9c 100644
--- a/src/server/app/Service/UserService.php
+++ b/src/server/app/Service/UserService.php
@@ -26,7 +26,6 @@ class UserService
             }
 
             $user = new User();
-            $user->id = $request->id;
             $user->name = $request->name;
             $user->password = password_hash($request->password, PASSWORD_BCRYPT);
 
@@ -46,11 +45,13 @@ class UserService
 
     private function validateUserRegistrationRequest(UserRegisterRequest $request)
     {
-        if ($request->id == null || $request->name == null | $request->password == null ||
-            trim($request->id) == "" || trim($request->name) == "" || trim($request->password) == "") {
+        if (
+            $request->name == null | $request->password == null ||
+            trim($request->name) == "" || trim($request->password) == ""
+        ) {
             throw new ValidationException("Id, name, password cannot be blank");
         }
 
         // more validations goes here
     }
-}
+}
\ No newline at end of file
-- 
GitLab