Compare commits

...

5 commits

Author SHA1 Message Date
Imbus
6554cb2b6f All tests passing 2024-05-05 10:52:13 +02:00
Imbus
4aeb738df3 Passing all but one test 2024-05-05 10:44:37 +02:00
Imbus
19b6b05b69 Dropping entire database every migration 2024-05-05 10:44:28 +02:00
Imbus
7d21a572af Helper tostring in recipe for easier debugging 2024-05-05 10:44:07 +02:00
Imbus
8c8584b6bf DB script fix. INT is not the same as INTEGER, apparently 2024-05-05 09:37:33 +02:00
3 changed files with 115 additions and 44 deletions

View file

@ -1,23 +1,33 @@
PRAGMA foreign_keys = OFF;
-- Drop everything...
DROP TABLE IF EXISTS pallets;
DROP TABLE IF EXISTS raw_materials_deliveries;
DROP TABLE IF EXISTS raw_materials;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS customers;
DROP TABLE IF EXISTS cookies;
--------------------------------------------
-- Recipe/Cookie related tables
--------------------------------------------
-- Holds the different types of cookies we can make.
CREATE TABLE IF NOT EXISTS cookies (
cookie_id INT PRIMARY KEY,
cookie_id INTEGER PRIMARY KEY,
cookie_name VARCHAR(50) NOT NULL UNIQUE
);
-- Our known customers, may need more fields
CREATE TABLE IF NOT EXISTS customers (
customer_id INT PRIMARY KEY,
customer_id INTEGER PRIMARY KEY,
customer_name VARCHAR(50) NOT NULL,
customer_address VARCHAR(50) NOT NULL
);
-- Orders from customers.
CREATE TABLE IF NOT EXISTS orders (
order_id INT PRIMARY KEY,
order_id INTEGER PRIMARY KEY,
customer_id INT NOT NULL,
cookie_id INT NOT NULL,
order_date DATE NOT NULL DEFAULT CURRENT_DATE CHECK (order_date >= CURRENT_DATE),
@ -31,7 +41,7 @@ CREATE TABLE IF NOT EXISTS orders (
-- Describes ingredients and stock.
CREATE TABLE IF NOT EXISTS raw_materials (
ingredient_id INT PRIMARY KEY,
ingredient_id INTEGER PRIMARY KEY,
ingredient_name VARCHAR(50) NOT NULL UNIQUE,
ingredient_quantity INT NOT NULL,
unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml'))
@ -39,7 +49,7 @@ CREATE TABLE IF NOT EXISTS raw_materials (
-- When did we get the ingredients?
CREATE TABLE IF NOT EXISTS raw_materials_deliveries (
delivery_id INT PRIMARY KEY,
delivery_id INTEGER PRIMARY KEY,
ingredient_id INT NOT NULL,
delivery_date DATE NOT NULL,
delivery_quantity INT NOT NULL,
@ -54,7 +64,7 @@ CREATE TABLE IF NOT EXISTS raw_materials_deliveries (
-- Pallets are used to store cookies for delivery
-- Order related columns are unused for now.
CREATE TABLE IF NOT EXISTS pallets (
pallet_id INT PRIMARY KEY,
pallet_id INTEGER PRIMARY KEY,
cookie_id INT NOT NULL,
-- order_id INT NOT NULL,
status VARCHAR(50) NOT NULL CHECK (status IN ('freezer', 'delivered', 'blocked')),
@ -63,3 +73,5 @@ CREATE TABLE IF NOT EXISTS pallets (
FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id)
-- FOREIGN KEY (order_id) REFERENCES orders(order_id)
);
PRAGMA foreign_keys = ON;

View file

@ -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()
@ -97,26 +94,26 @@ public class Database {
};
}
// Both of these must be present
if (from_str != null && to_str != null) {
if(from_str != null) {
try {
// Parse both in the format (2024-05-23), also called ISO 8601
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) {
// Reset the dates to empty
from = Optional.empty();
to = Optional.empty();
// We have a bad date, maybe log this somewhere
}
}
// Check so that the dates are in the correct order
if (from.isPresent() && to.isPresent() && from.get().after(to.get())) {
// We have a bad interval, perhaps set dates to empty agian?
// TODO: This obviously need louder error handling
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.
@ -130,28 +127,46 @@ 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()) {
String query_from = new SimpleDateFormat("yyyy-MM-dd").format(from.get());
query.append("production_date BETWEEN '" + from_str + "' AND '" + to_str + "'");
// Super hacky, low quality code
String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
query.append(clause + "production_date >= '" + query_from + "'");
}
if (blocked.isPresent()) {
// WARNING THIS IS NOT CORRECT WARNING
if (r.isPresent() || from != null) {
query.append(" AND ");
}
if(to.isPresent()) {
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 <= '" + 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 ";
query.append(clause);
// TODO: WARNING This logic is flawed. WARNING
// Remember, status can be 'freezer', 'delivered' or 'blocked'
@ -159,8 +174,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 +201,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 +231,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 +269,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");

View file

@ -10,6 +10,12 @@ public class Recipe {
}
public String toString() {
return name;
StringBuilder sb = new StringBuilder(name + ": ");
for (Ingredient i : ingredients) {
sb.append(i.toString());
sb.append(" ");
}
return sb.toString();
}
}