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 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;