From 8fc32103452c420f8c6989ab30c7e30572782026 Mon Sep 17 00:00:00 2001
From: Your Name <youremail@example.com>
Date: Sun, 8 Oct 2023 20:20:41 +0700
Subject: [PATCH] seeding with docker

---
 .gitignore                               | 10 +++++++++-
 Dockerfile                               |  7 +------
 app/controllers/BookController.php       |  3 ---
 app/main/SessionManager.php              | 14 ++++++++++++++
 app/storage/audio/.gitkeep               |  0
 scripts/build-image.sh => build-image.sh |  0
 clean-run.sh                             |  3 +++
 db-init/seeding/all.sh                   | 22 ++++++++++++++++++----
 db-init/seeding/author.py                | 10 +++++-----
 db-init/seeding/book.py                  | 14 +++++++-------
 db-init/seeding/genre.py                 | 10 +++++-----
 db-init/seeding/user.py                  | 10 +++++-----
 docker-compose.yml                       | 19 +++++++++++++++----
 reset-db.sh                              | 15 +++++++++++++++
 14 files changed, 97 insertions(+), 40 deletions(-)
 create mode 100644 app/main/SessionManager.php
 create mode 100644 app/storage/audio/.gitkeep
 rename scripts/build-image.sh => build-image.sh (100%)
 create mode 100644 clean-run.sh
 mode change 100644 => 100755 db-init/seeding/all.sh
 create mode 100644 reset-db.sh

