Skip to content

Commit

Permalink
Merge pull request #7 from dmd-web-and-mobile/rules-endpoints
Browse files Browse the repository at this point in the history
Rules endpoints
  • Loading branch information
jrs06005 committed Feb 27, 2017
2 parents c0820eb + 4e3afff commit 5a67774
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 59 deletions.
4 changes: 2 additions & 2 deletions api/routes/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
foreach( $cats as $cat ) {

$q = new CategoryRankNutrientQuery;
$requestedCatRules = $q->filterByCategoryId($cat->getId())->find();
$requestedCatRules = $q->filterByCategoryId($cat->getId())->orderByRankId()->find();
foreach($requestedCatRules as $rule) {
$rules[] = [
$rules[$rule->getNutrient()->getName()][] = [
"nutrientName" => $rule->getNutrient()->getName(),
"nutrientId" => $rule->getNutrient()->getId(),
"operator" => $rule->getOperator(),
Expand Down
162 changes: 105 additions & 57 deletions api/routes/decision.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,86 +124,134 @@ function getProductInfo($upc) {


$app->get('/getDecision/{upc}/{category}', function($request, $response) {
/** Checking to see if the food is already in the DB, in which case we save lots of time **/

// get the nutrients by UPC
$prodInfo = getProductInfo($request->getAttribute('upc'));
$nutrients = $prodInfo['response']['body']['product_list']['products'][0]['product_nutrient_list']['product_nutrients'];
$q = new FoodQuery();
$barcode = $request->getAttribute('upc');
$food = $q::create()
->useRankQuery()
->endUse()
->useCategoryQuery()
->endUse()
->filterByBarcode($barcode)
->findOne();

// get the whittled down nutrtient info shittily
$theNuts = array();
if($food){

$rank = $food->getRank();
$category = $food->getCategory();

$res['rankId'] = $food->getRankId();
$res['rankName'] = $rank->getName();


$res['request']['message'] = "Did not hit Fooducate. Food already exists in DB.";

foreach ($nutrients as $nutrient) {
$theNuts[$nutrient['nutrient']['@attributes']['name']] = $nutrient['@attributes']['value'];
}

// echo "<h2>The Product has...</h2>";
// echo "<pre>";
// print_r($theNuts);
// echo "</pre>";
if(!$food) {
// 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 rules by category
$q = new CategoryRankNutrientQuery;
$c = new CategoryQuery();
// get the whittled down nutrtient info shittily
$theNuts = array();

foreach ($nutrients as $nutrient) {
$theNuts[$nutrient['nutrient']['@attributes']['name']] = $nutrient['@attributes']['value'];
}

$allRules = $q->filterByCategoryId($request->getAttribute('category'))->find();
//$res["food"] = $prodInfo;
$res["food"]['brand'] = $prodInfo['response']['body']['product_list']['products'][0]["@attributes"]['brand'];
$res["food"]['name'] = $prodInfo['response']['body']['product_list']['products'][0]["@attributes"]['name'];
// echo "<h2>The Product has...</h2>";
// echo "<pre>";
// print_r($theNuts);
// echo "</pre>";

$res["request"]["categoryRuleRun"] = $c->findPk($request->getAttribute("category"))->getName();
// get the rules by category
$q = new CategoryRankNutrientQuery;
$c = new CategoryQuery();


//default
$res["rankId"] = 0;
$res["rankName"] = "Red";
$allRules = $q->filterByCategoryId($request->getAttribute('category'))->find();
//$res["food"] = $prodInfo;
$res["food"]['brand'] = $prodInfo['response']['body']['product_list']['products'][0]["@attributes"]['brand'];
$res["food"]['name'] = $prodInfo['response']['body']['product_list']['products'][0]["@attributes"]['name'];

// for each rule, evaluate ingredients
foreach($allRules as $rule) {
$nut = $rule->getNutrient()->getName();
$operator = $rule->getOperator();
$threshold = $rule->getThreshold();
$presentVal = $theNuts[$nut];
$res["request"]["categoryRuleRun"] = $c->findPk($request->getAttribute("category"))->getName();


$pass = 0;
switch($operator) {
case "lte":
$pass = ($presentVal <= $threshold);
break;
//default
$res["rankId"] = 3;
$res["rankName"] = "Red";

case "lt":
$pass = ($presentVal < $threshold);
break;
// for each rule, evaluate ingredients
foreach($allRules as $rule) {
$nut = $rule->getNutrient()->getName();
$operator = $rule->getOperator();
$threshold = $rule->getThreshold();
$presentVal = $theNuts[$nut];

case "gt":
$pass = ($presentVal > $threshold);
break;

case "gte":
$pass = ($presentVal >= $threshold);
break;
$pass = 0;
switch($operator) {
case "lte":
$pass = ($presentVal <= $threshold);
break;

case "is":
$pass = ($presentVal == $threshold);
break;
}
case "lt":
$pass = ($presentVal < $threshold);
break;

case "gt":
$pass = ($presentVal > $threshold);
break;

$rules[$rule->getRank()->getName()][$rule->getNutrient()->getName()] = [
"operator" => $operator,
"threshold" => $threshold,
"passed" => $pass?"true" : "false"
];
case "gte":
$pass = ($presentVal >= $threshold);
break;

$rules[$rule->getRank()->getName()]['passedRank'] = $pass?"true" : "false";
case "is":
$pass = ($presentVal == $threshold);
break;
}

if($pass) {
$res["rankId"] = $rule->getRank()->getId();
$res["rankName"] = $rule->getRank()->getName();
}

}
$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) {
$res["rankId"] = $rule->getRank()->getId();
$res["rankName"] = $rule->getRank()->getName();
}
}


/** Now create this Food with the associated rank and category **/

$newFood = new Food;

$newFoodCat = $request->getAttribute('category');

$newFoodRank = $res['rankId'];

$newFood->setRankId($newFoodRank);
$newFood->setCategoryId($newFoodCat);
$newFood->setBarcode($barcode);


if($newFood->save()){
$msg = "New Food also added to DB.";
} else {
$msg = "There was an error saving this new Food to the DB";
}

$res['request']['message'] = $msg;

}

// echo "<h1>The rules expected...</h1>";
// echo "<pre>";
Expand Down

0 comments on commit 5a67774

Please sign in to comment.