Commit 5fd3b369 authored by poslovitch's avatar poslovitch
Browse files

Added the three first routes in Shopping Lists

parent 4bbfaedf
<?php
require_once __DIR__ . '/../db/DBConnection.php';
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
/**
* Function to retrieve - from DB - all articles (short format) for a specified shoppinglist's id
* It returns an array of articles in short format description (using get_product_data_array
* function from 'openfoodfacts.php')
* Note : if an article qtity > 1, this'll be generated several time in the returned array.
*/
function get_articles($shoppinglistid)
{
//connect to DB
$dbconn = new DB\DBConnection();
$db = $dbconn->connect();
//prepare query
$sql = "SELECT article_id, quantity FROM shoppinglists_articles WHERE shoppinglist_id = :shoppinglistid";
// https://www.php.net/manual/en/pdo.prepare.php
$stmt = $db->prepare($sql);
//bind each param
// https://www.php.net/manual/en/pdostatement.bindparam.php
$stmt->bindParam(':shoppinglistid', $shoppinglistid);
//execute sql
$stmt->execute();
$articles = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null; // clear db object
//create empty array
$articles_res = array();
//create OFFs api object
$api = new OpenFoodFacts\Api('food', 'fr');
//foreach article's row from 'shoppinglists_articles' table
foreach ($articles as $row) {
//retrieve article desc from api
//convert it in short desc
//add it into result array as many times as qtity
$articleid = $row->article_id;
$qty = intval($row->quantity);
$product = $api->getProduct($articleid);
$shortproduct = get_product_short_data_array($product->getData());
if (!is_null($shortproduct)) {
for ($cpt = 0; $cpt < $qty; $cpt++) {
$articles_res[] = $shortproduct;
}
}
}
return $articles_res;
}
$app->get('/api/shoppinglists', function (Request $request, Response $response) {
$token = get_token_infos($request);
$sl_result = array();
try {
$dbconn = new DB\DBConnection();
$db = $dbconn->connect();
$sql = "SELECT * FROM shoppinglists WHERE username = '" . $token->username . "'";
$stmt = $db->query($sql);
$shoppinglists = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach ($shoppinglists as $shoppinglist) {
$articles = get_articles($shoppinglist->id);
foreach ($articles as $article) {
if (!empty($mean_nutriscore)) {
$mean_nutriscore += $article["nutriscore"];
} else {
$mean_nutriscore = $article["nutriscore"];
}
$shoppinglist->mean_nutriscore = $mean_nutriscore;
}
$sl_result[] = $shoppinglist;
}
$db = null;
$response->getBody()->write(json_encode($sl_result));
return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
} catch (PDOException $e) {
echo $e;
//response : 500 : PDO Error (DB)
$response->getBody()->write('{"error": {"msg": "' . $e->getMessage() . '"}}');
return $response->withHeader('Content-Type', 'application/json')->withStatus(500);
}
});
$app->post('/api/shoppinglists', function (Request $request, Response $response) {
$token = get_token_infos($request);
try {
$dbconn = new DB\DBConnection();
$db = $dbconn->connect();
$sql = "SELECT * FROM shoppinglists WHERE username = '" . $token->username . "'";
$stmt = $db->query($sql);
$users = $stmt->fetchAll(PDO::FETCH_OBJ);
// Si l'utilisateur existe (Erreur 400)
if (array_key_exists("id", $request->getParsedBody())) {
$response->getBody()->write('{"error": {"msg": "Invalid ID or username"}}');
return $response->withHeader('Content-Type', 'application/json')->withStatus(400);
}
} catch (PDOException $e) {
// response : 500 : PDO Error (DB)
$response->getBody()->write('{"error": {"msg": "' . $e->getMessage() . '"}}');
return $response->withHeader('Content-Type', 'application/json')->withStatus(500);
}
if ((array_key_exists("title", $request->getParsedBody())) && (array_key_exists("purchase_date", $request->getParsedBody()))) {
$tt = $request->getParsedBody()['title'];
$p_d = $request->getParsedBody()['purchase_date'];
$sql = "INSERT INTO shoppinglists (`username`, `title`, `creation_date`, `purchase_date`) VALUES ('$token->username','$tt', now(), '$p_d')";
$stmt = $db->query($sql);
$response->getBody()->write('{"msg": "Ajouté"}');
return $response->withHeader('Content-Type', 'application/json')->withStatus(201);
} else {
$response->getBody()->write('{"error": {"msg": "Please provide a title and purchase_date"}}');
return $response->withHeader('Content-Type', 'application/json')->withStatus(400);
}
$db = null;
});
$app->get('/api/shoppinglists/{shoppinglistid}', function (Request $request, Response $response) {
$token = get_token_infos($request);
$sl_id = $request->getAttribute('shoppinglistid');
try {
$dbconn = new DB\DBConnection();
$db = $dbconn->connect();
$sql = "SELECT * FROM shoppinglists WHERE id = '" . $sl_id . "'";
$stmt = $db->query($sql);
$shoppinglists = $stmt->fetchAll(PDO::FETCH_OBJ);
if ($shoppinglists) {
foreach ($shoppinglists as $shoppinglist) {
$articles = get_articles($shoppinglist->id);
foreach ($articles as $article) {
if (!empty($mean_nutriscore)) {
$mean_nutriscore += $article["nutriscore"];
} else {
$mean_nutriscore = $article["nutriscore"];
}
$shoppinglist->mean_nutriscore = $mean_nutriscore;
}
}
}
$db = null;
$response->getBody()->write(json_encode($shoppinglist));
return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
} catch (PDOException $e) {
// response : 500 : PDO Error (DB)
$response->getBody()->write('{"error": {"msg": "' . $e->getMessage() . '"}}');
return $response->withHeader('Content-Type', 'application/json')->withStatus(500);
}
});
......@@ -32,5 +32,7 @@ require __DIR__ . '/../apiroutes/users.php';
require __DIR__ . '/../apiroutes/auth.php';
// # include OpenFoodFacts route
require __DIR__ . '/../apiroutes/openfoodfacts.php';
// # include OpenFoodFacts route
require __DIR__ . '/../apiroutes/shoppinglists.php';
$app->run();
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment