diff --git a/app/src/main/java/krusty/Database.java b/app/src/main/java/krusty/Database.java index 609843c..7a8fed9 100644 --- a/app/src/main/java/krusty/Database.java +++ b/app/src/main/java/krusty/Database.java @@ -6,6 +6,7 @@ import spark.Response; // Likely dependencies for db operations import java.sql.Connection; import java.sql.DriverManager; +import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.text.SimpleDateFormat; @@ -29,7 +30,7 @@ public class Database { private static final String jdbcString = "jdbc:sqlite::memory:"; // private static final String jdbcString = "jdbc:sqlite:krusty.db"; - // Hold a single connection to the database. Note that this is + // Hold a single connection to the database. Note that this is // not a pool, so this is not thread-safe nor efficient. private Connection conn = null; @@ -128,7 +129,8 @@ public class Database { // Helmets, seatbelts and safety goggles on; we need to execute a query. try { Statement stmt = conn.createStatement(); - StringBuilder query = new StringBuilder("SELECT * FROM pallets JOIN cookies ON pallets.cookie_id = cookies.cookie_id"); + StringBuilder query = new StringBuilder( + "SELECT * FROM pallets JOIN cookies ON pallets.cookie_id = cookies.cookie_id"); // r is validated here if (r.isPresent()) { @@ -178,6 +180,67 @@ public class Database { } public String createPallet(Request req, Response res) { + // This on only has one query param and looks like: + // http://localhost:8888/api/v1/pallets?cookie=Amneris + + Optional r = Optional.empty(); + String cookie = req.queryParams("cookie"); + + if (cookie != null) { + r = Optional.ofNullable(DefaultRecipes.recipes.stream() + .filter(recipe -> recipe.name.equals(cookie)) + .findFirst().orElse(null)); + } + + if (r.isEmpty()) { + // Return 404 + res.status(404); + return "{}"; + } + + 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) { + getRawMaterials.setString(1, i.name); + ResultSet result = getRawMaterials.executeQuery(); + if (!result.next()) { + // Rollback transaction + conn.rollback(); + // Return 500 + res.status(500); + return "{}"; + } + + // Check if we have enough raw materials + if (result.getInt("ingredient_quantity") < i.amount) { + // Rollback transaction + conn.rollback(); + // Return 500 + res.status(500); + return "{}"; + } + + decrementRawMaterials.setInt(1, i.amount * 54); // 5400 / 100 + decrementRawMaterials.setString(2, i.name); + decrementRawMaterials.executeUpdate(); + } + + insertPallet.setInt(1, 1); + insertPallet.setString(2, new SimpleDateFormat("yyyy-MM-dd").format(new Date())); + insertPallet.setString(3, "freezer"); + + insertPallet.executeUpdate(); + conn.commit(); + } catch (SQLException e) { + System.out.printf("Error starting transaction: \n%s", e); + } + + // TODO: NOT DONE + // 1. Get query param // 2. Check if cookie exists (is in DefaultRecipes) // 3. Start transaction