diff --git a/src/main/java/krusty/Database.java b/src/main/java/krusty/Database.java index ebf0683..5ec0f57 100644 --- a/src/main/java/krusty/Database.java +++ b/src/main/java/krusty/Database.java @@ -51,7 +51,7 @@ public class Database { } public String getRawMaterials(Request req, Response res) { - String query = "SELECT name, stock as amount, unit FROM Ingredient"; + String query = "SELECT ingredient_name as name, stock as amount, unit FROM Ingredient"; String title = "raw-materials"; return getJson(query, title); @@ -66,14 +66,16 @@ public class Database { } public String getRecipes(Request req, Response res) { - String query = "Select c.name, i.name, r.amount, i.unit FROM Cookie c, Ingredient i, Recipe r WHERE r.cookie_id = c.cookie_id AND r.ingredient_id = i.ingredient_id;"; + String query = "Select r.cookie_name, i.ingredient_name, r.amount, i.unit From Ingredient i, Recipe r WHERE r.ingredient_name = i.ingredient_name;"; String title = "recipes"; return getJson(query, title); } public String getPallets(Request req, Response res) { - return "{\"pallets\":[]}"; + String query = "Select cookie_name as cookie, blocked From Pallet;"; + String title = "pallets"; + return getJson(query, title); } public String reset(Request req, Response res) { @@ -81,7 +83,6 @@ public class Database { } public String createPallet(Request req, Response res) { - if (req.queryParams("cookie") != null) { String cookie = req.queryParams("cookie"); return createPallet(cookie); @@ -91,11 +92,13 @@ public class Database { } protected String createPallet(String cookie) { - String query = ""; + String query = "Insert into Pallet (manufacture_date, cookie_name) VALUES (NOW(), ?);"; try (PreparedStatement stmt = conn.prepareStatement(query)) { - + stmt.setString(1, cookie); + stmt.executeUpdate(); + removeMaterials(cookie); } catch (SQLException e) { - return ""; + return "bob"; } return ""; } @@ -104,13 +107,31 @@ public class Database { private String getJson(String query, String title) { //privat hjälpmetod så man slipper kalla på jsonizer varje gång - try { - PreparedStatement stmt = conn.prepareStatement(query); + try (PreparedStatement stmt = conn.prepareStatement(query)) { ResultSet rs = stmt.executeQuery(); - return Jsonizer.toJson(rs, title); } catch (SQLException e) { return ""; } } + public void removeMaterials(String cookie) { //privat hjälpmetod för att minska antalet material efter man gör en pallet + String query = "Select ingredient_name, amount from Recipe where cookie_name = ?;"; + String query2 = "UPDATE Ingredient SET stock = stock - ? WHERE ingredient_name = ?;"; + + try (PreparedStatement stmt = conn.prepareStatement(query)) { + stmt.setString(1, cookie); + ResultSet rs = stmt.executeQuery(); + while(rs.next()) { //loopar igenom och uppdaterar all material + try (PreparedStatement stmt2 = conn.prepareStatement(query2)) { + stmt2.setInt(1, rs.getInt("amount") * 54); //54 för (10*15*36)/100 = 10 kakor per låda, 15 lådor per box 36 boxar per pallet, materialet är för 100 kakor därför man delar allt med 100 + stmt2.setString(2, rs.getString("ingredient_name")); + stmt2.executeUpdate(); + } catch (SQLException e) { + } + } + } catch (SQLException e){ + //return false; + } + //return true; + } }