From 6768dc440bed66ea426ebbd6b8ce2d973272fedc Mon Sep 17 00:00:00 2001
From: Kenneth Dave <dave.bahana@gmail.com>
Date: Sat, 30 Sep 2023 15:39:02 +0700
Subject: [PATCH] feat: add data tables and page example for docker

---
 .env               |  6 ++++
 Dockerfile         |  8 ++++-
 app/Database.php   | 77 ++++++++++++++++++++++++++++++++++++++++++++++
 app/tables.php     | 37 ++++++++++++++++++++++
 docker-compose.yml |  2 ++
 index.php          |  9 ++++++
 6 files changed, 138 insertions(+), 1 deletion(-)
 create mode 100644 .env
 create mode 100644 app/Database.php
 create mode 100644 app/tables.php
 create mode 100644 index.php

diff --git a/.env b/.env
new file mode 100644
index 0000000..e8c3c35
--- /dev/null
+++ b/.env
@@ -0,0 +1,6 @@
+MYSQL_ROOT_PASSWORD=password
+MYSQL_HOST=db-tubes-1
+MYSQL_DATABASE=tubeswbd
+MYSQL_USER=root
+MYSQL_PASSWORD=password
+MYSQL_PORT=3306
\ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
index 2e3ddd6..ac9e46d 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1 +1,7 @@
-FROM php:8.0-apache
\ No newline at end of file
+FROM php:8.0-apache
+
+WORKDIR /var/www/html
+
+COPY index.php /var/www/html
+
+EXPOSE 80
\ No newline at end of file
diff --git a/app/Database.php b/app/Database.php
new file mode 100644
index 0000000..b5d5a6b
--- /dev/null
+++ b/app/Database.php
@@ -0,0 +1,77 @@
+<?php
+
+$servername = "localhost";
+$username = "root";
+$password = "password"
+$dbname = "tubeswbd"
+
+// Koneksi
+$conn = new mysqli($servername, $username, $password, $dbname);
+
+// Cek gagal ato ga
+if ($conn->connect_error) {
+    // Nah gagal
+    die("Connection failed: " . $conn->connect_error);
+
+}
+
+
+// Query table author
+$authorTableQuery = "
+CREATE TABLE IF NOT EXISTS author (
+    id INT AUTO_INCREMENT PRIMARY KEY,
+    name VARCHAR(255) NOT NULL,
+);"
+
+if ($conn->query($authorTableQuery) === TRUE) {
+    echo "Author table created successfully<br>";
+} else {
+    echo "Error creating author table: " . $conn->error . "<br>";
+}
+
+
+// Query table book
+$bookTableQuery = "
+CREATE TABLE IF NOT EXISTS book (
+    id INT AUTO_INCREMENT PRIMARY KEY,
+    name VARCHAR(255) NOT NULL,
+    author_id INT,
+    FOREIGN KEY(author_id) REFERENCES author(id)
+
+)";
+if ($conn->query($bookTableQuery) === TRUE) {
+    echo "Book table created successfully<br>";
+} else {
+    echo "Error creating book table: " . $conn->error . "<br>";
+}
+
+
+// Query table user
+$userTableQuery = "
+CREATE TABLE IF NOT EXISTS user(
+    id INT AUTO_INCREMENT PRIMARY KEY,
+    username VARCHAR(20) NOT NULL,
+    password VARCHAR(255) NOT NULL,
+)";
+if ($conn->query($userTableQuery) === TRUE) {
+    echo "User table created successfully<br>";
+} else {
+    echo "Error creating user table: " . $conn->error . "<br>";
+}
+// Query table inventory for bookmark
+$inventoryTableQUery = "
+CREATE TABLE IF NOT EXISTS inventory (
+    id INT AUTO_INCREMENT PRIMARY KEY,
+    user_id INT NOT NULL,
+    book_id INT NOT NULL,
+    FOREIGN KEY (user_id) REFERNCES user(id),
+    FOREIGN KEY (book_id) REFERENCES book(id)
+)";
+if ($conn->query($inventoryTableQuery) === TRUE) {
+    echo "Inventory table created successfully<br>";
+} else {
+    echo "Error creating inventory table: " . $conn->error . "<br>";
+}
+
+
+$conn-close();
diff --git a/app/tables.php b/app/tables.php
new file mode 100644
index 0000000..876327b
--- /dev/null
+++ b/app/tables.php
@@ -0,0 +1,37 @@
+<?php
+
+class Tables
+{
+    public const AUTHOR_TABLE =
+    "CREATE TABLE IF NOT EXISTS author (
+        id INT AUTO_INCREMENT PRIMARY KEY,
+        name VARCHAR(255) NOT NULL
+    );";
+    public const USER_TABLE =
+    "CREATE TABLE IF NOT EXISTS user(
+        id INT AUTO_INCREMENT PRIMARY KEY,
+        username VARCHAR(20) NOT NULL,
+        password VARCHAR(255) NOT NULL,
+        admin BOOLEAN NOT NULL
+    );";
+
+    public const BOOK_TABLE =
+    "CREATE TABLE IF NOT EXISTS book (
+        id INT AUTO_INCREMENT PRIMARY KEY,
+        name VARCHAR(256) NOT NULL,
+        author_id INT,
+        audio_path VARCHAR(256) NOT NULL,
+        image_path VARCHAR(256) NOT NULL,
+        FOREIGN KEY(author_id) REFERENCES author(id)
+
+    );";
+
+    public const INVENTORY_TABLE =
+    "CREATE TABLE IF NOT EXISTS inventory (
+        id INT AUTO_INCREMENT PRIMARY KEY,
+        user_id INT NOT NULL,
+        book_id INT NOT NULL,
+        FOREIGN KEY (user_id) REFERNCES user(id),
+        FOREIGN KEY (book_id) REFERENCES book(id)
+    );";
+}
\ No newline at end of file
diff --git a/docker-compose.yml b/docker-compose.yml
index 7c5e0ae..dce3572 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -4,3 +4,5 @@ services:
     image: tubes-1:latest
     ports:
       - 8008:80
+
+
diff --git a/index.php b/index.php
new file mode 100644
index 0000000..be8b9ca
--- /dev/null
+++ b/index.php
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>My Docker Website</title>
+</head>
+<body>
+    <h1>Hello from HTML!</h1>
+</body>
+</html>
\ No newline at end of file
-- 
GitLab