Compare commits
	
		
			No commits in common. "6554cb2b6f9e440734c50a0d0721d64d4ac803df" and "9843a078ea38912660552b090a34a11dd920c634" have entirely different histories.
		
	
	
		
			6554cb2b6f
			...
			9843a078ea
		
	
		
					 3 changed files with 45 additions and 116 deletions
				
			
		| 
						 | 
					@ -1,33 +1,23 @@
 | 
				
			||||||
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
 | 
					-- Recipe/Cookie related tables
 | 
				
			||||||
--------------------------------------------
 | 
					--------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- Holds the different types of cookies we can make.
 | 
					-- Holds the different types of cookies we can make.
 | 
				
			||||||
CREATE TABLE IF NOT EXISTS cookies (
 | 
					CREATE TABLE IF NOT EXISTS cookies (
 | 
				
			||||||
    cookie_id INTEGER PRIMARY KEY,
 | 
					    cookie_id INT PRIMARY KEY,
 | 
				
			||||||
    cookie_name VARCHAR(50) NOT NULL UNIQUE
 | 
					    cookie_name VARCHAR(50) NOT NULL UNIQUE
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- Our known customers, may need more fields
 | 
					-- Our known customers, may need more fields
 | 
				
			||||||
CREATE TABLE IF NOT EXISTS customers (
 | 
					CREATE TABLE IF NOT EXISTS customers (
 | 
				
			||||||
    customer_id INTEGER PRIMARY KEY,
 | 
					    customer_id INT PRIMARY KEY,
 | 
				
			||||||
    customer_name VARCHAR(50) NOT NULL,
 | 
					    customer_name VARCHAR(50) NOT NULL,
 | 
				
			||||||
    customer_address VARCHAR(50) NOT NULL
 | 
					    customer_address VARCHAR(50) NOT NULL
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- Orders from customers.
 | 
					-- Orders from customers.
 | 
				
			||||||
CREATE TABLE IF NOT EXISTS orders (
 | 
					CREATE TABLE IF NOT EXISTS orders (
 | 
				
			||||||
    order_id INTEGER PRIMARY KEY,
 | 
					    order_id INT PRIMARY KEY,
 | 
				
			||||||
    customer_id INT NOT NULL,
 | 
					    customer_id INT NOT NULL,
 | 
				
			||||||
    cookie_id INT NOT NULL,
 | 
					    cookie_id INT NOT NULL,
 | 
				
			||||||
    order_date DATE NOT NULL DEFAULT CURRENT_DATE CHECK (order_date >= CURRENT_DATE),
 | 
					    order_date DATE NOT NULL DEFAULT CURRENT_DATE CHECK (order_date >= CURRENT_DATE),
 | 
				
			||||||
| 
						 | 
					@ -41,7 +31,7 @@ CREATE TABLE IF NOT EXISTS orders (
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- Describes ingredients and stock.
 | 
					-- Describes ingredients and stock.
 | 
				
			||||||
CREATE TABLE IF NOT EXISTS raw_materials (
 | 
					CREATE TABLE IF NOT EXISTS raw_materials (
 | 
				
			||||||
    ingredient_id INTEGER PRIMARY KEY,
 | 
					    ingredient_id INT PRIMARY KEY,
 | 
				
			||||||
    ingredient_name VARCHAR(50) NOT NULL UNIQUE,
 | 
					    ingredient_name VARCHAR(50) NOT NULL UNIQUE,
 | 
				
			||||||
    ingredient_quantity INT NOT NULL,
 | 
					    ingredient_quantity INT NOT NULL,
 | 
				
			||||||
    unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml'))
 | 
					    unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml'))
 | 
				
			||||||
| 
						 | 
					@ -49,7 +39,7 @@ CREATE TABLE IF NOT EXISTS raw_materials (
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- When did we get the ingredients?
 | 
					-- When did we get the ingredients?
 | 
				
			||||||
CREATE TABLE IF NOT EXISTS raw_materials_deliveries (
 | 
					CREATE TABLE IF NOT EXISTS raw_materials_deliveries (
 | 
				
			||||||
    delivery_id INTEGER PRIMARY KEY,
 | 
					    delivery_id INT PRIMARY KEY,
 | 
				
			||||||
    ingredient_id INT NOT NULL,
 | 
					    ingredient_id INT NOT NULL,
 | 
				
			||||||
    delivery_date DATE NOT NULL,
 | 
					    delivery_date DATE NOT NULL,
 | 
				
			||||||
    delivery_quantity INT NOT NULL,
 | 
					    delivery_quantity INT NOT NULL,
 | 
				
			||||||
| 
						 | 
					@ -64,7 +54,7 @@ CREATE TABLE IF NOT EXISTS raw_materials_deliveries (
 | 
				
			||||||
-- Pallets are used to store cookies for delivery
 | 
					-- Pallets are used to store cookies for delivery
 | 
				
			||||||
-- Order related columns are unused for now.
 | 
					-- Order related columns are unused for now.
 | 
				
			||||||
CREATE TABLE IF NOT EXISTS pallets (
 | 
					CREATE TABLE IF NOT EXISTS pallets (
 | 
				
			||||||
    pallet_id INTEGER PRIMARY KEY,
 | 
					    pallet_id INT PRIMARY KEY,
 | 
				
			||||||
    cookie_id INT NOT NULL,
 | 
					    cookie_id INT NOT NULL,
 | 
				
			||||||
    -- order_id INT NOT NULL,
 | 
					    -- order_id INT NOT NULL,
 | 
				
			||||||
    status VARCHAR(50) NOT NULL CHECK (status IN ('freezer', 'delivered', 'blocked')),
 | 
					    status VARCHAR(50) NOT NULL CHECK (status IN ('freezer', 'delivered', 'blocked')),
 | 
				
			||||||
| 
						 | 
					@ -73,5 +63,3 @@ CREATE TABLE IF NOT EXISTS pallets (
 | 
				
			||||||
    FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id)
 | 
					    FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id)
 | 
				
			||||||
    -- FOREIGN KEY (order_id) REFERENCES orders(order_id)
 | 
					    -- FOREIGN KEY (order_id) REFERENCES orders(order_id)
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
 | 
					 | 
				
			||||||
PRAGMA foreign_keys = ON;
 | 
					 | 
				
			||||||
| 
						 | 
					@ -79,6 +79,9 @@ public class Database {
 | 
				
			||||||
		String from_str = req.queryParams("from");
 | 
							String from_str = req.queryParams("from");
 | 
				
			||||||
		String to_str = req.queryParams("to");
 | 
							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
 | 
							// 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()
 | 
				
			||||||
| 
						 | 
					@ -94,28 +97,28 @@ public class Database {
 | 
				
			||||||
			};
 | 
								};
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if(from_str != null) {
 | 
							// Both of these must be present
 | 
				
			||||||
 | 
							if (from_str != null && to_str != null) {
 | 
				
			||||||
			try {
 | 
								try {
 | 
				
			||||||
 | 
									// Parse both in the format (2024-05-23), also called ISO 8601
 | 
				
			||||||
				from = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(from_str));
 | 
									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));
 | 
									to = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(to_str));
 | 
				
			||||||
			} catch (Exception e) {
 | 
								} 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();
 | 
									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.
 | 
				
			||||||
| 
						 | 
					@ -127,46 +130,28 @@ public class Database {
 | 
				
			||||||
		try {
 | 
							try {
 | 
				
			||||||
			Statement stmt = conn.createStatement();
 | 
								Statement stmt = conn.createStatement();
 | 
				
			||||||
			StringBuilder query = new StringBuilder(
 | 
								StringBuilder query = new StringBuilder(
 | 
				
			||||||
					"SELECT cookie_name, status FROM pallets JOIN cookies ON pallets.cookie_id = cookies.cookie_id");
 | 
										"SELECT * FROM pallets JOIN cookies ON pallets.cookie_id = cookies.cookie_id");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// r is validated here
 | 
								// r is validated here
 | 
				
			||||||
			if (r.isPresent()) {
 | 
								if (r.isPresent()) {
 | 
				
			||||||
				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 != null && to != null) {
 | 
				
			||||||
			if (from.isPresent()) {
 | 
									if (r.isPresent()) {
 | 
				
			||||||
				String query_from = new SimpleDateFormat("yyyy-MM-dd").format(from.get());
 | 
										query.append(" AND ");
 | 
				
			||||||
 | 
									} else {
 | 
				
			||||||
 | 
										query.append(" WHERE ");
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				// Super hacky, low quality code
 | 
									query.append("production_date BETWEEN '" + from_str + "' AND '" + to_str + "'");
 | 
				
			||||||
				String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
				query.append(clause + "production_date >= '" + query_from + "'");
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if(to.isPresent()) {
 | 
								if (blocked.isPresent()) {
 | 
				
			||||||
				String query_to = new SimpleDateFormat("yyyy-MM-dd").format(to.get());
 | 
									// WARNING THIS IS NOT CORRECT WARNING
 | 
				
			||||||
 | 
									if (r.isPresent() || from != null) {
 | 
				
			||||||
				// Super hacky, low quality code
 | 
										query.append(" AND ");
 | 
				
			||||||
				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
 | 
									// TODO: WARNING This logic is flawed. WARNING
 | 
				
			||||||
				// Remember, status can be 'freezer', 'delivered' or 'blocked'
 | 
									// Remember, status can be 'freezer', 'delivered' or 'blocked'
 | 
				
			||||||
| 
						 | 
					@ -174,21 +159,8 @@ public class Database {
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			query.append(";");
 | 
								query.append(";");
 | 
				
			||||||
 | 
					 | 
				
			||||||
			System.out.println(query.toString());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			ResultSet result = stmt.executeQuery(query.toString());
 | 
								ResultSet result = stmt.executeQuery(query.toString());
 | 
				
			||||||
 | 
					 | 
				
			||||||
			// Rename the columns
 | 
					 | 
				
			||||||
			String jsonResult = Jsonizer.toJson(result, "pallets");
 | 
								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;
 | 
								return jsonResult;
 | 
				
			||||||
		} catch (SQLException e) {
 | 
							} catch (SQLException e) {
 | 
				
			||||||
			System.out.printf("Error executing query: \n%s", e);
 | 
								System.out.printf("Error executing query: \n%s", e);
 | 
				
			||||||
| 
						 | 
					@ -201,14 +173,9 @@ public class Database {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public String reset(Request req, Response res) {
 | 
						public String reset(Request req, Response res) {
 | 
				
			||||||
		try {
 | 
							// 1. Wipe database
 | 
				
			||||||
			this.migrateScript("Migrations/create-schema.sql");
 | 
							// 2. Re-run migrations
 | 
				
			||||||
			this.migrateScript("Migrations/initial-data.sql");
 | 
							// 3. Return success
 | 
				
			||||||
		} catch (Exception e) {
 | 
					 | 
				
			||||||
			System.out.printf("Error resetting database: \n%s", e);
 | 
					 | 
				
			||||||
			res.status(500);
 | 
					 | 
				
			||||||
			return "{}";
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		return "{}";
 | 
							return "{}";
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -231,20 +198,13 @@ public class Database {
 | 
				
			||||||
			return "{}";
 | 
								return "{}";
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 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 = ?");
 | 
				
			||||||
		try (PreparedStatement getRawMaterials = conn
 | 
									PreparedStatement insertPallet = conn.prepareStatement("INSERT INTO pallets (cookie_id, production_date, status) VALUES (?, ?, ?)")) {
 | 
				
			||||||
				.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
 | 
								// Start transaction
 | 
				
			||||||
			conn.setAutoCommit(false);
 | 
								conn.setAutoCommit(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			for (Ingredient i : r.get().ingredients) {
 | 
								for(Ingredient i : r.get().ingredients) {
 | 
				
			||||||
				getRawMaterials.setString(1, i.name);
 | 
									getRawMaterials.setString(1, i.name);
 | 
				
			||||||
				ResultSet result = getRawMaterials.executeQuery();
 | 
									ResultSet result = getRawMaterials.executeQuery();
 | 
				
			||||||
				if (!result.next()) {
 | 
									if (!result.next()) {
 | 
				
			||||||
| 
						 | 
					@ -269,20 +229,7 @@ public class Database {
 | 
				
			||||||
				decrementRawMaterials.executeUpdate();
 | 
									decrementRawMaterials.executeUpdate();
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// Fish out the cookie id
 | 
								insertPallet.setInt(1, 1);
 | 
				
			||||||
			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(2, new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
 | 
				
			||||||
			insertPallet.setString(3, "freezer");
 | 
								insertPallet.setString(3, "freezer");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,12 +10,6 @@ public class Recipe {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public String toString() {
 | 
					    public String toString() {
 | 
				
			||||||
        StringBuilder sb = new StringBuilder(name + ": ");
 | 
					        return name;
 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (Ingredient i : ingredients) {
 | 
					 | 
				
			||||||
            sb.append(i.toString());
 | 
					 | 
				
			||||||
            sb.append(" ");
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        return sb.toString();
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue