add: add items to list
This commit is contained in:
		| @@ -81,7 +81,7 @@ POST 127.0.0.1/DirektiveDesDons/shoppinglist/1 | |||||||
|  |  | ||||||
| BODY | BODY | ||||||
| { | { | ||||||
|   "incredientId": 1 |   "ingredientId": 1 | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										69
									
								
								index.php
									
									
									
									
									
								
							
							
						
						
									
										69
									
								
								index.php
									
									
									
									
									
								
							| @@ -135,7 +135,7 @@ $app->route("/ingredient/:id") | |||||||
|         } |         } | ||||||
|         $unitId = $unitInTable[0]["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]]); |         $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]); |         $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); |         $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") | ||||||
|     $id = $req["params"]["id"]; |     ->get(function (array $req, Response $res) use ($db) { | ||||||
|     $list = $db->select("elenco", ["id" => $id, "utenteID" => $req["user"]->id])[0]; |         $id = $req["params"]["id"]; | ||||||
|  |         $query = $db->select("elenco", ["id" => $id, "utenteID" => $req["user"]->id]); | ||||||
|  |  | ||||||
|     $listId = $list["id"]; |         if (count($query) < 1) { | ||||||
|     $ingredients = $db->select("elencoIngredienti", ["elencoId" => $listId]); |             $res->json(["message" => "List does not exists or you dont have permissions to view it"]); | ||||||
|     $ingredientData = []; |             return; | ||||||
|     foreach ($ingredients as &$ingredient) { |         } | ||||||
|         $ingredientData[] = $db->select("Ingredienti", ["id" => $ingredient["ingredientiID"]]); |         $list = $query[0]; | ||||||
|     } |  | ||||||
|     $list["inredients"] = $ingredientData; |  | ||||||
|  |  | ||||||
|     $res->json(["data" => $list]); |         $listId = $list["id"]; | ||||||
| }); |         $ingredients = $db->select("elencoIngredienti", ["elencoId" => $listId]); | ||||||
| $app->post("/list/:id", function (array $req, Response $res) use ($db) { |         $ingredientData = []; | ||||||
|     // TODO: add Item to List with id |         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) { | $app->delete("/list/:id/:item", function (array $req, Response $res) use ($db) { | ||||||
|     // TODO: delete item from list |     // TODO: delete item from list | ||||||
| }); | }); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Johannes Kantz
					Johannes Kantz