From 9d7219040a96aff7a8d07ff7f871a023e30aa468 Mon Sep 17 00:00:00 2001
From: Agsha Athalla Nurkareem <agshaathalla@gmail.com>
Date: Thu, 16 Nov 2023 01:42:05 +0700
Subject: [PATCH] feat: soap database setup

---
 eatsnowsoap/.env                              |  3 ++
 eatsnowsoap/db/db.sql                         | 18 +++++++++
 .../java/com/eatsnowsoap/core/Database.java   | 37 +++++++++++++++++++
 3 files changed, 58 insertions(+)
 create mode 100644 eatsnowsoap/.env
 create mode 100644 eatsnowsoap/db/db.sql
 create mode 100644 eatsnowsoap/src/main/java/com/eatsnowsoap/core/Database.java

diff --git a/eatsnowsoap/.env b/eatsnowsoap/.env
new file mode 100644
index 0000000..d3b6d8a
--- /dev/null
+++ b/eatsnowsoap/.env
@@ -0,0 +1,3 @@
+DB_URL = jdbc:mysql://soap-db:3306/eatsnow-soap-service
+DB_USER = user
+DB_PASS = password
\ No newline at end of file
diff --git a/eatsnowsoap/db/db.sql b/eatsnowsoap/db/db.sql
new file mode 100644
index 0000000..5a1e35c
--- /dev/null
+++ b/eatsnowsoap/db/db.sql
@@ -0,0 +1,18 @@
+-- Create the Logging table
+CREATE TABLE logging (
+    req_desc TEXT NOT NULL,
+    ip VARCHAR(255) NOT NULL,
+    endpoint VARCHAR(255) NOT NULL,
+    timestamp DATETIME NOT NULL
+);
+
+-- Create the Review table
+CREATE TABLE review (
+    review_id INT AUTO_INCREMENT PRIMARY KEY,
+    content TEXT NOT NULL,
+    rating FLOAT NOT NULL,
+    id_user INT NOT NULL,
+    name_user VARCHAR(255) NOT NULL,
+    profile_img VARCHAR(255) NOT NULL,
+    id_restaurant INT NOT NULL
+);
\ No newline at end of file
diff --git a/eatsnowsoap/src/main/java/com/eatsnowsoap/core/Database.java b/eatsnowsoap/src/main/java/com/eatsnowsoap/core/Database.java
new file mode 100644
index 0000000..2a48a4c
--- /dev/null
+++ b/eatsnowsoap/src/main/java/com/eatsnowsoap/core/Database.java
@@ -0,0 +1,37 @@
+package com.eatsnowsoap.core;
+
+import java.sql.*;
+
+import io.github.cdimascio.dotenv.Dotenv;
+
+public class Database {
+    private Connection connection;
+    private Dotenv dotenv = Dotenv.load();
+    private String DB_URL = dotenv.get("DB_URL");
+    private String DB_USER = dotenv.get("DB_USER");
+    private String DB_PASS = dotenv.get("DB_PASS");
+
+    public Database(){
+        try{
+            this.connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
+            System.out.println("Connected to the database!");
+        } catch (Exception e){
+            e.printStackTrace();
+            System.out.println("Failed to connect to the database!");
+        }
+    }
+
+    public Connection getConnection() {
+        return this.connection;
+    }
+
+    public void closeConnection(Connection connection) {
+        if (connection != null) {
+            try {
+                connection.close();
+            } catch (SQLException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+}
\ No newline at end of file
-- 
GitLab