Skip to content

Commit

Permalink
categories merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy Morris committed Feb 28, 2017
2 parents 6d551b5 + 0901491 commit 47a0365
Show file tree
Hide file tree
Showing 17 changed files with 693 additions and 367 deletions.
2 changes: 1 addition & 1 deletion api/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

header("Access-Control-Allow-Origin: *");
$autoloader = require './vendor/autoload.php'; // Composer Autoload Libraries
$autoloader->add('', __DIR__ . '/generated-classes/'); // Propel ORM Classes
require './generated-conf/config.php'; // Propel Config
Expand Down
23 changes: 22 additions & 1 deletion api/routes/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,42 @@
$app->get('/category/all', function( $request, $response ) {
$q = new CategoryQuery;
$cats = $q::create()->find();

$response_json = [
"status" => [
"code" => $response->getStatusCode(),
"message" => "OK"
],
"data" => []
];

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


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

}

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

});

/**
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
54 changes: 53 additions & 1 deletion api/routes/rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,59 @@
$newRule->setRankId($rank);
$newRule->setNutrientId($nutrient);

$newRule->save();
if($newRule->save()){
$msg = "New Rule Saved";
} else {
$msg = "There was an error saving this rule";
}



$response_json = [
"status" => [
"code" => $response->getStatusCode(),
"message" => [$msg]
]
];

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


});

$app->post('/rules/updateRuleById/{ruleId}', function($request, $response) {

$rule = CategoryRankNutrientQuery::create()->findPk($request->getAttribute("ruleId"));

$cat = $request->getParam('catId');


$nutrient = $request->getParam('nutrientId');

$rank = $request->getParam('rankId');


$rule->setThreshold($request->getParam('threshold'));
$rule->setUnits($request->getParam('units'));
$rule->setOperator($request->getParam('operator'));
$rule->setCategoryId($cat);
$rule->setRankId($rank);
$rule->setNutrientId($nutrient);

if($rule->save()){
$msg = "Rule updated.";
} else {
$msg = "There was an error saving this rule";
}


$response_json = [
"status" => [
"code" => $response->getStatusCode(),
"message" => [$msg]
]
];
$response = $response->withJSON($response_json);
return $response;
});
36 changes: 20 additions & 16 deletions app/app.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
var app = angular.module('app', ['ngRoute', 'ngResource']);
var app = angular.module('app', ['ngRoute', 'ngResource', 'ngCookies']);

// Root controllers in /root/ folder
// Page controllers in /pages/[your-page] folder

// Factories

// TODO: 'Foods' factory TEMPORARY
app.factory('Foods', function($resource) {
return $resource('assets/json/foods.json');
});

// Just does a PUT request when u call Food.update
app.factory('Food', function ($resource) {
// {food: '@id'} means replace :food with $scope.foodCRUD.id
var data = $resource('http://foodbank.develop.digitalmediauconn.org/api/food/:food', {food: '@id'}, {
update:{ method:'PUT' }
});
return data;
app.factory('FoodDetail', function($resource) {
// http://foodbank.develop.digitalmediauconn.org/api/getProductInfo/049000031249
// return $resource('https://example.com/api/food/:barcode', {barcode: '@barcode'});
return $resource('assets/json/foodDetail.json');
});

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

// TODO: 'Nutrients' factory TEMPORARY
app.factory('Nutrients', function($resource) {
return $resource('assets/json/nutrients.json');
});

app.factory('Nutrient', function ($resource) {
var data = $resource('http://foodbank.develop.digitalmediauconn.org/api/nutrients/:nutrient', {nutrient: '@id'}, {
update:{ method:'PUT' }
});
return data;
});
// Just does a PUT request when u call Food.update
// app.factory('Food', function ($resource) {
// // {food: '@id'} means replace :food with $scope.foodCRUD.id
// var data = $resource('http://foodbank.develop.digitalmediauconn.org/api/food/:food', {food: '@id'}, {
// update:{ method:'PUT' }
// });
// return data;
// });

// app.factory('Nutrient', function ($resource) {
// var data = $resource('http://foodbank.develop.digitalmediauconn.org/api/nutrients/:nutrient', {nutrient: '@id'}, {
// update:{ method:'PUT' }
// });
// return data;
// });
Loading

0 comments on commit 47a0365

Please sign in to comment.