add: update and delete ingredients

This commit is contained in:
Johannes Kantz 2023-01-24 04:05:15 +01:00
parent e465ed9e95
commit 9a00e7eb16

View File

@ -113,11 +113,37 @@ $app->route("/ingredient")
$res->json(["message" => "New ingredient has been listed", $res->json(["message" => "New ingredient has been listed",
"ingredient" => $db->select("ingredienti", ["id" => $id])]); "ingredient" => $db->select("ingredienti", ["id" => $id])]);
}); });
$app->get("/ingredient/:id", function (array $req, Response $res) use ($db) { $app->route("/ingredient/:id")
->get(function (array $req, Response $res) use ($db) {
$id = $req["params"]["id"]; $id = $req["params"]["id"];
$ingredient = $db->select("ingredienti", ["id" => $id])[0]; $ingredient = $db->select("ingredienti", ["id" => $id])[0];
$res->json(["data" => $ingredient]); $res->json(["data" => $ingredient]);
}); })
->put(function (array $req, Response $res) use ($db) {
$id = $req["params"]["id"];
$name = $req["body"]["name"];
$calories = $req["body"]["calories"];
$quantity = $req["body"]["quantity"];
$unit = $req["body"]["unit"];
$price = $req["body"]["price"];
$unitInTable = $db->select("folla", ["unita" => $unit]);
if (count($unitInTable) > 1) {
$res->json(["message" => "Unit: " . $unit . " does not exist. Please create unit first"], HTTP_STATUS_CODE::BAD_REQUEST);
return;
}
$unitId = $unitInTable[0]["id"];
$db->update("ingredienti", $id,["cognome" => "$name", "calorie" => "$calories", "quantita" => "$quantity", "follaID" => $unitId, "prezzo" => "$price"]);
$res->json(["message" => "Ingredient has been updated", "data" => $db->select("ingredienti", ["id" => $id])[0]]);
})
->delete(function (array $req, Response $res) use ($db) {
$id = $req["params"]["id"];
$db->delete("ingredienti", $id);
$res->json(["message" => "Ingredient has been deleted"]);
});
/* /*
* Unit * Unit