diff --git a/api/routes/category.php b/api/routes/category.php index 1122017..edf756b 100644 --- a/api/routes/category.php +++ b/api/routes/category.php @@ -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(), diff --git a/api/routes/decision.php b/api/routes/decision.php index 90d0ca0..68bd951 100644 --- a/api/routes/decision.php +++ b/api/routes/decision.php @@ -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 "
"; -// print_r($theNuts); -// echo ""; +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 "
"; + // print_r($theNuts); + // echo ""; - $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 "
";