Bara reset och getPallets som ska fixas tror jag, allt annat fungerar

This commit is contained in:
Ivan Bogosavljevic 2024-04-18 00:23:25 +02:00
parent 051c15d8d3
commit d1daee1e03

View file

@ -51,7 +51,7 @@ public class Database {
} }
public String getRawMaterials(Request req, Response res) { 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"; String title = "raw-materials";
return getJson(query, title); return getJson(query, title);
@ -66,14 +66,16 @@ public class Database {
} }
public String getRecipes(Request req, Response res) { 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"; String title = "recipes";
return getJson(query, title); return getJson(query, title);
} }
public String getPallets(Request req, Response res) { 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) { public String reset(Request req, Response res) {
@ -81,7 +83,6 @@ public class Database {
} }
public String createPallet(Request req, Response res) { public String createPallet(Request req, Response res) {
if (req.queryParams("cookie") != null) { if (req.queryParams("cookie") != null) {
String cookie = req.queryParams("cookie"); String cookie = req.queryParams("cookie");
return createPallet(cookie); return createPallet(cookie);
@ -91,11 +92,13 @@ public class Database {
} }
protected String createPallet(String cookie) { protected String createPallet(String cookie) {
String query = ""; String query = "Insert into Pallet (manufacture_date, cookie_name) VALUES (NOW(), ?);";
try (PreparedStatement stmt = conn.prepareStatement(query)) { try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, cookie);
stmt.executeUpdate();
removeMaterials(cookie);
} catch (SQLException e) { } catch (SQLException e) {
return ""; return "bob";
} }
return ""; return "";
} }
@ -104,13 +107,31 @@ public class Database {
private String getJson(String query, String title) { //privat hjälpmetod man slipper kalla jsonizer varje gång private String getJson(String query, String title) { //privat hjälpmetod man slipper kalla jsonizer varje gång
try { try (PreparedStatement stmt = conn.prepareStatement(query)) {
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery(); ResultSet rs = stmt.executeQuery();
return Jsonizer.toJson(rs, title); return Jsonizer.toJson(rs, title);
} catch (SQLException e) { } catch (SQLException e) {
return ""; 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;
}
} }