-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
8eecd5d
commit d354aa3
Showing
6 changed files
with
144 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}); | ||
}); | ||
} | ||
} | ||
}) |