| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- const recipeBook = require("../strands/recipeBook.js");
- const analytics = require("../strands/analytics.js");
- class RecipeIngredient{
- constructor(ingredient, quantity, unit){
- this._ingredient = ingredient;
- this._quantity = quantity;
- this._unit = unit;
- }
- get ingredient(){
- return this._ingredient;
- }
- get quantity(){
- return this._quantity * controller.unitMultiplier(controller.getBaseUnit(this._unit), this._unit);
- }
- set quantity(quantity){
- this._quantity = quantity;
- }
- get unit(){
- return this._unit;
- }
- getQuantityDisplay(){
- return `${this.quantity.toFixed(2)} ${this._unit.toUpperCase()}`;
- }
- }
- /*
- Recipe Object
- id = database id of recipe
- name = name of recipe
- price = price of recipe in cents
- ingredients = [{
- ingredient: Ingredient Object,
- quantity: quantity of the ingredient within the recipe (stored as base unit, i.e grams)
- unit: String
- baseUnitmultiplier: Number
- }]
- parent = merchant that it belongs to
- */
- class Recipe{
- constructor(id, name, category, price, ingredients, parent, hidden){
- this._id = id;
- this._name = name;
- this._category = category;
- this._price = price;
- this._parent = parent;
- this._hidden = hidden;
- this._ingredients = [];
- //Ingredient totals is the total amount of each ingredient within the recipe, converted to ingredient base unit
- this._ingredientTotals = {};
- for(let i = 0; i < ingredients.length; i++){
- let ingredient = parent.getIngredient(ingredients[i].ingredient);
- let recipeIngredient = new RecipeIngredient(
- ingredient.ingredient,
- ingredients[i].quantity,
- ingredients[i].unit,
- );
- this._ingredients.push(recipeIngredient);
- }
- }
- get id(){
- return this._id;
- }
- get name(){
- return this._name;
- }
- set name(name){
- this._name = name;
- }
- get category(){
- return this._category;
- }
- set category(category){
- this._category = category;
- }
- get price(){
- return this._price / 100;
- }
- set price(price){
- this._price = price;
- }
- get parent(){
- return this._parent;
- }
- get hidden(){
- return this._hidden;
- }
- set hidden(hidden){
- this._hidden = hidden;
- }
- get ingredients(){
- return this._ingredients;
- }
- clearIngredients(){
- this._ingredients = [];
- }
- get ingredientTotals(){
- return this._ingredientTotals;
- }
- //Returns the quantity of a single ingredient with the recipe.
- //Returns the quantity converted to the base unit of the ingredient
- getIngredientTotal(id){
- for(let i = 0; i < this._ingredients.length; i++){
- if(this._ingredients[i].ingredient.id === id){
- return (this._ingredientTotals[id] === undefined) ? 0 : this._ingredientTotals[id] * controller.unitMultiplier(controller.toBase(this._ingredients[i].ingredient.unit), this._ingredients[i].ingredient.unit);
- }
- }
- return 0;
- }
- addIngredient(ingredient, quantity, unit, baseUnitMultiplier){
- let recipeIngredient = new RecipeIngredient(ingredient, quantity, unit, baseUnitMultiplier);
- this._ingredients.push(recipeIngredient);
- recipeBook.isPopulated = false;
- analytics.isPopulated = false;
- }
- removeIngredients(){
- this._ingredients = [];
- }
- calculateIngredientTotals(){
- this._ingredientTotals = {};
- let traverseIngredient = (ingredient, multiplier)=>{
- for(let i = 0; i < ingredient.subIngredients.length; i++){
- traverseIngredient(ingredient.subIngredients[i].ingredient, multiplier * ingredient.subIngredients[i]._quantity);
- }
- if(this._ingredientTotals[ingredient.id] === undefined){
- this._ingredientTotals[ingredient.id] = multiplier;
- }else{
- this._ingredientTotals[ingredient.id] += multiplier;
- }
- }
- for(let i = 0; i < this._ingredients.length; i++){
- traverseIngredient(this._ingredients[i]._ingredient, this._ingredients[i]._quantity);
- }
- }
- }
- module.exports = Recipe;
|