Skip to content

Commit

Permalink
Grocery list (#71)
Browse files Browse the repository at this point in the history
* Created method on meal plan model to getGroceryList()

* Added column to mealPlanModel that contains groceryList

* Created route /users/:userid/meals/grocerylist to get a grocerylist

* Created a ListFactory with method to getGroceryList

* Hooked up back grocery list with front end

* Grocery list is now divided by sections and is alphabetical
  • Loading branch information
verasveras authored and galencorey committed Oct 10, 2016
1 parent 8eecd5d commit d354aa3
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 17 deletions.
15 changes: 9 additions & 6 deletions browser/js/groceries/groceries.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
<navbar></navbar>
<div id="groceries">
<div class="groceries">
<md-subheader class="md-no-sticky">Grocery List</md-subheader>
<md-list-item ng-repeat="item in items">
<p> {{ item.name }} </p>
<md-checkbox class="md-secondary" ng-model="item.wanted"></md-checkbox>
</md-list-item>
<md-subheader class="md-no-sticky">Grocery List</md-subheader>
<div ng-repeat="section in headers | orderBy : section">
<h3> {{ section }} </h3> <br />
<md-list-item ng-repeat="item in sections[section] | orderBy: 'name'">
<p> {{item.name}} || {{round(item.amount)}} {{item.unitLong}} </p>
<md-checkbox class="md-secondary" ng-model="item.wanted"></md-checkbox>
</md-list-item>
</div>
</div>
</md-content>
</div>
</md-content>
60 changes: 53 additions & 7 deletions browser/js/groceries/groceries.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,58 @@ app.config(function ($stateProvider) {
});
});

app.controller('ListCtrl', function($scope) {
$scope.items = [
{ name: 'Pepperoni', wanted: true },
{ name: 'Sausage', wanted: false },
{ name: 'Black Olives', wanted: true },
{ name: 'Green Peppers', wanted: false }
];
app.controller('ListCtrl', function($scope, ListFactory, Session) {

$scope.sections = {};


ListFactory.getGroceryList(Session.user.id)
.then(function(groceryList){
$scope.sections = groceryList;
$scope.headers = Object.keys(groceryList);
});

$scope.round = function(number){
if (isNaN(number)){
return parseInt(number, 10);
}
return Math.round(number * 100) / 100;
}


});

app.factory('ListFactory', function($http) {

let ListFactory = {};

ListFactory.getGroceryList = function(userId){
return $http.get(`api/users/${userId}/meals/grocerylist`)
.then(function(response){
return response.data;
// {amount, unit, unitLong, Section}
})
.then(function(groceryList){
let formatedList = {};

for (var key in groceryList){
if (!formatedList[groceryList[key].section]){
formatedList[groceryList[key].section] = [];
}
let newItem = {
name: key,
amount: groceryList[key].amount,
unit: groceryList[key].unitShort,
unitLong: groceryList[key].unitLong,
section: groceryList[key].aisle
}
formatedList[groceryList[key].section].push(newItem)
}

return formatedList;
});

}

return ListFactory;
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"chai": "^2.1.0",
"chalk": "^1.0.0",
"connect-session-sequelize": "^3.0.0",
"convert-units": "^1.2.0",
"cookie-parser": "^1.3.4",
"d3": "3.5.17",
"eslint": "^3.4.0",
Expand Down
17 changes: 17 additions & 0 deletions server/app/routes/user/meal-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ router.post('', (req, res, next) => {

})

//create a new active meal plan for user
//req.body.mealPlan = array of recipe ids
router.get('/grocerylist', (req, res, next) => {
//if the user already has a meal plan, close it
MealPlan.findOne({
where: {
userId: req.params.userId,
status: 'active'
}
})
.then(function(plan){
res.send(plan.groceryList);
})
.catch(next);

})

module.exports = router;


2 changes: 0 additions & 2 deletions server/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ Ingredient.belongsToMany(Recipe, { through: 'recipes_ingredients', as: 'recipes'
Recipe.belongsToMany(Ingredient, { through: 'recipes_ingredients', as: 'ingredients' });

UserPref.belongsTo(User); // UserPref has userId column
// User.belongsTo(UserPref);

MealPlan.belongsTo(User);
// MealPlan.belongsToMany(Recipe, {through: 'meal-plans_recipes'});

module.exports = db;
66 changes: 64 additions & 2 deletions server/db/models/meal-plan-model.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,72 @@
'use strict';
const crypto = require('crypto');
const convert = require('convert-units');
const Recipe = require('./recipe-model.js');
const Sequelize = require('sequelize');

const db = require('../_db');

module.exports = db.define('mealPlan', {
status: Sequelize.ENUM('active', 'complete'),
meals: Sequelize.ARRAY(Sequelize.INTEGER)
meals: Sequelize.ARRAY(Sequelize.INTEGER),
groceryList: Sequelize.JSON
},
{
instanceMethods: {
getGroceryList: function(){

let groceryList = {};

return Recipe.findAll({
where: {
id: this.meals // meals will be an array of recipe ids
}
})
.then(function(recipes){
recipes.forEach(function(recipe){
recipe.extendedIngredients.forEach(function(ingredient){
// ingredient has: name, amount, unitShort
console.log(ingredient.name, ingredient.unitShort);

// if you've never seen this ingredient before, add it to the list with the amount and unit.
if (!groceryList[ingredient.name]){
groceryList[ingredient.name] = {
amount: ingredient.amount,
unit: ingredient.unitShort,
unitLong: ingredient.unitLong,
section: ingredient.aisle
}
}
// if you have seen this ingredient before, convert the unit and then add the amount;
else {
let unit = groceryList[ingredient.name].unit;
let convertedAmount;
try { //
convertedAmount = convert(ingredient.amount).from(ingredient.unitShort).to(unit);
}
catch (error) {
convertedAmount = ingredient.unitShort;
}

groceryList[ingredient.name].amount += convertedAmount;
}
})
})

return groceryList;
});
}
}, // end Instance Methods

hooks: {
afterCreate: function(mealPlan){

return mealPlan.getGroceryList()
.then(function(groceryList){
console.log(groceryList);
return mealPlan.update({
groceryList: JSON.stringify(groceryList)
});
});
}
}
})

0 comments on commit d354aa3

Please sign in to comment.