From a2367e1ac81d76eee3d54fdc38eb1629a91d594a Mon Sep 17 00:00:00 2001
From: Kenneth Dave <dave.bahana@gmail.com>
Date: Tue, 3 Oct 2023 21:14:50 +0700
Subject: [PATCH] feat: add mvc and core adjustments

---
 .env                                   |  4 +--
 Dockerfile                             |  5 +--
 app/controllers/NotFoundController.php | 10 ++++++
 app/controllers/UserController.php     |  0
 app/core/Controller.php                | 23 ++++++++++++++
 app/core/Database.php                  | 11 +++++++
 app/exceptions/LoggedException.php     | 15 +++++++++
 app/interface/ControllerInterface.php  |  6 ++++
 app/interface/ViewInterface.php        |  6 ++++
 app/models/BookModel.php               | 11 +++++++
 app/models/UserModel.php               | 11 +++++++
 docker-compose.yml                     |  8 ++++-
 index.html                             | 42 --------------------------
 index.php                              | 42 --------------------------
 public/index.php                       |  4 +++
 15 files changed, 109 insertions(+), 89 deletions(-)
 create mode 100644 app/controllers/NotFoundController.php
 create mode 100644 app/controllers/UserController.php
 create mode 100644 app/core/Controller.php
 create mode 100644 app/exceptions/LoggedException.php
 create mode 100644 app/interface/ControllerInterface.php
 create mode 100644 app/interface/ViewInterface.php
 create mode 100644 app/models/BookModel.php
 create mode 100644 app/models/UserModel.php
 delete mode 100644 index.html
 delete mode 100644 index.php
 create mode 100644 public/index.php

diff --git a/.env b/.env
index b8ee5f8..949ba7e 100644
--- a/.env
+++ b/.env
@@ -1,5 +1,5 @@
-MYSQL_HOST=db-tubes-1
-MYSQL_DATABASE=tubes-wbd
+MYSQL_HOST=host-wbd-1
+MYSQL_DATABASE=db-wbd-1
 MYSQL_USER=root
 MYSQL_PASSWORD=katakunci
 MYSQL_PORT=3306
\ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
index ac9e46d..9805a6e 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -2,6 +2,7 @@ FROM php:8.0-apache
 
 WORKDIR /var/www/html
 
-COPY index.php /var/www/html
-
+COPY public/index.php .
+RUN docker-php-ext-install mysqli pdo pdo_mysql
+RUN a2enmod rewrite
 EXPOSE 80
\ No newline at end of file
diff --git a/app/controllers/NotFoundController.php b/app/controllers/NotFoundController.php
new file mode 100644
index 0000000..2a6d582
--- /dev/null
+++ b/app/controllers/NotFoundController.php
@@ -0,0 +1,10 @@
+<?php
+
+class NotFoundController extends Controller implements ControllerInterface
+{
+    public function index()
+    {
+        $notFoundView = $this->view('not-found', 'NotFoundView');
+        $notFoundView->render();
+    }
+}
\ No newline at end of file
diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php
new file mode 100644
index 0000000..e69de29
diff --git a/app/core/Controller.php b/app/core/Controller.php
new file mode 100644
index 0000000..acdb193
--- /dev/null
+++ b/app/core/Controller.php
@@ -0,0 +1,23 @@
+<?php
+
+class Controller
+{
+    public function view($folder, $view, $data = [])
+    {
+        require_once __DIR__ . '/../views/' . $folder . '/' . $view . '.php';
+        return new $view($data);
+    }
+
+    public function model($model)
+    {
+        echo "Model here";
+        require_once __DIR__ . '/../models/' . $model . '.php';
+        return new $model();
+    }
+
+   /* public function middleware($middleware)
+    {
+        require_once __DIR__ . '/../middlewares/' . $middleware . '.php';
+        return new $middleware();
+    } */
+}
\ No newline at end of file
diff --git a/app/core/Database.php b/app/core/Database.php
index 8a8845d..1b1b99c 100644
--- a/app/core/Database.php
+++ b/app/core/Database.php
@@ -19,6 +19,7 @@ class Database
             $this->db_connection = new PDO($dsn, $this->user, $this->password, $option);
         } catch (PDOException) {
             throw new LoggedException('Bad Gateway', 502);
+            echo "Bad Gateway";
         }
 
         try {
@@ -28,7 +29,9 @@ class Database
             $this->db_connection->exec(Tables::INVENTORY_TABLE);
         } catch (PDOException) {
             throw new LoggedException('Internal Server Error', 500);
+            echo "Internal Server Error";
         }
+        echo "Thank you, next";
     }
 
     public function query($query)
@@ -97,4 +100,12 @@ class Database
             throw new LoggedException('Internal Server Error', 500);
         }
     }
+    public function lastInsertID()
+    {
+        try {
+            return $this->db_connection->lastInsertId();
+        } catch (PDOException) {
+            throw new LoggedException('Internal Server Error', 500);
+        }
+    }
 }
