From 1038deded0eba02f012c398c7acf0e6f524e4519 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Thu, 8 Dec 2022 18:01:23 +0100 Subject: [PATCH 01/18] fix: redirect everything to index.php --- .htaccess | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.htaccess b/.htaccess index a589ab5..4444ee4 100644 --- a/.htaccess +++ b/.htaccess @@ -1 +1,5 @@ -FallbackResource /DirektiveDesDons/index.php \ No newline at end of file +# Redirect everything to index.php +RewriteEngine on +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule ^.*$ /DirektiveDesDons/index.php [L,QSA] \ No newline at end of file -- 2.39.5 From 464a9c7dbce63ff46c797d7585971dada80d9767 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Thu, 19 Jan 2023 18:37:48 +0100 Subject: [PATCH 02/18] fix: select fetchAll --- BancaDati/BancaDati.php | 7 ++++--- User.php | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/BancaDati/BancaDati.php b/BancaDati/BancaDati.php index 9690979..c678d7a 100644 --- a/BancaDati/BancaDati.php +++ b/BancaDati/BancaDati.php @@ -22,6 +22,7 @@ class BancaDati { , $this->pw , array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } catch (PDOException $e) { + var_dump($e); die; } } @@ -108,7 +109,7 @@ class BancaDati { * @return void * @author Malte Schulze Hobeling */ - public function select(string $table, array $where){ + public function select(string $table, array $where = []){ $whereString = ""; $orderString = ""; if(isset($where["by"])){ @@ -125,9 +126,9 @@ class BancaDati { } $whereString .= "`" . $col . "` LIKE '" . $v . "'"; } - $sql = "SELECT * FROM ".$table." WHERE ".$whereString.$orderString.";"; + $sql = "SELECT * FROM " . $table . ((count($where) > 0) ? " WHERE ".$whereString.$orderString : "") .";"; try { - return $this->pdo->query($sql)->fetch(); + return $this->pdo->query($sql)->fetchAll(); }catch (PDOException $e){ die; } diff --git a/User.php b/User.php index 203368b..ba73a67 100644 --- a/User.php +++ b/User.php @@ -18,7 +18,7 @@ class User { return true; } public function loginWithUsername(string $username, string $password) : string { - $userObject = $this->db->select("utente", ["nomeUtente" => $username]); + $userObject = $this->db->select("utente", ["nomeUtente" => $username])[0]; if(!$userObject){ return false; @@ -38,7 +38,7 @@ class User { return $this->token; } public function loginWithToken(string $token){ - $userObject = $this->db->select("utente", ["gettone" => $token]); + $userObject = $this->db->select("utente", ["gettone" => $token])[0]; if(!$userObject){ return false; } -- 2.39.5 From 1034dd30f93e3e2bdba7d1cfc81e78eaaacf4092 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Sat, 21 Jan 2023 13:25:49 +0100 Subject: [PATCH 03/18] add: json error message --- Router/Router.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Router/Router.php b/Router/Router.php index 35d05fc..6e5b7a3 100644 --- a/Router/Router.php +++ b/Router/Router.php @@ -235,7 +235,7 @@ class Router if ($controller) { $controller($this->request, $this->response); } else { - echo "404"; + $this->response->json(["status" => "404", "message" => "Not found"], 404); } } } \ No newline at end of file -- 2.39.5 From c110825140cd4ca190dde2420b674d7331206300 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Sat, 21 Jan 2023 13:49:59 +0100 Subject: [PATCH 04/18] fix: ignore trailing / at the end of uri --- Router/Router.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Router/Router.php b/Router/Router.php index 6e5b7a3..edf7c2f 100644 --- a/Router/Router.php +++ b/Router/Router.php @@ -20,8 +20,9 @@ class Router */ public function __construct(string $pathname) { - $this->uri = is_countable($_SERVER['REQUEST_URI']) && count($_SERVER['REQUEST_URI']) > 1 ? rtrim($_SERVER['REQUEST_URI'], "/") : $_SERVER['REQUEST_URI']; - $this->uri = str_replace($pathname, "", $this->uri); + $this->uri = str_replace($pathname, "", $_SERVER['REQUEST_URI']); + $this->uri = strlen($this->uri) > 1 ? rtrim($this->uri, "/") : $this->uri; + $this->requestType = $_SERVER['REQUEST_METHOD']; $this->routes = []; $this->middleware = []; -- 2.39.5 From 92f539fa146cf536cc63e7e24db14c2dc35f1fd0 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Sat, 21 Jan 2023 14:10:02 +0100 Subject: [PATCH 05/18] fix: typo --- Router/Router.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Router/Router.php b/Router/Router.php index edf7c2f..69baccd 100644 --- a/Router/Router.php +++ b/Router/Router.php @@ -33,7 +33,7 @@ class Router if (isset($_POST)) { $this->request["body"] = json_decode(file_get_contents('php://input'), true); } - if (isset($_POST)) { + if (isset($_GET)) { $this->request["params"] = $_GET; } -- 2.39.5 From 288dbd6d4cd10c87b78ce07745b58ea98da12da4 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Mon, 23 Jan 2023 00:33:56 +0100 Subject: [PATCH 06/18] add: Ingredient Route --- index.php | 67 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/index.php b/index.php index b172924..a0b3a32 100644 --- a/index.php +++ b/index.php @@ -2,11 +2,13 @@ require_once("Router/Router.php"); require_once("Router/Response.php"); +require_once("Router/Route.php"); require_once("BancaDati/BancaDati.php"); require_once("User.php"); use Router\Response; use Router\Router; +use Router\Route; use BancaDati\BancaDati; $app = new Router("/DirektiveDesDons"); @@ -21,26 +23,33 @@ $app->use("/", function (array &$req, Response $res) { } }); -$app->get("/", function (array $req, Response $res) { +$app->get("/", function (array $req, Response $res) use ($db) { $res->send("Hello World"); //var_dump($req["user"]); }); +/* + * User + */ + $app->get("/user", function (array $req, Response $res) { - $res->send("user"); + if(isset($req["user"])){ + $res->json(["id" => $req["user"]->id, "username" => $req["user"]->username, "email" => $req["user"]->email]); + }else { + $res->json(["status" => 403, "message" => "You are not logged in. Goto '/login' to login"]); + } }); $app->get("/user/:id", function (array $req, Response $res) use ($db) { $db->select("utente", ["username" => $req["id"]]); $res->send("user " . $req["params"]["id"]); }); -$app->post("/createuser", function (array $req, Response $res) use ($db) { +$app->post("/signup", function (array $req, Response $res) use ($db) { $newUsername = $req["body"]["username"]; $newPassword = $req["body"]["password"]; $newEmail = $req["body"]["email"]; - // $db->insert("utente", ["email" => "test@email.com", "parolaDordine" => "password", "nomeUtente" => "testuser"]); $db->insert("utente", ["email" => "$newEmail", "parolaDordine" => "$newPassword", "nomeUtente" => "$newUsername"]); - $res->send("user "); + $res->send("Account Created", 201); }); $app->post("/login", function( array $req, Response $res) use ($db) { @@ -58,21 +67,43 @@ $app->post("/login", function( array $req, Response $res) use ($db) { } }); -$app->post("/createingredients", function (array $req, Response $res) use ($db) { - $newIngredient = $req["body"]["ingredient"]; - $newCalorie = $req["body"]["calories"]; - $newWeight = $req["body"]["weight"]; - $newPrice = $req["body"]["price"]; - $db->insert("ingredienti", ["cognome" => "$newIngredient", "caloriePerCento" => "$newCalorie", "ilPeso" => "$newWeight", "prezzo" => "$newPrice"]); +/* + * Ingredients + */ +$app->route("/ingredient") + ->get(function (array $req, Response $res) use ($db) { + $res->json(["status" => 200, "data" => $db->select("ingredienti")]); + }) + ->post(function (array $req, Response $res) use ($db) { + $name = $req["body"]["name"]; + $calories = $req["body"]["calories"]; + $quantity = $req["body"]["quantity"]; + $unit = $req["body"]["unit"]; + $price = $req["body"]["price"]; - $res->send("New ingredient has been listed "); + $unitInTable = $db->select("folla", ["unita" => $unit]); + if(count($unitInTable) > 1){ + $res->json(["status" => "400", "message" => "Unit: " . $unit . " does not exist. Please create unit first" ]); + return; + } + $unitId = $unitInTable[0]["id"]; + + $id = $db->insert("ingredienti", ["cognome" => "$name", "calorie" => "$calories", "quantita" => "$quantity", "follaID" => $unitId, "prezzo" => "$price"]); + if(!$id){ + $res->json(["status" => 400, + "message" => "Something went wrong when creating the Ingredient"], 500); + return; + } + + $res->json(["status" => "200", + "message" => "New ingredient has been listed", + "ingredient" => $db->select("ingredienti", ["id" => $id])]); + }); +$app->get("/ingredient/:id", function (array $req, Response $res) use ($db) { + $id = $req["params"]["id"]; + $ingredient = $db->select("ingredienti", ["id" => $id])[0]; + $res->json(["status" => 200, "data" => $ingredient]); }); -$app->post("/createunits", function (array $req, Response $res) use ($db) { - $newUnit = $req["body"]["unit"]; - $db->insert("folla", ["unita" => "$newUnit"]); - - $res->send("New unit has been listed "); -}); $app->start(); \ No newline at end of file -- 2.39.5 From 83019b999ca66bdb7f7aa465ad273b06dcf3f95a Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Mon, 23 Jan 2023 01:08:14 +0100 Subject: [PATCH 07/18] add: include http status codes in json response --- Router/Response.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Router/Response.php b/Router/Response.php index 69065d7..8dedd7e 100644 --- a/Router/Response.php +++ b/Router/Response.php @@ -31,6 +31,7 @@ class Response http_response_code($status); } header('Content-Type: application/json; charset=utf-8'); + $data["status"] = http_response_code(); echo json_encode($data); } -- 2.39.5 From 67912410d9d48b460f747531f872312c092b00b4 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Mon, 23 Jan 2023 01:12:43 +0100 Subject: [PATCH 08/18] add: unit and more simple http status codes --- index.php | 60 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/index.php b/index.php index a0b3a32..39c3d5c 100644 --- a/index.php +++ b/index.php @@ -11,6 +11,16 @@ use Router\Router; use Router\Route; use BancaDati\BancaDati; +abstract class HTTP_STATUS_CODE +{ + const OK = 200; + const CREATED = 201; + const BAD_REQUEST = 400; + const FORBIDDEN = 403; + const NOT_FOUNT = 404; +} + + $app = new Router("/DirektiveDesDons"); $db = new BancaDati(); @@ -31,12 +41,11 @@ $app->get("/", function (array $req, Response $res) use ($db) { /* * User */ - $app->get("/user", function (array $req, Response $res) { if(isset($req["user"])){ $res->json(["id" => $req["user"]->id, "username" => $req["user"]->username, "email" => $req["user"]->email]); }else { - $res->json(["status" => 403, "message" => "You are not logged in. Goto '/login' to login"]); + $res->json(["status" => HTTP_STATUS_CODE::FORBIDDEN, "message" => "You are not logged in. Goto '/login' to login"]); } }); $app->get("/user/:id", function (array $req, Response $res) use ($db) { @@ -49,7 +58,7 @@ $app->post("/signup", function (array $req, Response $res) use ($db) { $newEmail = $req["body"]["email"]; $db->insert("utente", ["email" => "$newEmail", "parolaDordine" => "$newPassword", "nomeUtente" => "$newUsername"]); - $res->send("Account Created", 201); + $res->send("Account Created", HTTP_STATUS_CODE::CREATED); }); $app->post("/login", function( array $req, Response $res) use ($db) { @@ -61,9 +70,9 @@ $app->post("/login", function( array $req, Response $res) use ($db) { if($usertoken){ setcookie("TOKEN", $usertoken, time()+3600); // 1h - $res->send("Login successful" . "token: " . $usertoken, 200); + $res->send("Login successful" . "token: " . $usertoken); }else{ - $res->send("Login failed", 403); + $res->send("Login failed", HTTP_STATUS_CODE::FORBIDDEN); } }); @@ -72,7 +81,7 @@ $app->post("/login", function( array $req, Response $res) use ($db) { */ $app->route("/ingredient") ->get(function (array $req, Response $res) use ($db) { - $res->json(["status" => 200, "data" => $db->select("ingredienti")]); + $res->json(["data" => $db->select("ingredienti")]); }) ->post(function (array $req, Response $res) use ($db) { $name = $req["body"]["name"]; @@ -83,27 +92,54 @@ $app->route("/ingredient") $unitInTable = $db->select("folla", ["unita" => $unit]); if(count($unitInTable) > 1){ - $res->json(["status" => "400", "message" => "Unit: " . $unit . " does not exist. Please create unit first" ]); + $res->json(["message" => "Unit: " . $unit . " does not exist. Please create unit first"], HTTP_STATUS_CODE::BAD_REQUEST); return; } $unitId = $unitInTable[0]["id"]; $id = $db->insert("ingredienti", ["cognome" => "$name", "calorie" => "$calories", "quantita" => "$quantity", "follaID" => $unitId, "prezzo" => "$price"]); if(!$id){ - $res->json(["status" => 400, - "message" => "Something went wrong when creating the Ingredient"], 500); + $res->json(["message" => "Something went wrong when creating the Ingredient"], HTTP_STATUS_CODE::BAD_REQUEST); return; } - $res->json(["status" => "200", - "message" => "New ingredient has been listed", + $res->json(["message" => "New ingredient has been listed", "ingredient" => $db->select("ingredienti", ["id" => $id])]); }); $app->get("/ingredient/:id", function (array $req, Response $res) use ($db) { $id = $req["params"]["id"]; $ingredient = $db->select("ingredienti", ["id" => $id])[0]; - $res->json(["status" => 200, "data" => $ingredient]); + $res->json(["data" => $ingredient]); }); +/* + * Unit + */ +$app->route("/unit") + ->get(function (array $req, Response $res) use ($db) { + $res->json(["data" => $db->select("folla")]); + }) + ->post(function (array $req, Response $res) use ($db) { + $name = $req["body"]["name"]; + + if(!isset($name) || strlen($name) < 1 || strlen($name) > 200){ + $res->json(["message" => "Invalid Request. Please follow the the Documentation", HTTP_STATUS_CODE::BAD_REQUEST]); + return; + } + + $selectWithSameName = $db->select("folla", ["unita" => $name]); + if(count($selectWithSameName) >= 1){ + $res->json(["message" => "Unit: " . $name ." already exists", "data" => $selectWithSameName[0]], HTTP_STATUS_CODE::BAD_REQUEST); + return; + } + + $newUnitId = $db->insert("folla", ["unita" => $name]); + $res->json(["message" => "Unit: '" . $name . "' created", "data" => $db->select("folla", ["id" => $newUnitId])[0]], HTTP_STATUS_CODE::CREATED); + }); +$app->get("/unit/:id", function (array $req, Response $res) use ($db) { + $id = $req["params"]["id"]; + $ingredient = $db->select("folla", ["id" => $id])[0]; + $res->json(["data" => $ingredient]); +}); $app->start(); \ No newline at end of file -- 2.39.5 From c717e5c4e9970bde10fff23ddad9cb58037e04dd Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Mon, 23 Jan 2023 05:26:12 +0100 Subject: [PATCH 09/18] add: List --- index.php | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/index.php b/index.php index 39c3d5c..f2aa0fa 100644 --- a/index.php +++ b/index.php @@ -24,6 +24,10 @@ abstract class HTTP_STATUS_CODE $app = new Router("/DirektiveDesDons"); $db = new BancaDati(); + +/* + * Middleware + */ $app->use("/", function (array &$req, Response $res) { if(isset($_COOKIE["TOKEN"])){ $user = new User(); @@ -33,6 +37,9 @@ $app->use("/", function (array &$req, Response $res) { } }); +/* + * Home + */ $app->get("/", function (array $req, Response $res) use ($db) { $res->send("Hello World"); //var_dump($req["user"]); @@ -123,7 +130,7 @@ $app->route("/unit") $name = $req["body"]["name"]; if(!isset($name) || strlen($name) < 1 || strlen($name) > 200){ - $res->json(["message" => "Invalid Request. Please follow the the Documentation", HTTP_STATUS_CODE::BAD_REQUEST]); + $res->json(["message" => "Invalid Request. Please follow the Documentation", HTTP_STATUS_CODE::BAD_REQUEST]); return; } @@ -142,4 +149,62 @@ $app->get("/unit/:id", function (array $req, Response $res) use ($db) { $res->json(["data" => $ingredient]); }); +/* + * List + */ +$app->use("/list", function (array $req, Response $res) { + if(!isset($req["user"])){ + $res->json(["message" => "You need to be signed in to use lists"], HTTP_STATUS_CODE::FORBIDDEN); + die; + } +}); + +$app->route("/list") + ->get(function (array $req, Response $res) use ($db) { + $lists = $db->select("elenco", ["utenteID" => $req["user"]->id]); + // add items to list + foreach ($lists as &$list){ + $listId = $list["id"]; + $ingredients = $db->select("elencoIngredienti", ["elencoId" => $listId]); + $ingredientData = []; + foreach ($ingredients as &$ingredient){ + $ingredientData[] = $db->select("Ingredienti", ["id" => $ingredient["ingredientiID"]]); + } + $list["inredients"] = $ingredientData; + } + $res->json(["data" => $lists]); + }) + ->post(function (array $req, Response $res) use ($db) { + $name = $req["body"]["name"]; + $bgColor = $req["body"]["backgoundColor"] ?? "#fff"; + + if(!isset($name) || strlen($name) < 1){ + $res->json(["message" => "Invalid Request. Please follow the Documentation"], HTTP_STATUS_CODE::BAD_REQUEST); + return; + } + + $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) { + $id = $req["params"]["id"]; + $list = $db->select("elenco", ["id" => $id, "utenteID" => $req["user"]->id])[0]; + + $listId = $list["id"]; + $ingredients = $db->select("elencoIngredienti", ["elencoId" => $listId]); + $ingredientData = []; + foreach ($ingredients as &$ingredient){ + $ingredientData[] = $db->select("Ingredienti", ["id" => $ingredient["ingredientiID"]]); + } + $list["inredients"] = $ingredientData; + + $res->json(["data" => $list]); +}); +$app->post("/list/:id", function (array $req, Response $res) use ($db) { + // TODO: add Item to List with id +}); +$app->delete("/list/:id/:item", function (array $req, Response $res) use ($db) { + // TODO: delete item from list +}); + $app->start(); \ No newline at end of file -- 2.39.5 From e8775385cf460367c2151075febd9ea307164a0f Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Mon, 23 Jan 2023 05:31:35 +0100 Subject: [PATCH 10/18] fix: refactor --- index.php | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/index.php b/index.php index f2aa0fa..f5aee5d 100644 --- a/index.php +++ b/index.php @@ -11,8 +11,7 @@ use Router\Router; use Router\Route; use BancaDati\BancaDati; -abstract class HTTP_STATUS_CODE -{ +abstract class HTTP_STATUS_CODE { const OK = 200; const CREATED = 201; const BAD_REQUEST = 400; @@ -29,9 +28,9 @@ $db = new BancaDati(); * Middleware */ $app->use("/", function (array &$req, Response $res) { - if(isset($_COOKIE["TOKEN"])){ + if (isset($_COOKIE["TOKEN"])) { $user = new User(); - if($user->loginWithToken($_COOKIE["TOKEN"])){ + if ($user->loginWithToken($_COOKIE["TOKEN"])) { $req["user"] = $user; } } @@ -41,17 +40,18 @@ $app->use("/", function (array &$req, Response $res) { * Home */ $app->get("/", function (array $req, Response $res) use ($db) { - $res->send("Hello World"); - //var_dump($req["user"]); + $res->json(["message" => "", + "name" => "Shopping List", + "authors" => "Simon Bock, Johannes Kantz & Malte Schulze Hobeling"]); }); /* * User */ $app->get("/user", function (array $req, Response $res) { - if(isset($req["user"])){ + if (isset($req["user"])) { $res->json(["id" => $req["user"]->id, "username" => $req["user"]->username, "email" => $req["user"]->email]); - }else { + } else { $res->json(["status" => HTTP_STATUS_CODE::FORBIDDEN, "message" => "You are not logged in. Goto '/login' to login"]); } }); @@ -68,18 +68,18 @@ $app->post("/signup", function (array $req, Response $res) use ($db) { $res->send("Account Created", HTTP_STATUS_CODE::CREATED); }); -$app->post("/login", function( array $req, Response $res) use ($db) { +$app->post("/login", function (array $req, Response $res) use ($db) { $username = $req["body"]["username"]; $password = $req["body"]["password"]; $user = new User(); $usertoken = $user->loginWithUsername($username, $password); - if($usertoken){ - setcookie("TOKEN", $usertoken, time()+3600); // 1h - $res->send("Login successful" . "token: " . $usertoken); - }else{ - $res->send("Login failed", HTTP_STATUS_CODE::FORBIDDEN); + if ($usertoken) { + setcookie("TOKEN", $usertoken, time() + 3600); // 1h + $res->json(["message" => "Login successful", "token" => $usertoken]); + } else { + $res->json(["message" => "Login failed"], HTTP_STATUS_CODE::FORBIDDEN); } }); @@ -98,14 +98,14 @@ $app->route("/ingredient") $price = $req["body"]["price"]; $unitInTable = $db->select("folla", ["unita" => $unit]); - if(count($unitInTable) > 1){ + 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"]; $id = $db->insert("ingredienti", ["cognome" => "$name", "calorie" => "$calories", "quantita" => "$quantity", "follaID" => $unitId, "prezzo" => "$price"]); - if(!$id){ + if (!$id) { $res->json(["message" => "Something went wrong when creating the Ingredient"], HTTP_STATUS_CODE::BAD_REQUEST); return; } @@ -129,14 +129,14 @@ $app->route("/unit") ->post(function (array $req, Response $res) use ($db) { $name = $req["body"]["name"]; - if(!isset($name) || strlen($name) < 1 || strlen($name) > 200){ + if (!isset($name) || strlen($name) < 1 || strlen($name) > 200) { $res->json(["message" => "Invalid Request. Please follow the Documentation", HTTP_STATUS_CODE::BAD_REQUEST]); return; } $selectWithSameName = $db->select("folla", ["unita" => $name]); - if(count($selectWithSameName) >= 1){ - $res->json(["message" => "Unit: " . $name ." already exists", "data" => $selectWithSameName[0]], HTTP_STATUS_CODE::BAD_REQUEST); + if (count($selectWithSameName) >= 1) { + $res->json(["message" => "Unit: " . $name . " already exists", "data" => $selectWithSameName[0]], HTTP_STATUS_CODE::BAD_REQUEST); return; } @@ -153,7 +153,7 @@ $app->get("/unit/:id", function (array $req, Response $res) use ($db) { * List */ $app->use("/list", function (array $req, Response $res) { - if(!isset($req["user"])){ + if (!isset($req["user"])) { $res->json(["message" => "You need to be signed in to use lists"], HTTP_STATUS_CODE::FORBIDDEN); die; } @@ -163,11 +163,11 @@ $app->route("/list") ->get(function (array $req, Response $res) use ($db) { $lists = $db->select("elenco", ["utenteID" => $req["user"]->id]); // add items to list - foreach ($lists as &$list){ + foreach ($lists as &$list) { $listId = $list["id"]; $ingredients = $db->select("elencoIngredienti", ["elencoId" => $listId]); $ingredientData = []; - foreach ($ingredients as &$ingredient){ + foreach ($ingredients as &$ingredient) { $ingredientData[] = $db->select("Ingredienti", ["id" => $ingredient["ingredientiID"]]); } $list["inredients"] = $ingredientData; @@ -178,7 +178,7 @@ $app->route("/list") $name = $req["body"]["name"]; $bgColor = $req["body"]["backgoundColor"] ?? "#fff"; - if(!isset($name) || strlen($name) < 1){ + if (!isset($name) || strlen($name) < 1) { $res->json(["message" => "Invalid Request. Please follow the Documentation"], HTTP_STATUS_CODE::BAD_REQUEST); return; } @@ -193,7 +193,7 @@ $app->get("/list/:id", function (array $req, Response $res) use ($db) { $listId = $list["id"]; $ingredients = $db->select("elencoIngredienti", ["elencoId" => $listId]); $ingredientData = []; - foreach ($ingredients as &$ingredient){ + foreach ($ingredients as &$ingredient) { $ingredientData[] = $db->select("Ingredienti", ["id" => $ingredient["ingredientiID"]]); } $list["inredients"] = $ingredientData; -- 2.39.5 From 09ab44362287ebe5f3a3fd12821f1da342708dc7 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Mon, 23 Jan 2023 05:33:21 +0100 Subject: [PATCH 11/18] add: test data & slight adjustments --- BancaDati.sql | 100 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 34 deletions(-) diff --git a/BancaDati.sql b/BancaDati.sql index fc74c0f..512a69b 100644 --- a/BancaDati.sql +++ b/BancaDati.sql @@ -3,64 +3,96 @@ SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; +CREATE TABLE `folla` +( /*Menge*/ + `id` int auto_increment NOT NULL PRIMARY KEY, + `unita` varchar(200) UNIQUE NOT NULL, /*Einheit*/ + `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP +) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `ingredienti` ( /*Zutaten*/ - `id` int auto_increment NOT NULL PRIMARY KEY, - `cognome` varchar(200) NOT NULL, /*Name*/ - `caloriePerCento` integer(5) NOT NULL, /*Kalorien pro Gramm*/ - `ilPeso` integer(5) NULL, /*Gewicht*/ - `prezzo` decimal(4, 2) NOT NULL, /*Preis*/ - `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP -) ENGINE=InnoDB DEFAULT CHARSET=utf8; - -CREATE TABLE `folla` -( /*Menge*/ - `id` int auto_increment NOT NULL PRIMARY KEY, - `unita` varchar(200) NOT NULL, /*Einheit*/ - `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP + `id` int auto_increment NOT NULL PRIMARY KEY, + `cognome` varchar(200) NOT NULL, /*Name*/ + `calorie` integer(5) NOT NULL, /*Kalorien*/ + `quantita` integer(5) NOT NULL, /*Anzahl*/ + `prezzo` decimal(4, 2) NOT NULL, /*Preis*/ + `follaID` int NOT NULL, /*MengeID*/ + `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `elenco` ( /*Liste*/ - `id` int auto_increment NOT NULL PRIMARY KEY, - `creatore` varchar(200) NOT NULL, /*Ersteller*/ - `coloreDiSfondo` integer(10) NOT NULL, /*Hintergrundfarbe*/ - `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP + `id` int auto_increment NOT NULL PRIMARY KEY, + `utenteID` int NOT NULL, /*ErstellerID*/ + `cognome` varchar(200) NOT NULL, /*Name*/ + `coloreDiSfondo` varchar(200), /*Hintergrundfarbe*/ + `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `utente` ( /*Benutzer*/ - `id` int auto_increment NOT NULL PRIMARY KEY, - `email` varchar(200) NOT NULL, /*Email*/ - `parolaDordine` varchar(255) NOT NULL, /*Passwort*/ - `nomeUtente` varchar(50) UNIQUE NOT NULL, /*Benutzernamen*/ - `gettone` varchar(255), /*Token für Session*/ - `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP + `id` int auto_increment NOT NULL PRIMARY KEY, + `email` varchar(200) NOT NULL, /*Email*/ + `parolaDordine` varchar(255) NOT NULL, /*Passwort*/ + `nomeUtente` varchar(50) UNIQUE NOT NULL, /*Benutzernamen*/ + `gettone` varchar(255), /*Token für Session*/ + `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `elencoIngredienti` ( /*Liste_Zutaten*/ - `id` int auto_increment NOT NULL PRIMARY KEY, - `ingredientiID` int NOT NULL, /*ZutatenID*/ - `elencoID` int NOT NULL, /*ListeID*/ - `follaID` int NOT NULL, /*MengeID*/ - `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP + `id` int auto_increment NOT NULL PRIMARY KEY, + `ingredientiID` int NOT NULL, /*ZutatenID*/ + `elencoID` int NOT NULL, /*ListeID*/ + `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `utenteElenco` ( /*Benutzer_Liste*/ - `id` int auto_increment NOT NULL PRIMARY KEY, - `elencoID` int NOT NULL, /*ListeID*/ - `utenteID` int NOT NULL, /*BenutzerID*/ - `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP + `id` int auto_increment NOT NULL PRIMARY KEY, + `elencoID` int NOT NULL, /*ListeID*/ + `utenteID` int NOT NULL, /*BenutzerID*/ + `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `elencoIngredienti` /*Liste_Zutaten*/ ADD CONSTRAINT `FK_ElencoIngredienti_Ingredienti` FOREIGN KEY (`ingredientiID`) REFERENCES `ingredienti`(`id`), /*Liste_Zutaten hat Foreignkey von Zutaten(id)*/ - ADD CONSTRAINT `FK_ElencoIngredienti_Elenco` FOREIGN KEY (`elencoID`) REFERENCES `elenco`(`id`), /*Liste_Zutaten hat Foreignkey von Liste(id)*/ - ADD CONSTRAINT `FK_ElencoIngredienti_Folla` FOREIGN KEY (`follaID`) REFERENCES `folla`(`id`); /*Liste_Zutaten hat Foreignkey von Menge(id)*/ + ADD CONSTRAINT `FK_ElencoIngredienti_Elenco` FOREIGN KEY (`elencoID`) REFERENCES `elenco`(`id`); /*Liste_Zutaten hat Foreignkey von Liste(id)*/ ALTER TABLE `utenteElenco` /*Benutzer_Liste*/ ADD CONSTRAINT `FK_UtenteElenco_Utente` FOREIGN KEY (`utenteId`) REFERENCES `utente`(`id`), /*Benutzer_Liste hat Foreignkey von Benutzer(id)*/ ADD CONSTRAINT `FK_UtenteElenco_Elenco` FOREIGN KEY (`elencoId`) REFERENCES `elenco`(`id`); /*Benutzer_Liste hat Foreignkey von Liste(id)*/ + +ALTER TABLE `ingredienti` + ADD CONSTRAINT `FK_Ingredienti_Folla` FOREIGN KEY (`follaID`) REFERENCES `folla`(`id`); /*Zutaten hat Foreignkey von Menge(id)*/ + +ALTER TABLE `elenco` + ADD CONSTRAINT `FK_Elenco_Utente` FOREIGN KEY (`utenteId`) REFERENCES `utente`(`id`); /*Liste hat Foreignkey von Benutzer(id)*/ + + + + + + +/*Test Data*/ +INSERT INTO `utente` (email, parolaDordine, nomeUtente) VALUES ('test@test.com', 'password', "testuser"); +INSERT INTO `utente` (email, parolaDordine, nomeUtente) VALUES ('test1@test.com', 'password1', "testuser1"); + + +INSERT INTO `folla` (unita) VALUES ('st'); +INSERT INTO `folla` (unita) VALUES ('g'); +INSERT INTO `folla` (unita) VALUES ('kg'); +INSERT INTO `folla` (unita) VALUES ('mg'); +INSERT INTO `folla` (unita) VALUES ('l'); +INSERT INTO `folla` (unita) VALUES ('ml'); +INSERT INTO `folla` (unita) VALUES ('TL'); +INSERT INTO `folla` (unita) VALUES ('EL'); + +INSERT INTO `ingredienti` (cognome, calorie, quantita, prezzo, follaID) VALUES ('Raffinierter Zucker', 3870, 1000, 5, 2); +INSERT INTO `ingredienti` (cognome, calorie, quantita, prezzo, follaID) VALUES ('Die Chips von der Tanke', 843, 375, 4.30, 2); + +INSERT INTO `elenco` (utenteID, cognome) VALUES (1, 'Liste den Einkauf bei Netto'); + +INSERT INTO `elencoIngredienti` (ingredientiID, elencoID) VALUES (1, 1); +INSERT INTO `elencoIngredienti` (ingredientiID, elencoID) VALUES (2, 1); -- 2.39.5 From e465ed9e9507dafc4bf4a7c99fb61326f0fdbb33 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Mon, 23 Jan 2023 05:34:13 +0100 Subject: [PATCH 12/18] add: Documentation --- Documentation/Dokumentation.md | 132 +++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 Documentation/Dokumentation.md diff --git a/Documentation/Dokumentation.md b/Documentation/Dokumentation.md new file mode 100644 index 0000000..4c63aef --- /dev/null +++ b/Documentation/Dokumentation.md @@ -0,0 +1,132 @@ +# Dokumentation + +Eine auflistung von allen Funktionen und API-Endpunkten mit Erklärung. + +- User +- Einkaufsliste +- Zutaten +- Einheiten + +--- + +## User + +### User erstellen +```shell +POST 127.0.0.1/DirektiveDesDons/user + +BODY +{ + "username": "Benutzername", + "email": "email" + "password": "Passwort", +} +``` + +### Aktueller User ausgeben +```shell +GET 127.0.0.1/DirektiveDesDons/user +``` + +### Daten des akteullen User ändern +```shell +PUT 127.0.0.1/DirektiveDesDons/user + +BODY +{ + "username": "Benutzername", + "password": "Passwort" +} +``` + +### Login +```shell +POST 127.0.0.1/DirektiveDesDons/login + +BODY +{ + "username": "Benutzername", + "password": "Passwort" +} +``` + +--- + +## Einkaufsliste + +### Einkaufsliste erstellen +```shell +POST 127.0.0.1/DirektiveDesDons/shoppinglist + +BODY +{ + "name": "Einkaufsliste 1" + "backgoundColor": "#123", +} +``` + +### Einkauslisten ausgeben +```shell +GET 127.0.0.1/DirektiveDesDons/shoppinglist +``` + +### Einkaufsliste ausgeben +```shell +GET 127.0.0.1/DirektiveDesDons/shoppinglist/1 +``` + +### Zutat zu Einkaufsliste hinzufügen +```shell +POST 127.0.0.1/DirektiveDesDons/shoppinglist/1 + +BODY +{ + "incredientId": 1 +} +``` + +--- + +## Zutaten + +### Zutaten erstellen +```shell +POST 127.0.0.1/DirektiveDesDons/ingredient + +BODY +{ + "name": "Nüsse", + "calories": "123", + "quantity": "5", + "unit": "st", + "price": "12.5" +} +``` + +### Zutaten ausgeben +```shell +GET 127.0.0.1/DirektiveDesDons/ingredient +``` + +### Zutat ausgeben +```shell +GET 127.0.0.1/DirektiveDesDons/ingredient/1 +``` + +--- + +## MengenEinheiten + +### Einheiten auflisten +```shell +GET 127.0.0.1/DirektiveDesDons/unit +``` +### Einheit Erstellen +```shell +POST 127.0.0.1/DirektiveDesDons/unit + +BODY +{ + "name": "lbs" +} +``` \ No newline at end of file -- 2.39.5 From 9a00e7eb163a887913f657168a73bf6a875b9d43 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Tue, 24 Jan 2023 04:05:15 +0100 Subject: [PATCH 13/18] add: update and delete ingredients --- index.php | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/index.php b/index.php index f5aee5d..b946653 100644 --- a/index.php +++ b/index.php @@ -113,11 +113,37 @@ $app->route("/ingredient") $res->json(["message" => "New ingredient has been listed", "ingredient" => $db->select("ingredienti", ["id" => $id])]); }); -$app->get("/ingredient/:id", function (array $req, Response $res) use ($db) { - $id = $req["params"]["id"]; - $ingredient = $db->select("ingredienti", ["id" => $id])[0]; - $res->json(["data" => $ingredient]); -}); +$app->route("/ingredient/:id") + ->get(function (array $req, Response $res) use ($db) { + $id = $req["params"]["id"]; + $ingredient = $db->select("ingredienti", ["id" => $id])[0]; + $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 -- 2.39.5 From 9ca46b9b3e31889769473a2acecd03887b8f3567 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Tue, 24 Jan 2023 04:10:30 +0100 Subject: [PATCH 14/18] add: update and delete units --- index.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/index.php b/index.php index b946653..685aa0c 100644 --- a/index.php +++ b/index.php @@ -169,11 +169,28 @@ $app->route("/unit") $newUnitId = $db->insert("folla", ["unita" => $name]); $res->json(["message" => "Unit: '" . $name . "' created", "data" => $db->select("folla", ["id" => $newUnitId])[0]], HTTP_STATUS_CODE::CREATED); }); -$app->get("/unit/:id", function (array $req, Response $res) use ($db) { - $id = $req["params"]["id"]; - $ingredient = $db->select("folla", ["id" => $id])[0]; - $res->json(["data" => $ingredient]); -}); +$app->route("/unit/:id") + ->get(function (array $req, Response $res) use ($db) { + $id = $req["params"]["id"]; + $ingredient = $db->select("folla", ["id" => $id])[0]; + $res->json(["data" => $ingredient]); + }) + ->put(function (array $req, Response $res) use ($db) { + $id = $req["params"]["id"]; + $name = $req["body"]["name"]; + + if (!isset($name) || strlen($name) < 1 || strlen($name) > 200) { + $res->json(["message" => "Invalid Request. Please follow the Documentation", HTTP_STATUS_CODE::BAD_REQUEST]); + return; + } + $db->update("folla", $id, ["unita" => $name]); + $res->json(["message" => "Unit has been updated", "data" => $db->select("folla", ["id" => $id])]); + }) + ->delete(function (array $req, Response $res) use ($db) { + $id = $req["params"]["id"]; + $db->delete("folla", $id); + $res->json(["message" => "Unit has been deleted"]); + }); /* * List -- 2.39.5 From b2ce4f829d101d521fe59e9f4502af0e34740621 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Tue, 24 Jan 2023 08:47:05 +0100 Subject: [PATCH 15/18] add: add items to list --- Documentation/Dokumentation.md | 2 +- index.php | 69 ++++++++++++++++++++++++++-------- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/Documentation/Dokumentation.md b/Documentation/Dokumentation.md index 4c63aef..98c455e 100644 --- a/Documentation/Dokumentation.md +++ b/Documentation/Dokumentation.md @@ -81,7 +81,7 @@ POST 127.0.0.1/DirektiveDesDons/shoppinglist/1 BODY { - "incredientId": 1 + "ingredientId": 1 } ``` diff --git a/index.php b/index.php index 685aa0c..191819a 100644 --- a/index.php +++ b/index.php @@ -135,7 +135,7 @@ $app->route("/ingredient/:id") } $unitId = $unitInTable[0]["id"]; - $db->update("ingredienti", $id,["cognome" => "$name", "calorie" => "$calories", "quantita" => "$quantity", "follaID" => $unitId, "prezzo" => "$price"]); + $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]]); }) @@ -229,23 +229,60 @@ $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) { - $id = $req["params"]["id"]; - $list = $db->select("elenco", ["id" => $id, "utenteID" => $req["user"]->id])[0]; +$app->route("/list/:id") + ->get(function (array $req, Response $res) use ($db) { + $id = $req["params"]["id"]; + $query = $db->select("elenco", ["id" => $id, "utenteID" => $req["user"]->id]); - $listId = $list["id"]; - $ingredients = $db->select("elencoIngredienti", ["elencoId" => $listId]); - $ingredientData = []; - foreach ($ingredients as &$ingredient) { - $ingredientData[] = $db->select("Ingredienti", ["id" => $ingredient["ingredientiID"]]); - } - $list["inredients"] = $ingredientData; + if (count($query) < 1) { + $res->json(["message" => "List does not exists or you dont have permissions to view it"]); + return; + } + $list = $query[0]; - $res->json(["data" => $list]); -}); -$app->post("/list/:id", function (array $req, Response $res) use ($db) { - // TODO: add Item to List with id -}); + $listId = $list["id"]; + $ingredients = $db->select("elencoIngredienti", ["elencoId" => $listId]); + $ingredientData = []; + foreach ($ingredients as &$ingredient) { + $ingredientData[] = $db->select("Ingredienti", ["id" => $ingredient["ingredientiID"]]); + } + $list["inredients"] = $ingredientData; + + $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"]; + + 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 }); -- 2.39.5 From c8582c8813826e8debabdf38899284d0c2b63ffc Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Tue, 24 Jan 2023 09:01:23 +0100 Subject: [PATCH 16/18] add: delete items from list --- BancaDati/BancaDati.php | 8 +++++--- index.php | 13 +++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) 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 -- 2.39.5 From 62ac1011cc2fe3670d9683948bf444668495c4fe Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Tue, 24 Jan 2023 09:01:42 +0100 Subject: [PATCH 17/18] update documentation --- Documentation/Dokumentation.md | 40 +++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/Documentation/Dokumentation.md b/Documentation/Dokumentation.md index 98c455e..e34181e 100644 --- a/Documentation/Dokumentation.md +++ b/Documentation/Dokumentation.md @@ -28,17 +28,6 @@ BODY GET 127.0.0.1/DirektiveDesDons/user ``` -### Daten des akteullen User ändern -```shell -PUT 127.0.0.1/DirektiveDesDons/user - -BODY -{ - "username": "Benutzername", - "password": "Passwort" -} -``` - ### Login ```shell POST 127.0.0.1/DirektiveDesDons/login @@ -56,7 +45,7 @@ BODY ### Einkaufsliste erstellen ```shell -POST 127.0.0.1/DirektiveDesDons/shoppinglist +POST 127.0.0.1/DirektiveDesDons/list BODY { @@ -67,17 +56,22 @@ BODY ### Einkauslisten ausgeben ```shell -GET 127.0.0.1/DirektiveDesDons/shoppinglist +GET 127.0.0.1/DirektiveDesDons/list ``` ### Einkaufsliste ausgeben ```shell -GET 127.0.0.1/DirektiveDesDons/shoppinglist/1 +GET 127.0.0.1/DirektiveDesDons/list/1 +``` + +### Einkaufsliste löschen +```shell +DELETE 127.0.0.1/DirektiveDesDons/list/1 ``` ### Zutat zu Einkaufsliste hinzufügen ```shell -POST 127.0.0.1/DirektiveDesDons/shoppinglist/1 +POST 127.0.0.1/DirektiveDesDons/list/1 BODY { @@ -85,6 +79,11 @@ BODY } ``` +### Zutat von Liste löschen +```shell +DELETE 127.0.0.1/DirektiveDesDons/list/1/4 +``` + --- ## Zutaten @@ -113,6 +112,11 @@ GET 127.0.0.1/DirektiveDesDons/ingredient GET 127.0.0.1/DirektiveDesDons/ingredient/1 ``` +### Zutat löschen +```shell +DELETE 127.0.0.1/DirektiveDesDons/ingredient/1 +``` + --- ## MengenEinheiten @@ -121,6 +125,7 @@ GET 127.0.0.1/DirektiveDesDons/ingredient/1 ```shell GET 127.0.0.1/DirektiveDesDons/unit ``` + ### Einheit Erstellen ```shell POST 127.0.0.1/DirektiveDesDons/unit @@ -129,4 +134,9 @@ BODY { "name": "lbs" } +``` + +### Einheit löschen +```shell +DELETE 127.0.0.1/DirektiveDesDons/unit/4 ``` \ No newline at end of file -- 2.39.5 From b607934c8da0ec20ac45f51e6560d6041e352e79 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Tue, 24 Jan 2023 09:01:58 +0100 Subject: [PATCH 18/18] db pw --- BancaDati/BancaDati.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BancaDati/BancaDati.php b/BancaDati/BancaDati.php index 801d5cd..24338bb 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 = ""; + private $pw = "root"; public $pdo; public function __construct() { -- 2.39.5