diff --git a/BancaDati/BancaDati.php b/BancaDati/BancaDati.php index c678d7a..801d5cd 100644 --- a/BancaDati/BancaDati.php +++ b/BancaDati/BancaDati.php @@ -9,7 +9,7 @@ class BancaDati { private $dbName = "BancaDati"; private $linkName = "localhost"; private $user = "root"; - private $pw = "root"; + private $pw = ""; public $pdo; public function __construct() { @@ -55,8 +55,10 @@ class BancaDati { try { $sth = $this->pdo->prepare($sql); $sth->execute(); + return $this->pdo->lastInsertId(); }catch (PDOException $e){ - die; + return false; + //die; } } @@ -128,7 +130,7 @@ class BancaDati { } $sql = "SELECT * FROM " . $table . ((count($where) > 0) ? " WHERE ".$whereString.$orderString : "") .";"; try { - return $this->pdo->query($sql)->fetchAll(); + return $this->pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); }catch (PDOException $e){ die; } diff --git a/index.php b/index.php index 191819a..c25554e 100644 --- a/index.php +++ b/index.php @@ -251,7 +251,6 @@ $app->route("/list/:id") $res->json(["data" => $list]); }) ->post(function (array $req, Response $res) use ($db) { - // TODO: add Item to List with id $id = $req["params"]["id"]; $ingredientId = $req["body"]["ingredientId"]; @@ -284,7 +283,17 @@ $app->route("/list/: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 + $listId = $req["params"]["id"]; + $itemId = $req["params"]["item"]; + + $list = $db->select("elenco", ["id" => $listId, "utenteID" => $req["user"]->id]); + if(count($list) < 1){ + $res->json(["message" => "List does not exist or you dont have the permissions to edit the list"]); + return; + } + + $db->delete("elencoIngredienti", $itemId); + $res->json(["message" => "Item has been deleted"]); }); $app->start(); \ No newline at end of file