\ No newline at end of file
diff --git a/app/exceptions/LoggedException.php b/app/exceptions/LoggedException.php
new file mode 100644
index 0000000..0622d24
--- /dev/null
+++ b/app/exceptions/LoggedException.php
@@ -0,0 +1,15 @@
+<?php
+
+class LoggedException extends Exception
+{
+    public function __construct($message = '', $code = 0)
+    {
+        parent::__construct($message, $code);
+        $this->logError($message, $code);
+    }
+
+    private function logError($message, $code)
+    {
+        error_log('[ERROR] ' . $code . ': ' . $message);
+    }
+}
\ No newline at end of file
diff --git a/app/interface/ControllerInterface.php b/app/interface/ControllerInterface.php
new file mode 100644
index 0000000..1a711bb
--- /dev/null
+++ b/app/interface/ControllerInterface.php
@@ -0,0 +1,6 @@
+<?php
+
+interface ControllerInterface
+{
+    public function index();
+}
\ No newline at end of file
diff --git a/app/interface/ViewInterface.php b/app/interface/ViewInterface.php
new file mode 100644
index 0000000..3f2806a
--- /dev/null
+++ b/app/interface/ViewInterface.php
@@ -0,0 +1,6 @@
+<?php
+
+interface ViewInterface
+{
+    public function render();
+}
\ No newline at end of file
diff --git a/app/models/BookModel.php b/app/models/BookModel.php
new file mode 100644
index 0000000..5d695ef
--- /dev/null
+++ b/app/models/BookModel.php
@@ -0,0 +1,11 @@
+
+<?php
+class BookModel
+{
+    private $database;
+
+    public function __construct()
+    {
+        $this->database = new Database();
+    }
+}
\ No newline at end of file
diff --git a/app/models/UserModel.php b/app/models/UserModel.php
new file mode 100644
index 0000000..b1e4d41
--- /dev/null
+++ b/app/models/UserModel.php
@@ -0,0 +1,11 @@
+<?php
+
+class UserModel
+{
+    private $database;
+
+    public function __construct()
+    {
+        $this->database = new Database();
+    }
+}
\ No newline at end of file
diff --git a/docker-compose.yml b/docker-compose.yml
index 87773b9..96607e0 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -4,12 +4,18 @@ services:
     image: tubes-1:latest
     ports:
       - 8008:80
+    hostname: web-wbd-1
+    env_file: .env
+    volumes:
+      - ./src:/var/www/html
   mysql:
     image: mysql:latest
     ports:
       - 3307:3306
-    hostname: db-tubes-1
+    hostname: db-wbd-1
     env_file: .env
+    volumes:
+      - ./mysql:/var/lib/mysql
 
 
 
diff --git a/index.html b/index.html
deleted file mode 100644
index 547041c..0000000
--- a/index.html
+++ /dev/null
@@ -1,42 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-
-<head>
-    <meta charset="UTF-8">
-    <meta http-equiv=""X-UA-Compatible" content="IE=edge">
-    <meta name = "viewport" content="width=device-width, initial-scale=1.0">
-    <title> Sign in</title>
-    <link rel ="stylesheet" href="style.css">
-    <link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>
-</head>
-
-<body>
-    <div class="wrapper">
-        <form action="">
-            <h1>Login</h1>
-            <div class="input-box">
-                <input type="text" placeholder="Username" required>
-                <i class='bx bxs-user'></i>
-
-            </div>
-            <div class="input-box">
-                <input type="password" placeholder="Password" required>
-                <i class='bx bxs-lock-alt'></i>
-            </div>
-
-            <div class="remember-forgot">
-                <label><input type="checkbox"> Remember me</label>
-                <a href="#">Forgot password?</a>
-            </div>
-            
-            <button type="submit" class="btn">Login</button>
-
-            <div class="register-link">
-                <p>Don't have an account? 
-                    <a href="#">Register</a>
-                </p>
-            </div>
-        </form>
-    </div>
-</body>
-</html>
\ No newline at end of file
diff --git a/index.php b/index.php
deleted file mode 100644
index 9065187..0000000
--- a/index.php
+++ /dev/null
@@ -1,42 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-
-<head>
-    <meta charset="UTF-8">
-    <meta http-equiv=""X-UA-Compatible" content="IE=edge">
-    <meta name = "viewport" content="width=device-width, initial-scale=1.0">
-    <title> Sign in</title>
-    <link rel ="stylesheet" type="text/css" href="style.css">
-    <link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>
-</head>
-
-<body>
-    <div class="wrapper">
-        <form action="">
-            <h1>Login</h1>
-            <div class="input-box">
-                <input type="text" placeholder="Username" required>
-                <i class='bx bxs-user'></i>
-
-            </div>
-            <div class="input-box">
-                <input type="password" placeholder="Password" required>
-                <i class='bx bxs-lock-alt'></i>
-            </div>
-
-            <div class="remember-forgot">
-                <label><input type="checkbox"> Remember me</label>
-                <a href="#">Forgot password?</a>
-            </div>
-            
-            <button type="submit" class="btn">Login</button>
-
-            <div class="register-link">
-                <p>Don't have an account? 
-                    <a href="#">Register</a>
-                </p>
-            </div>
-        </form>
-    </div>
-</body>
-</html>
\ No newline at end of file
diff --git a/public/index.php b/public/index.php
new file mode 100644
index 0000000..90aa015
--- /dev/null
+++ b/public/index.php
@@ -0,0 +1,4 @@
+<?php 
+$app = new App();
+echo "Sawadikap";
+?>
\ No newline at end of file
-- 
GitLab