Compare commits
	
		
			5 commits
		
	
	
		
			9843a078ea
			...
			6554cb2b6f
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
							 | 
						6554cb2b6f | ||
| 
							 | 
						4aeb738df3 | ||
| 
							 | 
						19b6b05b69 | ||
| 
							 | 
						7d21a572af | ||
| 
							 | 
						8c8584b6bf | 
					 3 changed files with 115 additions and 44 deletions
				
			
		| 
						 | 
					@ -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
 | 
					-- 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 INT PRIMARY KEY,
 | 
					    cookie_id INTEGER 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 INT PRIMARY KEY,
 | 
					    customer_id INTEGER 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 INT PRIMARY KEY,
 | 
					    order_id INTEGER 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),
 | 
				
			||||||
| 
						 | 
					@ -31,7 +41,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 INT PRIMARY KEY,
 | 
					    ingredient_id INTEGER 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'))
 | 
				
			||||||
| 
						 | 
					@ -39,7 +49,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 INT PRIMARY KEY,
 | 
					    delivery_id INTEGER 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,
 | 
				
			||||||
| 
						 | 
					@ -54,7 +64,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 INT PRIMARY KEY,
 | 
					    pallet_id INTEGER 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')),
 | 
				
			||||||
| 
						 | 
					@ -63,3 +73,5 @@ 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,9 +79,6 @@ 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()
 | 
				
			||||||
| 
						 | 
					@ -97,28 +94,28 @@ public class Database {
 | 
				
			||||||
			};
 | 
								};
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Both of these must be present
 | 
							if(from_str != null) {
 | 
				
			||||||
		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));
 | 
				
			||||||
				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();
 | 
									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(to_str != null) {
 | 
				
			||||||
			if (from.isPresent() && to.isPresent() && from.get().after(to.get())) {
 | 
								try {
 | 
				
			||||||
				// We have a bad interval, perhaps set dates to empty agian?
 | 
									to = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(to_str));
 | 
				
			||||||
				// TODO: This obviously need louder error handling
 | 
								} catch (Exception e) {
 | 
				
			||||||
				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.
 | 
				
			||||||
| 
						 | 
					@ -130,28 +127,46 @@ public class Database {
 | 
				
			||||||
		try {
 | 
							try {
 | 
				
			||||||
			Statement stmt = conn.createStatement();
 | 
								Statement stmt = conn.createStatement();
 | 
				
			||||||
			StringBuilder query = new StringBuilder(
 | 
								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
 | 
								// 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 (from != null && to != null) {
 | 
								// If both from and to are present
 | 
				
			||||||
				if (r.isPresent()) {
 | 
								if (from.isPresent()) {
 | 
				
			||||||
					query.append(" AND ");
 | 
									String query_from = new SimpleDateFormat("yyyy-MM-dd").format(from.get());
 | 
				
			||||||
				} else {
 | 
					
 | 
				
			||||||
					query.append(" WHERE ");
 | 
									// Super hacky, low quality code
 | 
				
			||||||
 | 
									String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									query.append(clause + "production_date >= '" + query_from + "'");
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				query.append("production_date BETWEEN '" + from_str + "' AND '" + to_str + "'");
 | 
								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()) {
 | 
								if(blocked.isPresent()) {
 | 
				
			||||||
				// WARNING THIS IS NOT CORRECT WARNING
 | 
									// This again
 | 
				
			||||||
				if (r.isPresent() || from != null) {
 | 
									String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
 | 
				
			||||||
					query.append(" AND ");
 | 
									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'
 | 
				
			||||||
| 
						 | 
					@ -159,8 +174,21 @@ 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);
 | 
				
			||||||
| 
						 | 
					@ -173,9 +201,14 @@ public class Database {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public String reset(Request req, Response res) {
 | 
						public String reset(Request req, Response res) {
 | 
				
			||||||
		// 1. Wipe database
 | 
							try {
 | 
				
			||||||
		// 2. Re-run migrations
 | 
								this.migrateScript("Migrations/create-schema.sql");
 | 
				
			||||||
		// 3. Return success
 | 
								this.migrateScript("Migrations/initial-data.sql");
 | 
				
			||||||
 | 
							} catch (Exception e) {
 | 
				
			||||||
 | 
								System.out.printf("Error resetting database: \n%s", e);
 | 
				
			||||||
 | 
								res.status(500);
 | 
				
			||||||
 | 
								return "{}";
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		return "{}";
 | 
							return "{}";
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -198,9 +231,16 @@ public class Database {
 | 
				
			||||||
			return "{}";
 | 
								return "{}";
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		try (PreparedStatement getRawMaterials = conn.prepareStatement("SELECT * FROM raw_materials WHERE ingredient_name = ?");
 | 
							// System.out.println(r.get());
 | 
				
			||||||
				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 (?, ?, ?)")) {
 | 
							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
 | 
								// Start transaction
 | 
				
			||||||
			conn.setAutoCommit(false);
 | 
								conn.setAutoCommit(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -229,7 +269,20 @@ public class Database {
 | 
				
			||||||
				decrementRawMaterials.executeUpdate();
 | 
									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(2, new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
 | 
				
			||||||
			insertPallet.setString(3, "freezer");
 | 
								insertPallet.setString(3, "freezer");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,6 +10,12 @@ public class Recipe {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public String toString() {
 | 
					    public String toString() {
 | 
				
			||||||
        return name;
 | 
					        StringBuilder sb = new StringBuilder(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