done #3

Merged
PBS2H21ASH merged 19 commits from done into don 2023-01-24 20:11:21 +01:00
2 changed files with 54 additions and 17 deletions
Showing only changes of commit b2ce4f829d - Show all commits

View File

@ -81,7 +81,7 @@ POST 127.0.0.1/DirektiveDesDons/shoppinglist/1
BODY
{
"incredientId": 1
"ingredientId": 1
}
```

View File

@ -229,9 +229,16 @@ $app->route("/list")
$lastListId = $db->insert("elenco", ["cognome" => $name, "coloreDiSfondo" => $bgColor, "utenteID" => $req["user"]->id]);
$res->json(["message" => "New List '" . $name . "' created", "data" => $db->select("elenco", ["id" => $lastListId])], HTTP_STATUS_CODE::CREATED);
});
$app->get("/list/:id", function (array $req, Response $res) use ($db) {
$app->route("/list/:id")
->get(function (array $req, Response $res) use ($db) {
$id = $req["params"]["id"];
$list = $db->select("elenco", ["id" => $id, "utenteID" => $req["user"]->id])[0];
$query = $db->select("elenco", ["id" => $id, "utenteID" => $req["user"]->id]);
if (count($query) < 1) {
$res->json(["message" => "List does not exists or you dont have permissions to view it"]);
return;
}
$list = $query[0];
$listId = $list["id"];
$ingredients = $db->select("elencoIngredienti", ["elencoId" => $listId]);
@ -242,9 +249,39 @@ $app->get("/list/:id", function (array $req, Response $res) use ($db) {
$list["inredients"] = $ingredientData;
$res->json(["data" => $list]);
});
$app->post("/list/:id", function (array $req, Response $res) use ($db) {
})
->post(function (array $req, Response $res) use ($db) {
// TODO: add Item to List with id
$id = $req["params"]["id"];
$ingredientId = $req["body"]["ingredientId"];
if (!isset($ingredientId)) {
$res->json(["message" => "You need to set a valid 'ingredientId'", HTTP_STATUS_CODE::BAD_REQUEST]);
return;
}
$newId = $db->insert("elencoIngredienti", ["ingredientiID" => $ingredientId, "elencoID" => $id]);
if(!$newId){
$res->json(["message" => "Cannot insert item in list"], HTTP_STATUS_CODE::BAD_REQUEST);
return;
}
$res->json(["message" => "Item has been added"]);
})
->delete(function (array $req, Response $res) use ($db) {
$id = $req["params"]["id"];
$list = $db->select("elenco", ["id" => $id]);
if (count($list) < 1) {
$res->json(["message" => "List does not exist"]);
return;
}
if ($list[0]["utenteID"] != $req["user"]->id) {
$res->json(["message" => "You have no permissions the delete this list"], HTTP_STATUS_CODE::FORBIDDEN);
return;
}
$db->delete("elenco", $id);
$res->json(["message" => "List has been deleted"]);
});
$app->delete("/list/:id/:item", function (array $req, Response $res) use ($db) {
// TODO: delete item from list