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.
// 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 static final String jdbcString = "jdbc:sqlite:krusty.db";
// private static final String jdbcString = "jdbc:sqlite::memory:";
private static final String jdbcString = "jdbc:sqlite:krusty.db";
// Hold a single connection to the database. Note that this is
// 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 to_str = req.queryParams("to");
// Date from = null;
// Date to = null;
// Fancy functional one-liner to get the recipe if the cookie is present
if (cookie != null) {
r = Optional.ofNullable(DefaultRecipes.recipes.stream()
@ -130,28 +127,28 @@ public class Database {
try {
Statement stmt = conn.createStatement();
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
if (r.isPresent()) {
query.append(" WHERE cookie_name = '" + r.get().name + "'");
}
if (from != null && to != null) {
if (r.isPresent()) {
query.append(" AND ");
} else {
query.append(" WHERE ");
// If both from and to are present
if (from.isPresent() && to.isPresent()) {
String query_from = new SimpleDateFormat("yyyy-MM-dd").format(from.get());
String query_to = new SimpleDateFormat("yyyy-MM-dd").format(to.get());
// Super hacky, low quality code
String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
query.append(clause + "production_date BETWEEN '" + query_from + "' AND '" + query_to + "'");
}
query.append("production_date BETWEEN '" + from_str + "' AND '" + to_str + "'");
}
if (blocked.isPresent()) {
// WARNING THIS IS NOT CORRECT WARNING
if (r.isPresent() || from != null) {
query.append(" AND ");
}
if(blocked.isPresent()) {
// This again
String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
query.append(clause);
// TODO: WARNING This logic is flawed. WARNING
// Remember, status can be 'freezer', 'delivered' or 'blocked'
@ -159,8 +156,21 @@ public class Database {
}
query.append(";");
System.out.println(query.toString());
ResultSet result = stmt.executeQuery(query.toString());
// Rename the columns
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;
} catch (SQLException e) {
System.out.printf("Error executing query: \n%s", e);
@ -173,9 +183,14 @@ public class Database {
}
public String reset(Request req, Response res) {
// 1. Wipe database
// 2. Re-run migrations
// 3. Return success
try {
this.migrateScript("Migrations/create-schema.sql");
this.migrateScript("Migrations/initial-data.sql");
} catch (Exception e) {
System.out.printf("Error resetting database: \n%s", e);
res.status(500);
return "{}";
}
return "{}";
}
@ -198,13 +213,20 @@ public class Database {
return "{}";
}
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 (?, ?, ?)")) {
// System.out.println(r.get());
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
conn.setAutoCommit(false);
for(Ingredient i : r.get().ingredients) {
for (Ingredient i : r.get().ingredients) {
getRawMaterials.setString(1, i.name);
ResultSet result = getRawMaterials.executeQuery();
if (!result.next()) {
@ -229,7 +251,20 @@ public class Database {
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(3, "freezer");