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
|
|
|
|
|
|
|
$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-12 15:22:32 +01:00
|
|
|
$app->use("/", function (array &$req, Response $res) {
|
2023-01-11 14:41:58 +01:00
|
|
|
if(isset($_COOKIE["TOKEN"])){
|
2023-01-12 15:22:32 +01:00
|
|
|
$user = new User();
|
2023-01-12 15:29:58 +01:00
|
|
|
if($user->loginWithToken($_COOKIE["TOKEN"])){
|
|
|
|
$req["user"] = $user;
|
|
|
|
}
|
2023-01-11 14:41:58 +01:00
|
|
|
}
|
|
|
|
});
|
2022-12-08 14:43:05 +01:00
|
|
|
|
2023-01-23 00:33:56 +01:00
|
|
|
$app->get("/", function (array $req, Response $res) use ($db) {
|
2022-12-08 14:43:05 +01:00
|
|
|
$res->send("Hello World");
|
2023-01-12 15:29:58 +01:00
|
|
|
//var_dump($req["user"]);
|
2022-12-08 14:43:05 +01:00
|
|
|
});
|
|
|
|
|
2023-01-23 00:33:56 +01:00
|
|
|
/*
|
|
|
|
* User
|
|
|
|
*/
|
|
|
|
|
2022-12-08 14:52:20 +01:00
|
|
|
$app->get("/user", function (array $req, Response $res) {
|
2023-01-23 00:33:56 +01:00
|
|
|
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"]);
|
|
|
|
}
|
2022-12-08 14:52:20 +01:00
|
|
|
});
|
2023-01-12 14:44:52 +01:00
|
|
|
$app->get("/user/:id", function (array $req, Response $res) use ($db) {
|
|
|
|
$db->select("utente", ["username" => $req["id"]]);
|
2022-12-21 10:14:44 +01:00
|
|
|
$res->send("user " . $req["params"]["id"]);
|
|
|
|
});
|
2023-01-23 00:33:56 +01:00
|
|
|
$app->post("/signup", function (array $req, Response $res) use ($db) {
|
2023-01-18 09:51:43 +01:00
|
|
|
$newUsername = $req["body"]["username"];
|
|
|
|
$newPassword = $req["body"]["password"];
|
|
|
|
$newEmail = $req["body"]["email"];
|
|
|
|
$db->insert("utente", ["email" => "$newEmail", "parolaDordine" => "$newPassword", "nomeUtente" => "$newUsername"]);
|
|
|
|
|
2023-01-23 00:33:56 +01:00
|
|
|
$res->send("Account Created", 201);
|
2023-01-12 14:44:52 +01:00
|
|
|
});
|
2022-12-08 14:52:20 +01:00
|
|
|
|
2023-01-12 14:44:52 +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-12 15:04:15 +01:00
|
|
|
if($usertoken){
|
|
|
|
setcookie("TOKEN", $usertoken, time()+3600); // 1h
|
|
|
|
$res->send("Login successful" . "token: " . $usertoken, 200);
|
2023-01-11 14:41:58 +01:00
|
|
|
}else{
|
|
|
|
$res->send("Login failed", 403);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-23 00:33:56 +01:00
|
|
|
/*
|
|
|
|
* 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"];
|
2023-01-19 14:27:30 +01:00
|
|
|
|
2023-01-23 00:33:56 +01:00
|
|
|
$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"];
|
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"]);
|
|
|
|
if(!$id){
|
|
|
|
$res->json(["status" => 400,
|
|
|
|
"message" => "Something went wrong when creating the Ingredient"], 500);
|
|
|
|
return;
|
|
|
|
}
|
2023-01-19 14:36:32 +01:00
|
|
|
|
2023-01-23 00:33:56 +01:00
|
|
|
$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]);
|
2023-01-19 14:36:32 +01:00
|
|
|
});
|
|
|
|
|
2023-01-23 00:33:56 +01:00
|
|
|
|
2022-12-08 14:43:05 +01:00
|
|
|
$app->start();
|