add: Ingredient Route
This commit is contained in:
parent
92f539fa14
commit
288dbd6d4c
67
index.php
67
index.php
@ -2,11 +2,13 @@
|
||||
|
||||
require_once("Router/Router.php");
|
||||
require_once("Router/Response.php");
|
||||
require_once("Router/Route.php");
|
||||
require_once("BancaDati/BancaDati.php");
|
||||
require_once("User.php");
|
||||
|
||||
use Router\Response;
|
||||
use Router\Router;
|
||||
use Router\Route;
|
||||
use BancaDati\BancaDati;
|
||||
|
||||
$app = new Router("/DirektiveDesDons");
|
||||
@ -21,26 +23,33 @@ $app->use("/", function (array &$req, Response $res) {
|
||||
}
|
||||
});
|
||||
|
||||
$app->get("/", function (array $req, Response $res) {
|
||||
$app->get("/", function (array $req, Response $res) use ($db) {
|
||||
$res->send("Hello World");
|
||||
//var_dump($req["user"]);
|
||||
});
|
||||
|
||||
/*
|
||||
* User
|
||||
*/
|
||||
|
||||
$app->get("/user", function (array $req, Response $res) {
|
||||
$res->send("user");
|
||||
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"]);
|
||||
}
|
||||
});
|
||||
$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) {
|
||||
$app->post("/signup", 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 ");
|
||||
$res->send("Account Created", 201);
|
||||
});
|
||||
|
||||
$app->post("/login", function( array $req, Response $res) use ($db) {
|
||||
@ -58,21 +67,43 @@ $app->post("/login", function( array $req, Response $res) use ($db) {
|
||||
}
|
||||
});
|
||||
|
||||
$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"]);
|
||||
/*
|
||||
* 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"];
|
||||
|
||||
$res->send("New ingredient has been listed ");
|
||||
$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"];
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
$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]);
|
||||
});
|
||||
|
||||
$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();
|
Loading…
Reference in New Issue
Block a user