Ver Fonte

Create ingredientData.js for refactoring

Lee Morgan há 6 anos atrás
pai
commit
12c7fdfeb5
3 ficheiros alterados com 75 adições e 57 exclusões
  1. 0 54
      controllers/home.js
  2. 71 0
      controllers/ingredientData.js
  3. 4 3
      routes.js

+ 0 - 54
controllers/home.js

@@ -2,65 +2,11 @@ const axios = require("axios");
 const bcrypt = require("bcryptjs");
 
 const Merchant = require("../models/merchant");
-const Ingredient = require("../models/ingredient");
 const nonPosTransaction = require("../models/nonPosTransaction");
 
 const token = "b48068eb-411a-918e-ea64-52007147e42c";
 
 module.exports = {
-    getIngredients: function(req, res){
-        Ingredient.find()
-            .then((ingredients)=>{
-                return res.json(ingredients);
-            })
-            .catch((err)=>{
-                console.log(err);
-                return res.render("error");
-            });
-    },
-
-    createNewIngredients: function(req, res){
-        Ingredient.create(req.body)
-            .then((ingredients)=>{
-                return res.json(ingredients);
-            })
-            .catch((err)=>{
-                console.log(err);
-                return res.render("error");
-            });
-    },
-
-    createIngredient: function(req, res){
-        Ingredient.create(req.body.ingredient)
-            .then((ingredient)=>{
-                Merchant.findOne({_id: req.session.user})
-                    .then((merchant)=>{
-                        let item = {
-                            ingredient: ingredient,
-                            quantity: req.body.quantity
-                        }
-                        merchant.inventory.push(item);
-                        merchant.save()
-                            .then((merchant)=>{
-                                console.log("something");
-                                return res.json(item);
-                            })
-                            .catch((err)=>{
-                                console.log(err);
-                                return res.render("error");
-                            });
-                    })
-                    .catch((err)=>{
-                        console.log(err);
-                        return res.render("error");
-                    });
-            })
-            .catch((err)=>{
-                console.log(err);
-                return res.render("error");
-            });  
-    },
-
     createTransaction: function(req, res){
         let transaction = new nonPosTransaction({
             date: Date.now(),

+ 71 - 0
controllers/ingredientData.js

@@ -0,0 +1,71 @@
+const Merchant = require("../models/merchant");
+const Ingredient = require("../models/ingredient");
+
+module.exports = {
+    //GET - gets a list of all database ingredients
+    //Returns:
+    //  ingredients: list containing all ingredients
+    getIngredients: function(req, res){
+        Ingredient.find()
+            .then((ingredients)=>{
+                return res.json(ingredients);
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
+    },
+
+    //POST - creates new ingredients from a list
+    //Inputs:
+    //  req.body: list of ingredients (name, category, unit)
+    //Returns:
+    //  ingredients: list containing the newly created ingredients
+    createNewIngredients: function(req, res){
+        Ingredient.create(req.body)
+            .then((ingredients)=>{
+                return res.json(ingredients);
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
+    },
+
+    //TODO - Redirect to merchantData.js rather than adding here
+    //POST - create a single ingredient and then add to the merchant
+    //Inputs: 
+    //  req.body.ingredient: full ingredient to create (name, category, unit)
+    //  req.body.quantity: quantity of ingredient for merchant
+    //Returns:
+    //  item: ingredient and quantity
+    createIngredient: function(req, res){
+        Ingredient.create(req.body.ingredient)
+            .then((ingredient)=>{
+                Merchant.findOne({_id: req.session.user})
+                    .then((merchant)=>{
+                        let item = {
+                            ingredient: ingredient,
+                            quantity: req.body.quantity
+                        }
+                        merchant.inventory.push(item);
+                        merchant.save()
+                            .then((merchant)=>{
+                                return res.json(item);
+                            })
+                            .catch((err)=>{
+                                console.log(err);
+                                return res.render("error");
+                            });
+                    })
+                    .catch((err)=>{
+                        console.log(err);
+                        return res.render("error");
+                    });
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });  
+    }
+}

+ 4 - 3
routes.js

@@ -1,6 +1,7 @@
 const home = require("./controllers/home");
 const render = require("./controllers/render");
 const merchantData = require("./controllers/merchantData");
+const ingredientData = require("./controllers/ingredientData");
 
 module.exports = function(app){
     //Render page
@@ -22,9 +23,9 @@ module.exports = function(app){
     app.post("/merchant/recipes/ingredients/remove", merchantData.removeRecipeIngredient);
 
     //Ingredients
-    app.get("/ingredients", home.getIngredients);
-    app.post("/ingredients/create", home.createNewIngredients);
-    app.post("/ingredients/createone", home.createIngredient);  //also adds to merchant
+    app.get("/ingredients", ingredientData.getIngredients);
+    app.post("/ingredients/create", ingredientData.createNewIngredients);
+    app.post("/ingredients/createone", ingredientData.createIngredient);  //also adds to merchant
 
     //Transactions
     app.post("/transactions/create", home.createTransaction);