More tests passing

This commit is contained in:
Imbus 2024-05-05 07:25:19 +02:00
parent 71687c8bf7
commit 9843a078ea

View file

@ -6,6 +6,7 @@ import spark.Response;
// Likely dependencies for db operations // Likely dependencies for db operations
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -128,7 +129,8 @@ public class Database {
// Helmets, seatbelts and safety goggles on; we need to execute a query. // Helmets, seatbelts and safety goggles on; we need to execute a query.
try { try {
Statement stmt = conn.createStatement(); 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 // r is validated here
if (r.isPresent()) { if (r.isPresent()) {
@ -178,6 +180,67 @@ public class Database {
} }
public String createPallet(Request req, Response res) { 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<Recipe> 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 // 1. Get query param
// 2. Check if cookie exists (is in DefaultRecipes) // 2. Check if cookie exists (is in DefaultRecipes)
// 3. Start transaction // 3. Start transaction