done #3
60
index.php
60
index.php
@ -11,6 +11,16 @@ use Router\Router;
|
|||||||
use Router\Route;
|
use Router\Route;
|
||||||
use BancaDati\BancaDati;
|
use BancaDati\BancaDati;
|
||||||
|
|
||||||
|
abstract class HTTP_STATUS_CODE
|
||||||
|
{
|
||||||
|
const OK = 200;
|
||||||
|
const CREATED = 201;
|
||||||
|
const BAD_REQUEST = 400;
|
||||||
|
const FORBIDDEN = 403;
|
||||||
|
const NOT_FOUNT = 404;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
$app = new Router("/DirektiveDesDons");
|
$app = new Router("/DirektiveDesDons");
|
||||||
$db = new BancaDati();
|
$db = new BancaDati();
|
||||||
|
|
||||||
@ -31,12 +41,11 @@ $app->get("/", function (array $req, Response $res) use ($db) {
|
|||||||
/*
|
/*
|
||||||
* 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" => 403, "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"]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
$app->get("/user/:id", function (array $req, Response $res) use ($db) {
|
$app->get("/user/:id", function (array $req, Response $res) use ($db) {
|
||||||
@ -49,7 +58,7 @@ $app->post("/signup", function (array $req, Response $res) use ($db) {
|
|||||||
$newEmail = $req["body"]["email"];
|
$newEmail = $req["body"]["email"];
|
||||||
$db->insert("utente", ["email" => "$newEmail", "parolaDordine" => "$newPassword", "nomeUtente" => "$newUsername"]);
|
$db->insert("utente", ["email" => "$newEmail", "parolaDordine" => "$newPassword", "nomeUtente" => "$newUsername"]);
|
||||||
|
|
||||||
$res->send("Account Created", 201);
|
$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) {
|
||||||
@ -61,9 +70,9 @@ $app->post("/login", function( array $req, Response $res) use ($db) {
|
|||||||
|
|
||||||
if($usertoken){
|
if($usertoken){
|
||||||
setcookie("TOKEN", $usertoken, time()+3600); // 1h
|
setcookie("TOKEN", $usertoken, time()+3600); // 1h
|
||||||
$res->send("Login successful" . "token: " . $usertoken, 200);
|
$res->send("Login successful" . "token: " . $usertoken);
|
||||||
}else{
|
}else{
|
||||||
$res->send("Login failed", 403);
|
$res->send("Login failed", HTTP_STATUS_CODE::FORBIDDEN);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -72,7 +81,7 @@ $app->post("/login", function( array $req, Response $res) use ($db) {
|
|||||||
*/
|
*/
|
||||||
$app->route("/ingredient")
|
$app->route("/ingredient")
|
||||||
->get(function (array $req, Response $res) use ($db) {
|
->get(function (array $req, Response $res) use ($db) {
|
||||||
$res->json(["status" => 200, "data" => $db->select("ingredienti")]);
|
$res->json(["data" => $db->select("ingredienti")]);
|
||||||
})
|
})
|
||||||
->post(function (array $req, Response $res) use ($db) {
|
->post(function (array $req, Response $res) use ($db) {
|
||||||
$name = $req["body"]["name"];
|
$name = $req["body"]["name"];
|
||||||
@ -83,27 +92,54 @@ $app->route("/ingredient")
|
|||||||
|
|
||||||
$unitInTable = $db->select("folla", ["unita" => $unit]);
|
$unitInTable = $db->select("folla", ["unita" => $unit]);
|
||||||
if(count($unitInTable) > 1){
|
if(count($unitInTable) > 1){
|
||||||
$res->json(["status" => "400", "message" => "Unit: " . $unit . " does not exist. Please create unit first" ]);
|
$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(["status" => 400,
|
$res->json(["message" => "Something went wrong when creating the Ingredient"], HTTP_STATUS_CODE::BAD_REQUEST);
|
||||||
"message" => "Something went wrong when creating the Ingredient"], 500);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$res->json(["status" => "200",
|
$res->json(["message" => "New ingredient has been listed",
|
||||||
"message" => "New ingredient has been listed",
|
|
||||||
"ingredient" => $db->select("ingredienti", ["id" => $id])]);
|
"ingredient" => $db->select("ingredienti", ["id" => $id])]);
|
||||||
});
|
});
|
||||||
$app->get("/ingredient/:id", function (array $req, Response $res) use ($db) {
|
$app->get("/ingredient/:id", function (array $req, Response $res) use ($db) {
|
||||||
$id = $req["params"]["id"];
|
$id = $req["params"]["id"];
|
||||||
$ingredient = $db->select("ingredienti", ["id" => $id])[0];
|
$ingredient = $db->select("ingredienti", ["id" => $id])[0];
|
||||||
$res->json(["status" => 200, "data" => $ingredient]);
|
$res->json(["data" => $ingredient]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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"];
|
||||||
|
|
||||||
|
if(!isset($name) || strlen($name) < 1 || strlen($name) > 200){
|
||||||
|
$res->json(["message" => "Invalid Request. Please follow the 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);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$newUnitId = $db->insert("folla", ["unita" => $name]);
|
||||||
|
$res->json(["message" => "Unit: '" . $name . "' created", "data" => $db->select("folla", ["id" => $newUnitId])[0]], HTTP_STATUS_CODE::CREATED);
|
||||||
|
});
|
||||||
|
$app->get("/unit/:id", function (array $req, Response $res) use ($db) {
|
||||||
|
$id = $req["params"]["id"];
|
||||||
|
$ingredient = $db->select("folla", ["id" => $id])[0];
|
||||||
|
$res->json(["data" => $ingredient]);
|
||||||
|
});
|
||||||
|
|
||||||
$app->start();
|
$app->start();
|
Loading…
Reference in New Issue
Block a user