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
 | 
			
		||||
--------------------------------------------
 | 
			
		||||
 | 
			
		||||
-- Holds the different types of cookies we can make.
 | 
			
		||||
CREATE TABLE IF NOT EXISTS cookies (
 | 
			
		||||
    cookie_id INTEGER PRIMARY KEY,
 | 
			
		||||
    cookie_id INT PRIMARY KEY,
 | 
			
		||||
    cookie_name VARCHAR(50) NOT NULL UNIQUE
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
-- Our known customers, may need more fields
 | 
			
		||||
CREATE TABLE IF NOT EXISTS customers (
 | 
			
		||||
    customer_id INTEGER PRIMARY KEY,
 | 
			
		||||
    customer_id INT 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 INTEGER PRIMARY KEY,
 | 
			
		||||
    order_id INT 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),
 | 
			
		||||
| 
						 | 
				
			
			@ -41,7 +31,7 @@ CREATE TABLE IF NOT EXISTS orders (
 | 
			
		|||
 | 
			
		||||
-- Describes ingredients and stock.
 | 
			
		||||
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_quantity INT NOT NULL,
 | 
			
		||||
    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?
 | 
			
		||||
CREATE TABLE IF NOT EXISTS raw_materials_deliveries (
 | 
			
		||||
    delivery_id INTEGER PRIMARY KEY,
 | 
			
		||||
    delivery_id INT PRIMARY KEY,
 | 
			
		||||
    ingredient_id INT NOT NULL,
 | 
			
		||||
    delivery_date DATE 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
 | 
			
		||||
-- Order related columns are unused for now.
 | 
			
		||||
CREATE TABLE IF NOT EXISTS pallets (
 | 
			
		||||
    pallet_id INTEGER PRIMARY KEY,
 | 
			
		||||
    pallet_id INT PRIMARY KEY,
 | 
			
		||||
    cookie_id INT NOT NULL,
 | 
			
		||||
    -- order_id INT NOT NULL,
 | 
			
		||||
    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 (order_id) REFERENCES orders(order_id)
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
PRAGMA foreign_keys = ON;
 | 
			
		||||
| 
						 | 
				
			
			@ -79,6 +79,9 @@ 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()
 | 
			
		||||
| 
						 | 
				
			
			@ -94,26 +97,26 @@ public class Database {
 | 
			
		|||
			};
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if(from_str != null) {
 | 
			
		||||
		// Both of these must be present
 | 
			
		||||
		if (from_str != null && to_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) {
 | 
			
		||||
				to = Optional.empty();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// If the interval is negative, reset the dates
 | 
			
		||||
		if(from.isPresent() && to.isPresent() && from.get().after(to.get())) {
 | 
			
		||||
				// 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();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// This type of code is unreadable, error prone and hard to maintain.
 | 
			
		||||
| 
						 | 
				
			
			@ -127,46 +130,28 @@ public class Database {
 | 
			
		|||
		try {
 | 
			
		||||
			Statement stmt = conn.createStatement();
 | 
			
		||||
			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
 | 
			
		||||
			if (r.isPresent()) {
 | 
			
		||||
				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());
 | 
			
		||||
 | 
			
		||||
				// Super hacky, low quality code
 | 
			
		||||
				String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
 | 
			
		||||
 | 
			
		||||
				query.append(clause + "production_date >= '" + query_from + "'");
 | 
			
		||||
			if (from != null && to != null) {
 | 
			
		||||
				if (r.isPresent()) {
 | 
			
		||||
					query.append(" AND ");
 | 
			
		||||
				} else {
 | 
			
		||||
					query.append(" WHERE ");
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
			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 + "'");
 | 
			
		||||
				query.append("production_date BETWEEN '" + from_str + "' AND '" + to_str + "'");
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// 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);
 | 
			
		||||
			if (blocked.isPresent()) {
 | 
			
		||||
				// WARNING THIS IS NOT CORRECT WARNING
 | 
			
		||||
				if (r.isPresent() || from != null) {
 | 
			
		||||
					query.append(" AND ");
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				// TODO: WARNING This logic is flawed. WARNING
 | 
			
		||||
				// Remember, status can be 'freezer', 'delivered' or 'blocked'
 | 
			
		||||
| 
						 | 
				
			
			@ -174,21 +159,8 @@ 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);
 | 
			
		||||
| 
						 | 
				
			
			@ -201,14 +173,9 @@ public class Database {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	public String reset(Request req, Response res) {
 | 
			
		||||
		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 "{}";
 | 
			
		||||
		}
 | 
			
		||||
		// 1. Wipe database
 | 
			
		||||
		// 2. Re-run migrations
 | 
			
		||||
		// 3. Return success
 | 
			
		||||
		return "{}";
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -231,20 +198,13 @@ public class Database {
 | 
			
		|||
			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 = ?");
 | 
			
		||||
				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 = ?")) {
 | 
			
		||||
		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 (?, ?, ?)")) {
 | 
			
		||||
			// 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()) {
 | 
			
		||||
| 
						 | 
				
			
			@ -269,20 +229,7 @@ public class Database {
 | 
			
		|||
				decrementRawMaterials.executeUpdate();
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// 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.setInt(1, 1);
 | 
			
		||||
			insertPallet.setString(2, new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
 | 
			
		||||
			insertPallet.setString(3, "freezer");
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,12 +10,6 @@ public class Recipe {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    public String toString() {
 | 
			
		||||
        StringBuilder sb = new StringBuilder(name + ": ");
 | 
			
		||||
 | 
			
		||||
        for (Ingredient i : ingredients) {
 | 
			
		||||
            sb.append(i.toString());
 | 
			
		||||
            sb.append(" ");
 | 
			
		||||
        }
 | 
			
		||||
        return sb.toString();
 | 
			
		||||
        return name;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue