diff --git a/browser/js/groceries/groceries.html b/browser/js/groceries/groceries.html index d092590b..63acc682 100644 --- a/browser/js/groceries/groceries.html +++ b/browser/js/groceries/groceries.html @@ -2,11 +2,14 @@
- Grocery List - -

{{ item.name }}

- -
+ Grocery List +
+

{{ section }}


+ +

{{item.name}} || {{round(item.amount)}} {{item.unitLong}}

+ +
- +
+ \ No newline at end of file diff --git a/browser/js/groceries/groceries.js b/browser/js/groceries/groceries.js index 2802577c..25c03981 100644 --- a/browser/js/groceries/groceries.js +++ b/browser/js/groceries/groceries.js @@ -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; +}); diff --git a/package.json b/package.json index ac80c9dc..7e7a09c3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/server/app/routes/user/meal-routes.js b/server/app/routes/user/meal-routes.js index 3249bca4..1a8ec426 100644 --- a/server/app/routes/user/meal-routes.js +++ b/server/app/routes/user/meal-routes.js @@ -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; diff --git a/server/db/index.js b/server/db/index.js index 9b4f4823..402b39f9 100644 --- a/server/db/index.js +++ b/server/db/index.js @@ -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; diff --git a/server/db/models/meal-plan-model.js b/server/db/models/meal-plan-model.js index a2cc99e0..e8df4b4e 100644 --- a/server/db/models/meal-plan-model.js +++ b/server/db/models/meal-plan-model.js @@ -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) + }); + }); + } + } })