DirektiveDesDons/index.php
2023-01-19 14:36:32 +01:00

78 lines
2.5 KiB
PHP

<?php
require_once("Router/Router.php");
require_once("Router/Response.php");
require_once("BancaDati/BancaDati.php");
require_once("User.php");
use Router\Response;
use Router\Router;
use BancaDati\BancaDati;
$app = new Router("/DirektiveDesDons");
$db = new BancaDati();
$app->use("/", function (array &$req, Response $res) {
if(isset($_COOKIE["TOKEN"])){
$user = new User();
if($user->loginWithToken($_COOKIE["TOKEN"])){
$req["user"] = $user;
}
}
});
$app->get("/", function (array $req, Response $res) {
$res->send("Hello World");
//var_dump($req["user"]);
});
$app->get("/user", function (array $req, Response $res) {
$res->send("user");
});
$app->get("/user/:id", function (array $req, Response $res) use ($db) {
$db->select("utente", ["username" => $req["id"]]);
$res->send("user " . $req["params"]["id"]);
});
$app->post("/createuser", function (array $req, Response $res) use ($db) {
$newUsername = $req["body"]["username"];
$newPassword = $req["body"]["password"];
$newEmail = $req["body"]["email"];
// $db->insert("utente", ["email" => "test@email.com", "parolaDordine" => "password", "nomeUtente" => "testuser"]);
$db->insert("utente", ["email" => "$newEmail", "parolaDordine" => "$newPassword", "nomeUtente" => "$newUsername"]);
$res->send("user ");
});
$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, 200);
}else{
$res->send("Login failed", 403);
}
});
$app->post("/createingredients", function (array $req, Response $res) use ($db) {
$newIngredient = $req["body"]["ingredient"];
$newCalorie = $req["body"]["calories"];
$newWeight = $req["body"]["weight"];
$newPrice = $req["body"]["price"];
$db->insert("ingredienti", ["cognome" => "$newIngredient", "caloriePerCento" => "$newCalorie", "ilPeso" => "$newWeight", "prezzo" => "$newPrice"]);
$res->send("New ingredient has been listed ");
});
$app->post("/createunits", function (array $req, Response $res) use ($db) {
$newUnit = $req["body"]["unit"];
$db->insert("folla", ["unita" => "$newUnit"]);
$res->send("New unit has been listed ");
});
$app->start();