done #3

Merged
PBS2H21ASH merged 19 commits from done into don 2023-01-24 20:11:21 +01:00
Showing only changes of commit e8775385cf - Show all commits

View File

@ -11,8 +11,7 @@ use Router\Router;
use Router\Route; use Router\Route;
use BancaDati\BancaDati; use BancaDati\BancaDati;
abstract class HTTP_STATUS_CODE abstract class HTTP_STATUS_CODE {
{
const OK = 200; const OK = 200;
const CREATED = 201; const CREATED = 201;
const BAD_REQUEST = 400; const BAD_REQUEST = 400;
@ -29,9 +28,9 @@ $db = new BancaDati();
* Middleware * Middleware
*/ */
$app->use("/", function (array &$req, Response $res) { $app->use("/", function (array &$req, Response $res) {
if(isset($_COOKIE["TOKEN"])){ if (isset($_COOKIE["TOKEN"])) {
$user = new User(); $user = new User();
if($user->loginWithToken($_COOKIE["TOKEN"])){ if ($user->loginWithToken($_COOKIE["TOKEN"])) {
$req["user"] = $user; $req["user"] = $user;
} }
} }
@ -41,17 +40,18 @@ $app->use("/", function (array &$req, Response $res) {
* Home * Home
*/ */
$app->get("/", function (array $req, Response $res) use ($db) { $app->get("/", function (array $req, Response $res) use ($db) {
$res->send("Hello World"); $res->json(["message" => "",
//var_dump($req["user"]); "name" => "Shopping List",
"authors" => "Simon Bock, Johannes Kantz & Malte Schulze Hobeling"]);
}); });
/* /*
* User * User
*/ */
$app->get("/user", function (array $req, Response $res) { $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]); $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"]); $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); $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"]; $username = $req["body"]["username"];
$password = $req["body"]["password"]; $password = $req["body"]["password"];
$user = new User(); $user = new User();
$usertoken = $user->loginWithUsername($username, $password); $usertoken = $user->loginWithUsername($username, $password);
if($usertoken){ if ($usertoken) {
setcookie("TOKEN", $usertoken, time()+3600); // 1h setcookie("TOKEN", $usertoken, time() + 3600); // 1h
$res->send("Login successful" . "token: " . $usertoken); $res->json(["message" => "Login successful", "token" => $usertoken]);
}else{ } else {
$res->send("Login failed", HTTP_STATUS_CODE::FORBIDDEN); $res->json(["message" => "Login failed"], HTTP_STATUS_CODE::FORBIDDEN);
} }
}); });
@ -98,14 +98,14 @@ $app->route("/ingredient")
$price = $req["body"]["price"]; $price = $req["body"]["price"];
$unitInTable = $db->select("folla", ["unita" => $unit]); $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); $res->json(["message" => "Unit: " . $unit . " does not exist. Please create unit first"], HTTP_STATUS_CODE::BAD_REQUEST);
return; return;
} }
$unitId = $unitInTable[0]["id"]; $unitId = $unitInTable[0]["id"];
$id = $db->insert("ingredienti", ["cognome" => "$name", "calorie" => "$calories", "quantita" => "$quantity", "follaID" => $unitId, "prezzo" => "$price"]); $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); $res->json(["message" => "Something went wrong when creating the Ingredient"], HTTP_STATUS_CODE::BAD_REQUEST);
return; return;
} }
@ -129,14 +129,14 @@ $app->route("/unit")
->post(function (array $req, Response $res) use ($db) { ->post(function (array $req, Response $res) use ($db) {
$name = $req["body"]["name"]; $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]); $res->json(["message" => "Invalid Request. Please follow the Documentation", HTTP_STATUS_CODE::BAD_REQUEST]);
return; return;
} }
$selectWithSameName = $db->select("folla", ["unita" => $name]); $selectWithSameName = $db->select("folla", ["unita" => $name]);
if(count($selectWithSameName) >= 1){ if (count($selectWithSameName) >= 1) {
$res->json(["message" => "Unit: " . $name ." already exists", "data" => $selectWithSameName[0]], HTTP_STATUS_CODE::BAD_REQUEST); $res->json(["message" => "Unit: " . $name . " already exists", "data" => $selectWithSameName[0]], HTTP_STATUS_CODE::BAD_REQUEST);
return; return;
} }
@ -153,7 +153,7 @@ $app->get("/unit/:id", function (array $req, Response $res) use ($db) {
* List * List
*/ */
$app->use("/list", function (array $req, Response $res) { $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); $res->json(["message" => "You need to be signed in to use lists"], HTTP_STATUS_CODE::FORBIDDEN);
die; die;
} }
@ -163,11 +163,11 @@ $app->route("/list")
->get(function (array $req, Response $res) use ($db) { ->get(function (array $req, Response $res) use ($db) {
$lists = $db->select("elenco", ["utenteID" => $req["user"]->id]); $lists = $db->select("elenco", ["utenteID" => $req["user"]->id]);
// add items to list // add items to list
foreach ($lists as &$list){ foreach ($lists as &$list) {
$listId = $list["id"]; $listId = $list["id"];
$ingredients = $db->select("elencoIngredienti", ["elencoId" => $listId]); $ingredients = $db->select("elencoIngredienti", ["elencoId" => $listId]);
$ingredientData = []; $ingredientData = [];
foreach ($ingredients as &$ingredient){ foreach ($ingredients as &$ingredient) {
$ingredientData[] = $db->select("Ingredienti", ["id" => $ingredient["ingredientiID"]]); $ingredientData[] = $db->select("Ingredienti", ["id" => $ingredient["ingredientiID"]]);
} }
$list["inredients"] = $ingredientData; $list["inredients"] = $ingredientData;
@ -178,7 +178,7 @@ $app->route("/list")
$name = $req["body"]["name"]; $name = $req["body"]["name"];
$bgColor = $req["body"]["backgoundColor"] ?? "#fff"; $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); $res->json(["message" => "Invalid Request. Please follow the Documentation"], HTTP_STATUS_CODE::BAD_REQUEST);
return; return;
} }
@ -193,7 +193,7 @@ $app->get("/list/:id", function (array $req, Response $res) use ($db) {
$listId = $list["id"]; $listId = $list["id"];
$ingredients = $db->select("elencoIngredienti", ["elencoId" => $listId]); $ingredients = $db->select("elencoIngredienti", ["elencoId" => $listId]);
$ingredientData = []; $ingredientData = [];
foreach ($ingredients as &$ingredient){ foreach ($ingredients as &$ingredient) {
$ingredientData[] = $db->select("Ingredienti", ["id" => $ingredient["ingredientiID"]]); $ingredientData[] = $db->select("Ingredienti", ["id" => $ingredient["ingredientiID"]]);
} }
$list["inredients"] = $ingredientData; $list["inredients"] = $ingredientData;