diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 4dd2e1b..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "app/CrustyCookies"] - path = app/CrustyCookies - url = https://git.silversoft.se/Imbus/CrustyCookies.git diff --git a/app/CrustyCookies b/app/CrustyCookies deleted file mode 160000 index be904a7..0000000 --- a/app/CrustyCookies +++ /dev/null @@ -1 +0,0 @@ -Subproject commit be904a7ab3925f7bbf3b54126e25480476ae49e8 diff --git a/app/src/main/java/krusty/Database.java b/app/src/main/java/krusty/Database.java index 53d2cd3..ce6c69d 100644 --- a/app/src/main/java/krusty/Database.java +++ b/app/src/main/java/krusty/Database.java @@ -3,27 +3,32 @@ package krusty; import spark.Request; import spark.Response; -// Likely dependencies for db operations import java.sql.Connection; import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; - -// Likely dependencies for general operations -import java.io.File; -import java.util.StringJoiner; -import java.io.IOException; -import java.io.FileNotFoundException; -import java.util.Scanner; public class Database { - // Here, we use an in-memory database. This string could be changed to - // "jdbc:sqlite:.sqlite3" to use a file-based database instead. - // Nore that ":memory:" is an **SQLite specific** magic string that tells the - // underlying SQLite engine to store the database in memory. - private static final String jdbcString = "jdbc:sqlite::memory:"; - private Connection conn = null; + private static final String jdbcString = "jdbc:sqlite:krusty.sqlite3"; + + public void connect() { + Connection conn = null; + + try { + conn = DriverManager.getConnection(jdbcString); + System.out.println("Connection to SQLite has been established."); + } catch (Exception e) { + System.out.println(e.getMessage()); + } finally { + try { + if (conn != null) { + conn.close(); + } + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + } + + // TODO: Implement and change output in all methods below! public String getCustomers(Request req, Response res) { return "{}"; @@ -52,52 +57,4 @@ public class Database { public String createPallet(Request req, Response res) { return "{}"; } - - /** Connects to the database using the configured jdbcString. */ - public void connect() { - try { - conn = DriverManager.getConnection(jdbcString); - } catch (Exception e) { - System.out.println(e.getMessage()); - } finally { - System.out.println("Connection to SQLite has been established."); - } - } - - // The script location is relative to the gradle - // build script ("build.gradle.kts", in this case). - /** Reads an sql script into the database */ - public void migrateScript(String filename) { - // Open the file - StringBuilder script = new StringBuilder(); - try { - File myObj = new File(filename); - Scanner myReader = new Scanner(myObj); - - while (myReader.hasNextLine()) { - String data = myReader.nextLine(); - script.append(data); - } - myReader.close(); - } catch (FileNotFoundException e) { - System.out.println("Migration script not found..."); - e.printStackTrace(); - } finally { - System.out.println("Migration script read successfully..."); - } - - // Execute the script - try { - conn.setAutoCommit(false); - Statement stmt = conn.createStatement(); - stmt.execute(script.toString()); - conn.commit(); - conn.setAutoCommit(true); - } catch (SQLException e) { - System.out.println("Error executing migration script..."); - e.printStackTrace(); - } finally { - System.out.println("Migration script executed successfully..."); - } - } } diff --git a/app/src/main/java/krusty/ServerMain.java b/app/src/main/java/krusty/ServerMain.java index e310558..404ae58 100644 --- a/app/src/main/java/krusty/ServerMain.java +++ b/app/src/main/java/krusty/ServerMain.java @@ -18,9 +18,6 @@ public class ServerMain { db = new Database(); db.connect(); - // Here, we can migrate an arbitrary number of SQL scripts. - db.migrateScript("CrustyCookies/Migrations/0010_users.sql"); - port(PORT); enableCORS();