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

View file

@ -69,53 +69,56 @@ public class Database {
Optional<Date> from = Optional.empty(); // Holds the from date
Optional<Date> to = Optional.empty(); // Holds the to date
// First we need the cookie parameter
String cookie = req.queryParams("cookie");
// Parameter validation block
{
// First we need the cookie parameter
String cookie = req.queryParams("cookie");
// And the blocked parameter
String blocked_str = req.queryParams("blocked");
// And the blocked parameter
String blocked_str = req.queryParams("blocked");
// Then we need the date parameters
String from_str = req.queryParams("from");
String to_str = req.queryParams("to");
// Then we need the date parameters
String from_str = req.queryParams("from");
String to_str = req.queryParams("to");
// Fancy functional one-liner to get the recipe if the cookie is present
if (cookie != null) {
r = Optional.ofNullable(DefaultRecipes.recipes.stream()
.filter(recipe -> recipe.name.equals(cookie))
.findFirst().orElse(null));
}
if (blocked_str != null) {
blocked = switch (blocked_str) {
case "yes" -> Optional.of(true);
case "no" -> Optional.of(false);
default -> Optional.empty();
};
}
if(from_str != null) {
try {
from = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(from_str));
} catch (Exception e) {
from = Optional.empty();
// Fancy functional one-liner to get the recipe if the cookie is present
if (cookie != null) {
r = Optional.ofNullable(DefaultRecipes.recipes.stream()
.filter(recipe -> recipe.name.equals(cookie))
.findFirst().orElse(null));
}
}
if(to_str != null) {
try {
to = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(to_str));
} catch (Exception e) {
if (blocked_str != null) {
blocked = switch (blocked_str) {
case "yes" -> Optional.of(true);
case "no" -> Optional.of(false);
default -> Optional.empty();
};
}
if (from_str != null) {
try {
from = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(from_str));
} catch (Exception e) {
from = Optional.empty();
}
}
if (to_str != null) {
try {
to = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(to_str));
} catch (Exception e) {
to = Optional.empty();
}
}
// If the interval is negative, reset the dates
if (from.isPresent() && to.isPresent() && from.get().after(to.get())) {
from = Optional.empty();
to = Optional.empty();
}
}
// If the interval is negative, reset the dates
if(from.isPresent() && to.isPresent() && from.get().after(to.get())) {
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.
// This part almost made me write a simple query factory to handle this.
@ -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());
@ -144,7 +146,7 @@ public class Database {
query.append(clause + "production_date >= '" + query_from + "'");
}
if(to.isPresent()) {
if (to.isPresent()) {
String query_to = new SimpleDateFormat("yyyy-MM-dd").format(to.get());
// Super hacky, low quality code
@ -153,17 +155,7 @@ 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()) {
if (blocked.isPresent()) {
// This again
String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
query.append(clause);
@ -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))) {