From 3c3ce56b2141c19b82f0e083fd3edffd23edd049 Mon Sep 17 00:00:00 2001 From: Joel Salisbury Date: Fri, 10 Feb 2017 15:12:59 -0500 Subject: [PATCH] getDecision now working, just need to finish the response --- api/routes/decision.php | 96 +++++++++++++++++++++++++++++++++++++---- api/routes/rules.php | 3 +- 2 files changed, 89 insertions(+), 10 deletions(-) diff --git a/api/routes/decision.php b/api/routes/decision.php index df1870e..7079578 100644 --- a/api/routes/decision.php +++ b/api/routes/decision.php @@ -57,11 +57,9 @@ function getProductIngredients($fdct_id) { } +/** Not a route. Should not be here. Don't care. Sue me. **/ - -$app->get('/getProductInfo/{upc}', function($request, $response) { - - $upc = $request->getAttribute('upc'); +function getProductInfo($upc) { $fdctconfig = array(); $fdctconfig['passphrase'] = "Partially Hydrogenated Vegetable"; @@ -94,6 +92,17 @@ function getProductIngredients($fdct_id) { $fdct_id = $res['response']['body']['product_list']['products'][0]['@attributes']['id']; $ingreds = getProductIngredients($fdct_id); $res['response']['body']['product_list']['products'][0]['@attributes']['ingreds'] = $ingreds; + curl_close($ch); + + return $res; +} + + +$app->get('/getProductInfo/{upc}', function($request, $response) { + + $upc = $request->getAttribute('upc'); + + $res = getProductInfo($upc); @@ -109,15 +118,84 @@ function getProductIngredients($fdct_id) { return $response; - curl_close($ch); + }); + $app->get('/getDecision/{upc}/{category}', function($request, $response) { - // get the ingredients by UPC - // get the rules by category +// get the nutrients by UPC +$prodInfo = getProductInfo($request->getAttribute('upc')); +$nutrients = $prodInfo['response']['body']['product_list']['products'][0]['product_nutrient_list']['product_nutrients']; + +// get the whittled down nutrtient info shittily +$theNuts = array(); + +foreach ($nutrients as $nutrient) { + $theNuts[$nutrient['nutrient']['@attributes']['name']] = $nutrient['@attributes']['value']; +} + +echo "

The Product has...

"; +echo "
";
+print_r($theNuts);
+echo "
"; + +// get the rules by category + $q = new CategoryRankNutrientQuery; + $allRules = $q->filterByCategoryId($request->getAttribute('category'))->find(); + $passedRank = "Red"; + +// for each rule, evaluate ingredients + foreach($allRules as $rule) { + $nut = $rule->getNutrient()->getName(); + $operator = $rule->getOperator(); + $threshold = $rule->getThreshold(); + $presentVal = $theNuts[$nut]; + $pass = 0; + switch($operator) { + case "lte": + $pass = ($presentVal <= $threshold); + break; + + case "lt": + $pass = ($presentVal < $threshold); + break; + + case "gt": + $pass = ($presentVal > $threshold); + break; + + case "gte": + $pass = ($presentVal >= $threshold); + break; + + case "is": + $pass = ($presentVal == $threshold); + break; + } + + + $rules[$rule->getRank()->getName()][$rule->getNutrient()->getName()] = [ + "operator" => $operator, + "threshold" => $threshold, + "passed" => $pass?"true" : "false" + ]; + + $rules[$rule->getRank()->getName()]['passedRank'] = $pass?"true" : "false"; + + if($pass) { + $passedRank = $rule->getRank()->getName(); + } + + } + +echo "

The rules expected...

"; +echo "
";
+print_r($rules);
+echo "
"; + +echo "Ultimately this food is a ". $passedRank; + - // for each rules, evaluate ingredients - // profit! }); \ No newline at end of file diff --git a/api/routes/rules.php b/api/routes/rules.php index 5a24562..f18979a 100644 --- a/api/routes/rules.php +++ b/api/routes/rules.php @@ -18,7 +18,8 @@ "nutrientId" => $rule->getNutrient()->getId(), "operator" => $rule->getOperator(), "threshold" => $rule->getThreshold(), - "units" => $rule->getUnits() + "units" => $rule->getUnits(), + "rank" => $rule->getRankId() ]; }