Skip to content

Commit

Permalink
category CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy Morris committed Mar 6, 2017
1 parent 47a0365 commit 8a5462b
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 157 deletions.
2 changes: 1 addition & 1 deletion app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ app.factory('Categories', function($resource) {
});

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
50 changes: 29 additions & 21 deletions app/pages/categories/ctrl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

function GetRanks() {
return ['green', 'yellow'];
return [{name: 'green', id: 1}, {name: 'yellow', id: 2}];
}

function GetNutrients() {
Expand All @@ -15,14 +15,9 @@ function GetOperations() {
{name: 'e', symbol: 'Equal To'}];
}

function SaveData(categories) {
console.log('save');
}

app.controller('CategoryCtrl', ['$scope', 'Categories', function ($scope, Categories) {
app.controller('CategoryCtrl', ['$scope', '$http', 'Categories', 'Nutrients', function ($scope, $http, Categories, Nutrients) {
$scope.page.title = 'Categories';
$scope.page.id = 'cat';
$scope.page.yourVar = "globalStuff";


// GET list of categories
Expand All @@ -31,9 +26,16 @@ app.controller('CategoryCtrl', ['$scope', 'Categories', function ($scope, Catego
});

$scope.ranks = GetRanks();
$scope.nutrients = GetNutrients();

$scope.units = ['mg', 'g'];

Nutrients.get({}, function(data) {
$scope.nutrients = data.data;
});

$scope.operations = GetOperations();


$scope.getCategoryIndexFromName = function(categoryName) {
for(var i = 0; i < $scope.categories.length; i++) {
if($scope.categories[i].name == categoryName) {
Expand All @@ -44,26 +46,32 @@ app.controller('CategoryCtrl', ['$scope', 'Categories', function ($scope, Catego
};

$scope.addRule = function(categoryName) {
/*var categoryIndex = $scope.getCategoryIndexFromName(categoryName);
var categoryIndex = $scope.getCategoryIndexFromName(categoryName);
if(categoryIndex != -1) {
$scope.categories[categoryIndex].rules.push({ nutrient: 'sugar',
rule_id: '0',
ranks: [ { name: 'green', operation: 'lt', value: 5 },
{ name: 'yellow', operation: 'gt', value: 2 } ] });
}*/
$scope.categories[categoryIndex].rules[0].push({
ruleId: 0,
nutrientName: $scope.nutrients[0].name,
nutrientId: $scope.nutrients[0].id,
operator: "lte",
threshold: 10,
units: "mg",
rank: 1,
categoryId: categoryIndex
});
}

};

$scope.removeRule = function(category, rule) {
/*for(var i = 0; i < category.rules.length; i++) {
if(category.rules[i] == rule) {
category.rules.splice(i, 1);
for(var i = 0; i < category.rules[0].length; i++) {
if(category.rules[0][i] == rule) {
category.rules[0].splice(i, 1);
}
}*/
}
};

$scope.addCategory = function() {
$scope.categories.push({name: 'New Category', rules: []})
//$scope.categories.push({name: 'New Category', rules: []})
};

$scope.removeCategory = function(category) {
Expand All @@ -74,7 +82,7 @@ app.controller('CategoryCtrl', ['$scope', 'Categories', function ($scope, Catego
}
};

$scope.saveData = function() {
SaveData($scope.categories);
$scope.saveData = function(category) {
$http.post('http://foodbank.develop.digitalmediauconn.org/api/index.php/rules/saveBatch/' + category.id, category.rules[0]);
};
}]);
72 changes: 39 additions & 33 deletions app/pages/categories/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,59 @@
<div class="collapsible-header">{{ category.name }}</div>
<div class="collapsible-body">
<div class="row">
<div class="col s12">
<!--<div class="col s12">
<div class="input-field col s6">
<input placeholder="Placeholder" id="first_name" type="text" class="validate" ng-model="category.name">
<label for="first_name">Category Name</label>
</div>
<button class="btn grey" style="float:right" ng-click="removeCategory(category)">Delete Category</button>
</div>-->
<div class="col s6" ng-repeat="rule in category.rules[0]">
<div class="card">
<div class="card-content">
<div class="row">
<div class="col s6">
<select>
<option ng-repeat="nutrient in nutrients" value="{{nutrient.name}}" ng-selected="nutrient.name==rule.nutrientName">{{nutrient.name}}</option>
</select>
</div>
<div class="col s6">
<select>
<option ng-repeat="rank in ranks" value="{{rank.id}}" ng-selected="rule.rank == rank.id">{{ rank.name }}</option>
</select>
</div>
<div class="col s4">
<select>
<option ng-repeat="operation in operations" value="{{operation.name}}" ng-selected="operation.name==rank.operator">{{operation.symbol}}</option>
</select>
</div>
<div class="col s4">
<input type="number" ng-model="rule.threshold" value="{{rule.threshold}}">
</div>
<div class="col s4">
<select>
<option ng-repeat="unit in units" value="{{unit}}" ng-selected="rule.units == unit">{{unit}}</option>
</select>
</div>
<div class="col s4">
<button class="btn red darken-3" ng-click="removeRule(category, rule)">Delete</button>
</div>
</div>
</div>
</div>
</div>
<div class="col s12">
<table class="categoryTable">
<thead>
<tr>
<th>Nutrient</th>
<th ng-repeat="rank in ranks">{{rank}} condition</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="(rule_nutrient, rules) in category.rules">
<td>
<div class="input-field col s12">
<select>
<option ng-repeat="nutrient in nutrients" value="{{nutrient}}" ng-selected="nutrient==rule_nutrient">{{nutrient}}</option>
</select>
</div>
</td>
<td ng-repeat="rank in rules">
<div class="input-field col s6">
<select>
<option ng-repeat="operation in operations" value="{{operation.name}}" ng-selected="operation.name==rank.operator">{{operation.symbol}}</option>
</select>
</div>
<div class="input-field col s6">
<input type="number" value="{{rank.threshold | number}}">
</div>
</td>
<td><button class="btn red" ng-click="removeRule(category, rule)"><i class="material-icons">delete</i></button></td>
</tr>
</tbody>
</table>



<button ng-click="addRule(category.name)" class="btn">Add Rule</button>
<button class="btn green" ng-click="saveData(category)">Save</button>
</div>
</div>
</div>
</li>
</ul>
<button class="btn blue" ng-click="addCategory()" onclick="UpdateTextFields()">Add Category</button>
<!--<button class="btn blue" ng-click="addCategory()" onclick="UpdateTextFields()">Add Category</button>-->
</div>
<!--<div class="col s12 m3">
<div class="card grey darken-3">
Expand Down
Loading

0 comments on commit 8a5462b

Please sign in to comment.