Cleaning
This commit is contained in:
parent
09765fdb24
commit
72d0380023
1 changed files with 49 additions and 75 deletions
|
@ -69,6 +69,8 @@ public class Database {
|
|||
Optional<Date> from = Optional.empty(); // Holds the from date
|
||||
Optional<Date> to = Optional.empty(); // Holds the to date
|
||||
|
||||
// Parameter validation block
|
||||
{
|
||||
// First we need the cookie parameter
|
||||
String cookie = req.queryParams("cookie");
|
||||
|
||||
|
@ -115,6 +117,7 @@ public class Database {
|
|||
from = Optional.empty();
|
||||
to = Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
// This type of code is unreadable, error prone and hard to maintain.
|
||||
// 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 + "'");
|
||||
}
|
||||
|
||||
// If both from and to are present
|
||||
if (from.isPresent()) {
|
||||
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 + "'");
|
||||
}
|
||||
|
||||
// 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()) {
|
||||
// This again
|
||||
String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
|
||||
|
@ -173,10 +165,6 @@ public class Database {
|
|||
query.append("status = " + (blocked.get() ? "'blocked'" : "'freezer'"));
|
||||
}
|
||||
|
||||
query.append(";");
|
||||
|
||||
System.out.println(query.toString());
|
||||
|
||||
ResultSet result = stmt.executeQuery(query.toString());
|
||||
|
||||
// Rename the columns
|
||||
|
@ -226,13 +214,10 @@ public class Database {
|
|||
}
|
||||
|
||||
if (r.isEmpty()) {
|
||||
// Return 404
|
||||
res.status(404);
|
||||
return "{}";
|
||||
}
|
||||
|
||||
// System.out.println(r.get());
|
||||
|
||||
try (PreparedStatement getRawMaterials = conn
|
||||
.prepareStatement("SELECT * FROM raw_materials WHERE ingredient_name = ?");
|
||||
PreparedStatement decrementRawMaterials = conn.prepareStatement(
|
||||
|
@ -248,23 +233,21 @@ public class Database {
|
|||
getRawMaterials.setString(1, i.name);
|
||||
ResultSet result = getRawMaterials.executeQuery();
|
||||
if (!result.next()) {
|
||||
// Rollback transaction
|
||||
conn.rollback();
|
||||
// Return 500
|
||||
res.status(500);
|
||||
return "{}";
|
||||
}
|
||||
|
||||
int amount_per_pallet = i.amount * 54; // 54 * 100
|
||||
|
||||
// Check if we have enough raw materials
|
||||
if (result.getInt("ingredient_quantity") < i.amount) {
|
||||
// Rollback transaction
|
||||
if (result.getInt("ingredient_quantity") < amount_per_pallet) {
|
||||
conn.rollback();
|
||||
// Return 500
|
||||
res.status(500);
|
||||
return "{}";
|
||||
}
|
||||
|
||||
decrementRawMaterials.setInt(1, i.amount * 54); // 5400 / 100
|
||||
decrementRawMaterials.setInt(1, amount_per_pallet);
|
||||
decrementRawMaterials.setString(2, i.name);
|
||||
decrementRawMaterials.executeUpdate();
|
||||
}
|
||||
|
@ -273,9 +256,7 @@ public class Database {
|
|||
getCookieId.setString(1, cookie);
|
||||
ResultSet cookie_rs = getCookieId.executeQuery();
|
||||
if (!cookie_rs.next()) {
|
||||
// Rollback transaction
|
||||
conn.rollback();
|
||||
// Return 500
|
||||
res.status(500);
|
||||
return "{}";
|
||||
}
|
||||
|
@ -292,15 +273,7 @@ public class Database {
|
|||
System.out.printf("Error starting transaction: \n%s", e);
|
||||
}
|
||||
|
||||
// TODO: NOT DONE
|
||||
|
||||
// 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
|
||||
res.status(201);
|
||||
return "{}";
|
||||
}
|
||||
|
||||
|
@ -347,6 +320,7 @@ public class Database {
|
|||
|
||||
// The script location is relative to the gradle
|
||||
// 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 */
|
||||
public void migrateScript(String filename) throws IOException, SQLException {
|
||||
try (Stream<String> lines = Files.lines(Paths.get(filename))) {
|
||||
|
|
Loading…
Reference in a new issue