This commit is contained in:
Imbus 2024-05-05 11:47:26 +02:00
parent 09765fdb24
commit 72d0380023

View file

@ -69,6 +69,8 @@ public class Database {
Optional<Date> from = Optional.empty(); // Holds the from date Optional<Date> from = Optional.empty(); // Holds the from date
Optional<Date> to = Optional.empty(); // Holds the to date Optional<Date> to = Optional.empty(); // Holds the to date
// Parameter validation block
{
// First we need the cookie parameter // First we need the cookie parameter
String cookie = req.queryParams("cookie"); String cookie = req.queryParams("cookie");
@ -115,6 +117,7 @@ public class Database {
from = Optional.empty(); from = Optional.empty();
to = Optional.empty(); to = Optional.empty();
} }
}
// This type of code is unreadable, error prone and hard to maintain. // This type of code is unreadable, error prone and hard to maintain.
// The fact that im responsible for this code makes my soul hurt. // The fact that im responsible for this code makes my soul hurt.
@ -134,7 +137,6 @@ public class Database {
query.append(" WHERE cookie_name = '" + r.get().name + "'"); query.append(" WHERE cookie_name = '" + r.get().name + "'");
} }
// If both from and to are present
if (from.isPresent()) { if (from.isPresent()) {
String query_from = new SimpleDateFormat("yyyy-MM-dd").format(from.get()); String query_from = new SimpleDateFormat("yyyy-MM-dd").format(from.get());
@ -153,16 +155,6 @@ public class Database {
query.append(clause + "production_date <= '" + query_to + "'"); query.append(clause + "production_date <= '" + query_to + "'");
} }
// 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 + "'");
// }
if (blocked.isPresent()) { if (blocked.isPresent()) {
// This again // This again
String clause = query.toString().contains("WHERE") ? " AND " : " WHERE "; String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
@ -173,10 +165,6 @@ public class Database {
query.append("status = " + (blocked.get() ? "'blocked'" : "'freezer'")); query.append("status = " + (blocked.get() ? "'blocked'" : "'freezer'"));
} }
query.append(";");
System.out.println(query.toString());
ResultSet result = stmt.executeQuery(query.toString()); ResultSet result = stmt.executeQuery(query.toString());
// Rename the columns // Rename the columns
@ -226,13 +214,10 @@ public class Database {
} }
if (r.isEmpty()) { if (r.isEmpty()) {
// Return 404
res.status(404); res.status(404);
return "{}"; return "{}";
} }
// System.out.println(r.get());
try (PreparedStatement getRawMaterials = conn try (PreparedStatement getRawMaterials = conn
.prepareStatement("SELECT * FROM raw_materials WHERE ingredient_name = ?"); .prepareStatement("SELECT * FROM raw_materials WHERE ingredient_name = ?");
PreparedStatement decrementRawMaterials = conn.prepareStatement( PreparedStatement decrementRawMaterials = conn.prepareStatement(
@ -248,23 +233,21 @@ public class Database {
getRawMaterials.setString(1, i.name); getRawMaterials.setString(1, i.name);
ResultSet result = getRawMaterials.executeQuery(); ResultSet result = getRawMaterials.executeQuery();
if (!result.next()) { if (!result.next()) {
// Rollback transaction
conn.rollback(); conn.rollback();
// Return 500
res.status(500); res.status(500);
return "{}"; return "{}";
} }
int amount_per_pallet = i.amount * 54; // 54 * 100
// Check if we have enough raw materials // Check if we have enough raw materials
if (result.getInt("ingredient_quantity") < i.amount) { if (result.getInt("ingredient_quantity") < amount_per_pallet) {
// Rollback transaction
conn.rollback(); conn.rollback();
// Return 500
res.status(500); res.status(500);
return "{}"; return "{}";
} }
decrementRawMaterials.setInt(1, i.amount * 54); // 5400 / 100 decrementRawMaterials.setInt(1, amount_per_pallet);
decrementRawMaterials.setString(2, i.name); decrementRawMaterials.setString(2, i.name);
decrementRawMaterials.executeUpdate(); decrementRawMaterials.executeUpdate();
} }
@ -273,9 +256,7 @@ public class Database {
getCookieId.setString(1, cookie); getCookieId.setString(1, cookie);
ResultSet cookie_rs = getCookieId.executeQuery(); ResultSet cookie_rs = getCookieId.executeQuery();
if (!cookie_rs.next()) { if (!cookie_rs.next()) {
// Rollback transaction
conn.rollback(); conn.rollback();
// Return 500
res.status(500); res.status(500);
return "{}"; return "{}";
} }
@ -292,15 +273,7 @@ public class Database {
System.out.printf("Error starting transaction: \n%s", e); System.out.printf("Error starting transaction: \n%s", e);
} }
// TODO: NOT DONE res.status(201);
// 1. Get query param
// 2. Check if cookie exists (is in DefaultRecipes)
// 3. Start transaction
// 3. Check with db if raw materials are available -> decrement if so
// 4. Insert into pallets
// 5. Commit transaction
// 6. Return pallet id
return "{}"; return "{}";
} }
@ -347,6 +320,7 @@ public class Database {
// The script location is relative to the gradle // The script location is relative to the gradle
// build script ("build.gradle.kts", in this case). // build script ("build.gradle.kts", in this case).
// Assumes every statement ends with a semicolon. (notably broken for triggers)
/** Reads an sql script into the database */ /** Reads an sql script into the database */
public void migrateScript(String filename) throws IOException, SQLException { public void migrateScript(String filename) throws IOException, SQLException {
try (Stream<String> lines = Files.lines(Paths.get(filename))) { try (Stream<String> lines = Files.lines(Paths.get(filename))) {