Passing all but one test

This commit is contained in:
Imbus 2024-05-05 10:44:37 +02:00
parent 19b6b05b69
commit 4aeb738df3

View file

@ -27,8 +27,8 @@ public class Database {
// "jdbc:sqlite:<filename>.sqlite3" to use a file-based database instead. // "jdbc:sqlite:<filename>.sqlite3" to use a file-based database instead.
// Nore that ":memory:" is an **SQLite specific** magic string that tells the // Nore that ":memory:" is an **SQLite specific** magic string that tells the
// underlying SQLite engine to store the database in memory. // underlying SQLite engine to store the database in memory.
private static final String jdbcString = "jdbc:sqlite::memory:"; // private static final String jdbcString = "jdbc:sqlite::memory:";
// private static final String jdbcString = "jdbc:sqlite:krusty.db"; private static final String jdbcString = "jdbc:sqlite:krusty.db";
// Hold a single connection to the database. Note that this is // Hold a single connection to the database. Note that this is
// not a pool, so this is not thread-safe nor efficient. // not a pool, so this is not thread-safe nor efficient.
@ -79,9 +79,6 @@ public class Database {
String from_str = req.queryParams("from"); String from_str = req.queryParams("from");
String to_str = req.queryParams("to"); String to_str = req.queryParams("to");
// Date from = null;
// Date to = null;
// Fancy functional one-liner to get the recipe if the cookie is present // Fancy functional one-liner to get the recipe if the cookie is present
if (cookie != null) { if (cookie != null) {
r = Optional.ofNullable(DefaultRecipes.recipes.stream() r = Optional.ofNullable(DefaultRecipes.recipes.stream()
@ -130,28 +127,28 @@ public class Database {
try { try {
Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement();
StringBuilder query = new StringBuilder( StringBuilder query = new StringBuilder(
"SELECT * FROM pallets JOIN cookies ON pallets.cookie_id = cookies.cookie_id"); "SELECT cookie_name, status FROM pallets JOIN cookies ON pallets.cookie_id = cookies.cookie_id");
// r is validated here // r is validated here
if (r.isPresent()) { if (r.isPresent()) {
query.append(" WHERE cookie_name = '" + r.get().name + "'"); query.append(" WHERE cookie_name = '" + r.get().name + "'");
} }
if (from != null && to != null) { // If both from and to are present
if (r.isPresent()) { if (from.isPresent() && to.isPresent()) {
query.append(" AND "); String query_from = new SimpleDateFormat("yyyy-MM-dd").format(from.get());
} else { String query_to = new SimpleDateFormat("yyyy-MM-dd").format(to.get());
query.append(" WHERE ");
}
query.append("production_date BETWEEN '" + from_str + "' AND '" + to_str + "'"); // Super hacky, low quality code
String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
query.append(clause + "production_date BETWEEN '" + query_from + "' AND '" + query_to + "'");
} }
if (blocked.isPresent()) { if(blocked.isPresent()) {
// WARNING THIS IS NOT CORRECT WARNING // This again
if (r.isPresent() || from != null) { String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
query.append(" AND "); query.append(clause);
}
// TODO: WARNING This logic is flawed. WARNING // TODO: WARNING This logic is flawed. WARNING
// Remember, status can be 'freezer', 'delivered' or 'blocked' // Remember, status can be 'freezer', 'delivered' or 'blocked'
@ -159,8 +156,21 @@ public class Database {
} }
query.append(";"); query.append(";");
System.out.println(query.toString());
ResultSet result = stmt.executeQuery(query.toString()); ResultSet result = stmt.executeQuery(query.toString());
// Rename the columns
String jsonResult = Jsonizer.toJson(result, "pallets"); String jsonResult = Jsonizer.toJson(result, "pallets");
// Some carmack level code, as usual
jsonResult = jsonResult.replaceAll("cookie_name", "cookie");
jsonResult = jsonResult.replaceAll("freezer", "no");
jsonResult = jsonResult.replaceAll("delivered", "no");
jsonResult = jsonResult.replaceAll("blocked", "yes");
jsonResult = jsonResult.replaceAll("status", "blocked");
return jsonResult; return jsonResult;
} catch (SQLException e) { } catch (SQLException e) {
System.out.printf("Error executing query: \n%s", e); System.out.printf("Error executing query: \n%s", e);
@ -173,9 +183,14 @@ public class Database {
} }
public String reset(Request req, Response res) { public String reset(Request req, Response res) {
// 1. Wipe database try {
// 2. Re-run migrations this.migrateScript("Migrations/create-schema.sql");
// 3. Return success this.migrateScript("Migrations/initial-data.sql");
} catch (Exception e) {
System.out.printf("Error resetting database: \n%s", e);
res.status(500);
return "{}";
}
return "{}"; return "{}";
} }
@ -198,13 +213,20 @@ public class Database {
return "{}"; return "{}";
} }
try (PreparedStatement getRawMaterials = conn.prepareStatement("SELECT * FROM raw_materials WHERE ingredient_name = ?"); // System.out.println(r.get());
PreparedStatement decrementRawMaterials = conn.prepareStatement("UPDATE raw_materials SET ingredient_quantity = ingredient_quantity - ? WHERE ingredient_name = ?");
PreparedStatement insertPallet = conn.prepareStatement("INSERT INTO pallets (cookie_id, production_date, status) VALUES (?, ?, ?)")) { try (PreparedStatement getRawMaterials = conn
.prepareStatement("SELECT * FROM raw_materials WHERE ingredient_name = ?");
PreparedStatement decrementRawMaterials = conn.prepareStatement(
"UPDATE raw_materials SET ingredient_quantity = ingredient_quantity - ? WHERE ingredient_name = ?");
PreparedStatement insertPallet = conn.prepareStatement(
"INSERT INTO pallets (cookie_id, production_date, status) VALUES (?, ?, ?)");
PreparedStatement getCookieId = conn
.prepareStatement("SELECT cookie_id FROM cookies WHERE cookie_name = ?")) {
// Start transaction // Start transaction
conn.setAutoCommit(false); conn.setAutoCommit(false);
for(Ingredient i : r.get().ingredients) { for (Ingredient i : r.get().ingredients) {
getRawMaterials.setString(1, i.name); getRawMaterials.setString(1, i.name);
ResultSet result = getRawMaterials.executeQuery(); ResultSet result = getRawMaterials.executeQuery();
if (!result.next()) { if (!result.next()) {
@ -229,7 +251,20 @@ public class Database {
decrementRawMaterials.executeUpdate(); decrementRawMaterials.executeUpdate();
} }
insertPallet.setInt(1, 1); // Fish out the cookie id
getCookieId.setString(1, cookie);
ResultSet cookie_rs = getCookieId.executeQuery();
if (!cookie_rs.next()) {
// Rollback transaction
conn.rollback();
// Return 500
res.status(500);
return "{}";
}
int cookie_id = cookie_rs.getInt("cookie_id");
insertPallet.setInt(1, cookie_id);
insertPallet.setString(2, new SimpleDateFormat("yyyy-MM-dd").format(new Date())); insertPallet.setString(2, new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
insertPallet.setString(3, "freezer"); insertPallet.setString(3, "freezer");