diff --git a/.gitignore b/.gitignore
index 92fe3d6..fd438a1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,9 @@
-/mysql
\ No newline at end of file
+/mysql
+/db-init/seeding/init.flag
+
+/app/storage/image/book_cover/*
+!/app/storage/image/book_cover/.gitkeep
+/app/storage/image/profile_pic/*
+!/app/storage/image/profile_pic/.gitkeep
+/app/storage/audio/*
+!/app/storage/audio/.gitkeep
\ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
index fd71aac..42439ca 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -4,9 +4,4 @@ WORKDIR /var/www/html
 # PHP extension and Apache configuration
 RUN docker-php-ext-install mysqli
 RUN a2enmod rewrite
-RUN apt-get -y update && apt-get -y upgrade && apt-get install -y ffmpeg
-
-# Data seeding with Python script
-# ...
-
-EXPOSE 80
\ No newline at end of file
+RUN apt-get -y update && apt-get -y upgrade && apt-get install -y ffmpeg
\ No newline at end of file
diff --git a/app/controllers/BookController.php b/app/controllers/BookController.php
index abbf3ce..cd92b5b 100644
--- a/app/controllers/BookController.php
+++ b/app/controllers/BookController.php
@@ -49,7 +49,6 @@ class BookController extends Controller implements ControllerInterface{
                     $audioFile = $_FILES['audio']['tmp_name'];
                     $duration = (int) $fileHandler->getAudioDuration($audioFile);
 
-
                     $uploadedAudio = $fileHandler->saveAudioTo($audioFile, $_POST['title'], AUDIOBOOK_PATH);
                     $uploadedImage = $fileHandler->saveImageTo($imageFile, $_POST['title'], BOOK_COVER_PATH);
 
@@ -71,8 +70,6 @@ class BookController extends Controller implements ControllerInterface{
                         $title, $year, $summary, $price, $duration, $lang,
                         $uploadedAudio, $uploadedImage, $authors, $genres
                     );
-
-                    error_log("Book ID: $bookId\n\n");
                 
                     // header("Location: /public/song/detail/$bookId", true, 301);
                     exit;
diff --git a/app/main/SessionManager.php b/app/main/SessionManager.php
new file mode 100644
index 0000000..361adae
--- /dev/null
+++ b/app/main/SessionManager.php
@@ -0,0 +1,14 @@
+<?
+class SessionManager {
+    private static $instance = null;
+
+    private function __construct() {}
+
+    public static function getInstance() {
+        if (self::$instance == null) {
+            self::$instance = new SessionManager();
+        }
+        return self::$instance;
+    }
+}
+?>
\ No newline at end of file
diff --git a/app/storage/audio/.gitkeep b/app/storage/audio/.gitkeep
new file mode 100644
index 0000000..e69de29
diff --git a/scripts/build-image.sh b/build-image.sh
similarity index 100%
rename from scripts/build-image.sh
rename to build-image.sh
diff --git a/clean-run.sh b/clean-run.sh
new file mode 100644
index 0000000..99c7570
--- /dev/null
+++ b/clean-run.sh
@@ -0,0 +1,3 @@
+sh reset-db.sh
+sh build-images.sh
+docker-compose up
\ No newline at end of file
diff --git a/db-init/seeding/all.sh b/db-init/seeding/all.sh
old mode 100644
new mode 100755
index 04f5ba9..0694db8
--- a/db-init/seeding/all.sh
+++ b/db-init/seeding/all.sh
@@ -1,4 +1,18 @@
-python3 user.py
-python3 genre.py
-python3 author.py
-python3 book.py
\ No newline at end of file
+apt-get update
+apt-get install -y python3 python3-pip
+
+pip3 install requests Faker
+
+# check if seeding has already been done
+if [ ! -f init.flag ]; then
+    echo "Seeding database... ###########################################"
+    python3 user.py
+    python3 genre.py
+    python3 author.py
+    python3 book.py
+    touch init.flag
+else
+    echo "Action already initiated. Skipping..."
+
+echo "Seeding complete."
+fi
\ No newline at end of file
diff --git a/db-init/seeding/author.py b/db-init/seeding/author.py
index d53e67e..19ff43c 100644
--- a/db-init/seeding/author.py
+++ b/db-init/seeding/author.py
@@ -1,13 +1,13 @@
 import requests
 import random
 
-api_url = 'http://localhost:8000/author/add'
+api_url = 'http://web/author/add'
 
 print ("""
-    Seeding Authors
-    ====================================
-       
-    ...
+Seeding Authors
+====================================
+    
+...
 """)
 
 # Authors Generator
diff --git a/db-init/seeding/book.py b/db-init/seeding/book.py
index 456164e..d551699 100644
--- a/db-init/seeding/book.py
+++ b/db-init/seeding/book.py
@@ -3,17 +3,17 @@ import random
 import os
 
 print ("""
-    Seeding Books
-    ====================================
-       
-    ...
+Seeding Books
+====================================
+    
+...
 """)
 
 
 NUM_OF_AUTHORS = 15
 NUM_OF_GENRES = 15
 
-post_api = 'http://localhost:8000/book/add'
+post_api = 'http://web/book/add'
 
 
 books = []
@@ -76,8 +76,8 @@ for b in books:
         'title': title,
         'year': year,
         'summary': b['description'],
-        'author': authors,
-        'genre': genres,
+        'authors[]': authors,
+        'genres[]': genres,
         'price': price,
         'lang': 'English',
     }
diff --git a/db-init/seeding/genre.py b/db-init/seeding/genre.py
index 04d5ca3..b477fbd 100644
--- a/db-init/seeding/genre.py
+++ b/db-init/seeding/genre.py
@@ -1,10 +1,10 @@
 import requests
 
 print ("""
-    Seeding Genres
-    ====================================
-       
-    ...
+Seeding Genres
+====================================
+    
+...
 """)
 
 BOOK_GENRES = [
@@ -25,7 +25,7 @@ BOOK_GENRES = [
     "Classics"
 ]
 
-api_url = 'http://localhost:8000/genre/add'
+api_url = 'http://web/genre/add'
 
 for genre in BOOK_GENRES:
     data = {
diff --git a/db-init/seeding/user.py b/db-init/seeding/user.py
index 0bf8a74..df31fb5 100644
--- a/db-init/seeding/user.py
+++ b/db-init/seeding/user.py
@@ -5,13 +5,13 @@ import os
 
 fake = Faker()
 
-api_url = 'http://localhost:8000/user/register'
+api_url = 'http://web/user/register'
 
 print ("""
-    Seeding Users
-    ====================================
-       
-    ...
+Seeding Users
+====================================
+    
+...
 """)
 
 users = []
diff --git a/docker-compose.yml b/docker-compose.yml
index bf9f9aa..cc30bca 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -3,10 +3,7 @@ services:
   web:
     container_name: tubes-1
     user: "1000:1000"
-    build: 
-        context: .
-        dockerfile: Dockerfile
-    # image: tubes-1:latest
+    image: tubes-1:latest
     ports:
       - 8000:80
     depends_on:
@@ -15,6 +12,8 @@ services:
       - ./app:/var/www/html/
     networks:
       - wbd-network
+    expose:
+      - '80'
   db:
     container_name: wbd-db
     image: mysql
@@ -33,5 +32,17 @@ services:
       - '3306'
     networks:
       - wbd-network
+  data-seeding:
+    container_name: python-seeding
+    image: python:3.7
+    volumes:
+      - ./db-init/seeding/:/app
+    working_dir: /app
+    command: ["sh", "all.sh"]
+    depends_on:
+      - web
+      - db
+    networks:
+      - wbd-network
 networks:
   wbd-network:
\ No newline at end of file
diff --git a/reset-db.sh b/reset-db.sh
new file mode 100644
index 0000000..f99d2ab
--- /dev/null
+++ b/reset-db.sh
@@ -0,0 +1,15 @@
+sudo chown 1000 ./mysql/*
+rm -rf ./mysql/*
+touch ./mysql/.gitkeep
+
+sudo chown 1000 ./db-init/seeding/init.flag
+rm -rf ./db-init/seeding/init.flag
+
+rm -rf ./app/storage/image/book_cover/*
+touch ./app/storage/image/book_cover/.gitkeep
+
+rm -rf ./app/storage/image/profile_pic/*
+touch ./app/storage/image/profile_pic/.gitkeep
+
+rm -rf ./app/storage/audio/*
+touch ./app/storage/audio/.gitkeep
\ No newline at end of file
-- 
GitLab