Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'release/2017-03-23'
  • Loading branch information
Timothy Morris committed Mar 23, 2017
2 parents 0c9af31 + 28fc295 commit 1577c37
Show file tree
Hide file tree
Showing 23 changed files with 3,454 additions and 262 deletions.
1 change: 1 addition & 0 deletions api/index.php
Expand Up @@ -15,6 +15,7 @@ require './routes/category.php';
require './routes/rules.php';
require './routes/food.php';
require './routes/decision.php';
require './routes/nutrient.php';

// Run App
$app->run();
30 changes: 16 additions & 14 deletions api/routes/category.php
Expand Up @@ -8,41 +8,43 @@ $app->get('/category/all', function( $request, $response ) {
$q = new CategoryQuery;
$cats = $q::create()->find();





$response_json = [
"status" => [
"code" => $response->getStatusCode(),
"message" => "OK"
],
"data" => []
];
//[strtolower($rule->getRank()->getName())][$rule->getNutrient()->getName()]
foreach( $cats as $cat ) {

$rules = [];
$q = new CategoryRankNutrientQuery;
$requestedCatRules = $q->filterByCategoryId($cat->getId())->find();
$requestedCatRules = $q->filterByCategoryId($cat->getId())->orderByNutrientId()->find();
foreach($requestedCatRules as $rule) {
$rules[] = [
"nutrientName" => $rule->getNutrient()->getName(),
"nutrientId" => $rule->getNutrient()->getId(),
"operator" => $rule->getOperator(),
"threshold" => $rule->getThreshold(),
"units" => $rule->getUnits(),
"rank" => $rule->getRankId()
$rules[] = [
"ruleId" => $rule->getId(),
"nutrientName" => $rule->getNutrient()->getName(),
"nutrientId" => $rule->getNutrient()->getId(),
"operator" => $rule->getOperator(),
"threshold" => $rule->getThreshold(),
"units" => $rule->getUnits(),
"rank" => $rule->getRankId(),
"categoryId" => $rule->getCategoryId()
];
}


$response_json["data"][] = [
"id" => $cat->getId(),
"name" => $cat->getName(),
"rules" => $rules
"rules" => [$rules]
];

}

$response = $response->withJSON($response_json);
return $response;

});

/**
Expand Down
163 changes: 106 additions & 57 deletions api/routes/decision.php
Expand Up @@ -124,86 +124,135 @@ $app->get('/getProductInfo/{upc}', function($request, $response) {


$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);
$newFood->setName($res["food"]['brand']." " .$res["food"]['name']);


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
25 changes: 25 additions & 0 deletions api/routes/nutrient.php
@@ -0,0 +1,25 @@
<?php
$app->get('/nutrient/all', function( $request, $response ) {
$q = new NutrientQuery;
$nuts = $q::create()->find();

$response_json = [
"status" => [
"code" => $response->getStatusCode(),
"message" => "OK"
],
"data" => []
];
//[strtolower($rule->getRank()->getName())][$rule->getNutrient()->getName()]
foreach( $nuts as $nut ) {
$response_json["data"][] = [
"id" => $nut->getId(),
"name" => $nut->getName(),
];

}

$response = $response->withJSON($response_json);
return $response;

});
4 changes: 3 additions & 1 deletion api/routes/rules.php
Expand Up @@ -14,12 +14,14 @@ $app->get('/rules/byCategory/{id}', function($request, $response) {

foreach($requestedCatRules as $rule) {
$response_json["data"][] = [
"ruleId" => $rule->getId(),
"nutrientName" => $rule->getNutrient()->getName(),
"nutrientId" => $rule->getNutrient()->getId(),
"operator" => $rule->getOperator(),
"threshold" => $rule->getThreshold(),
"units" => $rule->getUnits(),
"rank" => $rule->getRankId()
"rank" => $rule->getRankId(),
"categoryId" => $rule->getCategoryId()
];
}

Expand Down
7 changes: 4 additions & 3 deletions app/app.js
Expand Up @@ -6,7 +6,8 @@ var app = angular.module('app', ['ngRoute', 'ngResource', 'ngCookies']);
// Factories

app.factory('Foods', function($resource) {
return $resource('assets/json/foods.json');
return $resource('http://foodbank.develop.digitalmediauconn.org/api/food/all');
// return $resource('assets/json/foods.json');
});

app.factory('FoodDetail', function($resource) {
Expand All @@ -16,11 +17,11 @@ app.factory('FoodDetail', function($resource) {
});

app.factory('Categories', function($resource) {
return $resource('assets/json/categories.json');
return $resource('http://foodbank.develop.digitalmediauconn.org/api/index.php/category/all');
});

app.factory('Nutrients', function($resource) {
return $resource('assets/json/nutrients.json');
return $resource('http://foodbank.develop.digitalmediauconn.org/api/index.php/nutrient/all');
});

// Just does a PUT request when u call Food.update
Expand Down
2 changes: 2 additions & 0 deletions app/pages/404/style.styl
@@ -0,0 +1,2 @@
.f404
text-align center
2 changes: 1 addition & 1 deletion app/pages/404/view.html
@@ -1,2 +1,2 @@
<div class="spacer"></div>
<h1>You've reached pages/404/view.html by going to an invalid link.</h1>
<h1 class="f404">404 Page not found</h1>
Empty file.

0 comments on commit 1577c37

Please sign in to comment.