Cleaning
This commit is contained in:
parent
09765fdb24
commit
72d0380023
1 changed files with 49 additions and 75 deletions
|
@ -69,53 +69,56 @@ 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
|
||||||
|
|
||||||
// First we need the cookie parameter
|
// Parameter validation block
|
||||||
String cookie = req.queryParams("cookie");
|
{
|
||||||
|
// First we need the cookie parameter
|
||||||
|
String cookie = req.queryParams("cookie");
|
||||||
|
|
||||||
// And the blocked parameter
|
// And the blocked parameter
|
||||||
String blocked_str = req.queryParams("blocked");
|
String blocked_str = req.queryParams("blocked");
|
||||||
|
|
||||||
// Then we need the date parameters
|
// Then we need the date parameters
|
||||||
String from_str = req.queryParams("from");
|
String from_str = req.queryParams("from");
|
||||||
String to_str = req.queryParams("to");
|
String to_str = req.queryParams("to");
|
||||||
|
|
||||||
// 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()
|
||||||
.filter(recipe -> recipe.name.equals(cookie))
|
.filter(recipe -> recipe.name.equals(cookie))
|
||||||
.findFirst().orElse(null));
|
.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();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if(to_str != null) {
|
if (blocked_str != null) {
|
||||||
try {
|
blocked = switch (blocked_str) {
|
||||||
to = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(to_str));
|
case "yes" -> Optional.of(true);
|
||||||
} catch (Exception e) {
|
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();
|
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.
|
// 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.
|
||||||
// This part almost made me write a simple query factory to handle this.
|
// 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 + "'");
|
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());
|
||||||
|
|
||||||
|
@ -144,7 +146,7 @@ public class Database {
|
||||||
query.append(clause + "production_date >= '" + query_from + "'");
|
query.append(clause + "production_date >= '" + query_from + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(to.isPresent()) {
|
if (to.isPresent()) {
|
||||||
String query_to = new SimpleDateFormat("yyyy-MM-dd").format(to.get());
|
String query_to = new SimpleDateFormat("yyyy-MM-dd").format(to.get());
|
||||||
|
|
||||||
// Super hacky, low quality code
|
// Super hacky, low quality code
|
||||||
|
@ -153,17 +155,7 @@ public class Database {
|
||||||
query.append(clause + "production_date <= '" + query_to + "'");
|
query.append(clause + "production_date <= '" + query_to + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (from.isPresent() && to.isPresent()) {
|
if (blocked.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
|
// This again
|
||||||
String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
|
String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
|
||||||
query.append(clause);
|
query.append(clause);
|
||||||
|
@ -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))) {
|
||||||
|
|
Loading…
Reference in a new issue