2022-12-08 14:26:04 +01:00
|
|
|
<?php
|
2022-12-08 14:43:05 +01:00
|
|
|
|
|
|
|
require_once("Router/Router.php");
|
|
|
|
require_once("Router/Response.php");
|
2023-01-23 00:33:56 +01:00
|
|
|
require_once("Router/Route.php");
|
2022-12-21 10:14:44 +01:00
|
|
|
require_once("BancaDati/BancaDati.php");
|
2023-01-11 14:41:58 +01:00
|
|
|
require_once("User.php");
|
2022-12-08 14:43:05 +01:00
|
|
|
|
|
|
|
use Router\Response;
|
|
|
|
use Router\Router;
|
2023-01-23 00:33:56 +01:00
|
|
|
use Router\Route;
|
2022-12-21 10:14:44 +01:00
|
|
|
use BancaDati\BancaDati;
|
2022-12-08 14:43:05 +01:00
|
|
|
|
2023-01-23 05:31:35 +01:00
|
|
|
abstract class HTTP_STATUS_CODE {
|
2023-01-23 01:12:43 +01:00
|
|
|
const OK = 200;
|
|
|
|
const CREATED = 201;
|
|
|
|
const BAD_REQUEST = 400;
|
|
|
|
const FORBIDDEN = 403;
|
|
|
|
const NOT_FOUNT = 404;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-08 14:43:05 +01:00
|
|
|
$app = new Router("/DirektiveDesDons");
|
2022-12-21 10:26:12 +01:00
|
|
|
$db = new BancaDati();
|
2022-12-08 14:43:05 +01:00
|
|
|
|
2023-01-23 05:26:12 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Middleware
|
|
|
|
*/
|
2023-01-12 15:22:32 +01:00
|
|
|
$app->use("/", function (array &$req, Response $res) {
|
2023-01-23 05:31:35 +01:00
|
|
|
if (isset($_COOKIE["TOKEN"])) {
|
2023-01-12 15:22:32 +01:00
|
|
|
$user = new User();
|
2023-01-23 05:31:35 +01:00
|
|
|
if ($user->loginWithToken($_COOKIE["TOKEN"])) {
|
2023-01-12 15:29:58 +01:00
|
|
|
$req["user"] = $user;
|
|
|
|
}
|
2023-01-11 14:41:58 +01:00
|
|
|
}
|
|
|
|
});
|
2022-12-08 14:43:05 +01:00
|
|
|
|
2023-01-23 05:26:12 +01:00
|
|
|
/*
|
|
|
|
* Home
|
|
|
|
*/
|
2023-01-23 00:33:56 +01:00
|
|
|
$app->get("/", function (array $req, Response $res) use ($db) {
|
2023-01-25 08:02:59 +01:00
|
|
|
$res->json(["message" => "Die Einkaufsliste des Don",
|
2023-01-23 05:31:35 +01:00
|
|
|
"name" => "Shopping List",
|
|
|
|
"authors" => "Simon Bock, Johannes Kantz & Malte Schulze Hobeling"]);
|
2022-12-08 14:43:05 +01:00
|
|
|
});
|
|
|
|
|
2023-01-23 00:33:56 +01:00
|
|
|
/*
|
|
|
|
* User
|
|
|
|
*/
|
2023-01-25 08:27:03 +01:00
|
|
|
$app->route("/user")
|
|
|
|
->get(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" => HTTP_STATUS_CODE::FORBIDDEN, "message" => "You are not logged in. Goto '/login' to login"]);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
->post(function (array $req, Response $res) use ($db) {
|
|
|
|
$newUsername = $req["body"]["username"];
|
|
|
|
$newPassword = $req["body"]["password"];
|
|
|
|
$newEmail = $req["body"]["email"];
|
|
|
|
$db->insert("utente", ["email" => "$newEmail", "parolaDordine" => "$newPassword", "nomeUtente" => "$newUsername"]);
|
2022-12-08 14:52:20 +01:00
|
|
|
|
2023-01-25 08:27:03 +01:00
|
|
|
$res->send("Account Created", HTTP_STATUS_CODE::CREATED);
|
|
|
|
});
|
2023-01-23 05:31:35 +01:00
|
|
|
$app->post("/login", function (array $req, Response $res) use ($db) {
|
2023-01-11 14:41:58 +01:00
|
|
|
$username = $req["body"]["username"];
|
2023-01-12 14:44:52 +01:00
|
|
|
$password = $req["body"]["password"];
|
|
|
|
|
|
|
|
$user = new User();
|
2023-01-12 15:04:15 +01:00
|
|
|
$usertoken = $user->loginWithUsername($username, $password);
|
2023-01-11 14:41:58 +01:00
|
|
|
|
2023-01-23 05:31:35 +01:00
|
|
|
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);
|
2023-01-11 14:41:58 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-23 00:33:56 +01:00
|
|
|
/*
|
|
|
|
* Ingredients
|
|
|
|
*/
|
|
|
|
$app->route("/ingredient")
|
|
|
|
->get(function (array $req, Response $res) use ($db) {
|
2023-01-23 01:12:43 +01:00
|
|
|
$res->json(["data" => $db->select("ingredienti")]);
|
2023-01-23 00:33:56 +01:00
|
|
|
})
|
|
|
|
->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"];
|
2023-01-19 14:27:30 +01:00
|
|
|
|
2023-01-23 00:33:56 +01:00
|
|
|
$unitInTable = $db->select("folla", ["unita" => $unit]);
|
2023-01-23 05:31:35 +01:00
|
|
|
if (count($unitInTable) > 1) {
|
2023-01-23 01:12:43 +01:00
|
|
|
$res->json(["message" => "Unit: " . $unit . " does not exist. Please create unit first"], HTTP_STATUS_CODE::BAD_REQUEST);
|
2023-01-23 00:33:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$unitId = $unitInTable[0]["id"];
|
2023-01-19 14:27:30 +01:00
|
|
|
|
2023-01-23 00:33:56 +01:00
|
|
|
$id = $db->insert("ingredienti", ["cognome" => "$name", "calorie" => "$calories", "quantita" => "$quantity", "follaID" => $unitId, "prezzo" => "$price"]);
|
2023-01-23 05:31:35 +01:00
|
|
|
if (!$id) {
|
2023-01-23 01:12:43 +01:00
|
|
|
$res->json(["message" => "Something went wrong when creating the Ingredient"], HTTP_STATUS_CODE::BAD_REQUEST);
|
2023-01-23 00:33:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-01-19 14:36:32 +01:00
|
|
|
|
2023-01-23 01:12:43 +01:00
|
|
|
$res->json(["message" => "New ingredient has been listed",
|
2023-01-23 00:33:56 +01:00
|
|
|
"ingredient" => $db->select("ingredienti", ["id" => $id])]);
|
|
|
|
});
|
2023-01-24 04:05:15 +01:00
|
|
|
$app->route("/ingredient/:id")
|
|
|
|
->get(function (array $req, Response $res) use ($db) {
|
|
|
|
$id = $req["params"]["id"];
|
2023-01-25 08:02:59 +01:00
|
|
|
$query = $db->select("ingredienti", ["id" => $id]);
|
|
|
|
if(count($query) < 1){
|
|
|
|
$res->json(["message" => "Item does not exists"]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$ingredient = $query[0];
|
2023-01-24 04:05:15 +01:00
|
|
|
$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"];
|
|
|
|
|
2023-01-24 08:47:05 +01:00
|
|
|
$db->update("ingredienti", $id, ["cognome" => "$name", "calorie" => "$calories", "quantita" => "$quantity", "follaID" => $unitId, "prezzo" => "$price"]);
|
2023-01-24 04:05:15 +01:00
|
|
|
|
|
|
|
$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"]);
|
|
|
|
});
|
2023-01-19 14:36:32 +01:00
|
|
|
|
2023-01-23 01:12:43 +01:00
|
|
|
/*
|
|
|
|
* 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"];
|
|
|
|
|
2023-01-23 05:31:35 +01:00
|
|
|
if (!isset($name) || strlen($name) < 1 || strlen($name) > 200) {
|
2023-01-23 05:26:12 +01:00
|
|
|
$res->json(["message" => "Invalid Request. Please follow the Documentation", HTTP_STATUS_CODE::BAD_REQUEST]);
|
2023-01-23 01:12:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$selectWithSameName = $db->select("folla", ["unita" => $name]);
|
2023-01-23 05:31:35 +01:00
|
|
|
if (count($selectWithSameName) >= 1) {
|
|
|
|
$res->json(["message" => "Unit: " . $name . " already exists", "data" => $selectWithSameName[0]], HTTP_STATUS_CODE::BAD_REQUEST);
|
2023-01-23 01:12:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$newUnitId = $db->insert("folla", ["unita" => $name]);
|
|
|
|
$res->json(["message" => "Unit: '" . $name . "' created", "data" => $db->select("folla", ["id" => $newUnitId])[0]], HTTP_STATUS_CODE::CREATED);
|
|
|
|
});
|
2023-01-24 04:10:30 +01:00
|
|
|
$app->route("/unit/:id")
|
|
|
|
->get(function (array $req, Response $res) use ($db) {
|
|
|
|
$id = $req["params"]["id"];
|
2023-01-25 08:02:59 +01:00
|
|
|
$query = $db->select("folla", ["id" => $id]);
|
|
|
|
if(count($query) < 1){
|
|
|
|
$res->json(["message" => "Unit does not exists"]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$unit = $query[0];
|
|
|
|
$res->json(["data" => $unit]);
|
2023-01-24 04:10:30 +01:00
|
|
|
})
|
|
|
|
->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"]);
|
|
|
|
});
|
2023-01-23 00:33:56 +01:00
|
|
|
|
2023-01-23 05:26:12 +01:00
|
|
|
/*
|
|
|
|
* List
|
|
|
|
*/
|
|
|
|
$app->use("/list", function (array $req, Response $res) {
|
2023-01-23 05:31:35 +01:00
|
|
|
if (!isset($req["user"])) {
|
2023-01-23 05:26:12 +01:00
|
|
|
$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
|
2023-01-23 05:31:35 +01:00
|
|
|
foreach ($lists as &$list) {
|
2023-01-23 05:26:12 +01:00
|
|
|
$listId = $list["id"];
|
|
|
|
$ingredients = $db->select("elencoIngredienti", ["elencoId" => $listId]);
|
|
|
|
$ingredientData = [];
|
2023-01-23 05:31:35 +01:00
|
|
|
foreach ($ingredients as &$ingredient) {
|
2023-01-23 05:26:12 +01:00
|
|
|
$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";
|
|
|
|
|
2023-01-23 05:31:35 +01:00
|
|
|
if (!isset($name) || strlen($name) < 1) {
|
2023-01-23 05:26:12 +01:00
|
|
|
$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);
|
|
|
|
});
|
2023-01-24 08:47:05 +01:00
|
|
|
$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]);
|
2023-01-23 05:26:12 +01:00
|
|
|
|
2023-01-24 08:47:05 +01:00
|
|
|
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]);
|
|
|
|
$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) {
|
|
|
|
$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"]);
|
|
|
|
});
|
2023-01-23 05:26:12 +01:00
|
|
|
$app->delete("/list/:id/:item", function (array $req, Response $res) use ($db) {
|
2023-01-24 09:01:23 +01:00
|
|
|
$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"]);
|
2023-01-23 05:26:12 +01:00
|
|
|
});
|
|
|
|
|
2022-12-08 14:43:05 +01:00
|
|
|
$app->start();
|