Просмотр исходного кода

Add creation of recipes for non-pos users

Lee Morgan 6 лет назад
Родитель
Сommit
6924807148

+ 0 - 2
controllers/merchantData.js

@@ -6,8 +6,6 @@ const Recipe = require("../models/recipe");
 const InventoryAdjustment = require("../models/inventoryAdjustment");
 const RecipeChange = require("../models/recipeChange");
 
-const token = "b48068eb-411a-918e-ea64-52007147e42c";
-
 module.exports = {
     //POST - Create a new merchant with no POS system
     //Inputs:

+ 41 - 0
controllers/recipeData.js

@@ -0,0 +1,41 @@
+const Recipe = require("../models/recipe");
+const Merchant = require("../models/merchant");
+
+module.exports = {
+    //POST - creates a single new recipe
+    //Inputs:
+    //  req.body.name: name of recipes
+    //Returns the newly created recipe
+    createRecipe: function(req, res){
+        if(!req.session.user){
+            req.session.error = "Must be logged in to do that";
+            return res.redirect("/");
+        }
+
+        let recipe = new Recipe({
+            merchant: req.session.user,
+            name: req.body.name,
+            ingredients: []
+        });
+
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                merchant.recipes.push(recipe);
+                merchant.save()
+                    .catch((err)=>{
+                        return res.json("Error: unable to save recipe");
+                    });
+            })
+            .catch((err)=>{
+                return res.json("Error: unable to retrieve user data");
+            })
+
+        recipe.save()
+            .then((newRecipe)=>{
+                return res.json(newRecipe);
+            })
+            .catch((err)=>{
+                return res.json("Error: unable to save new ingredient");
+            });
+    }
+}

+ 4 - 0
routes.js

@@ -3,6 +3,7 @@ const merchantData = require("./controllers/merchantData");
 const ingredientData = require("./controllers/ingredientData");
 const otherData = require("./controllers/otherData");
 const transactionData = require("./controllers/transactionData");
+const recipeData = require("./controllers/recipeData");
 
 module.exports = function(app){
     //Render page
@@ -29,6 +30,9 @@ module.exports = function(app){
     app.get("/ingredients", ingredientData.getIngredients);
     app.post("/ingredients/createone", ingredientData.createIngredient);  //also adds to merchant
 
+    //Recipes
+    app.post("/recipe/create", recipeData.createRecipe);
+
     //Other
     app.post("/transactions/create", otherData.createTransaction);  //Creates transaction for non-pos merchant
     app.post("/purchases/create", otherData.createPurchase);

+ 5 - 0
views/inventoryPage/inventory.css

@@ -41,6 +41,11 @@
         margin-bottom: 10px;
     }
 
+    #newRecipe{
+        display: none;
+        justify-content: center;
+    }
+
     #recipesContainer{
         display: flex;
         justify-content: space-around;

+ 11 - 1
views/inventoryPage/inventory.ejs

@@ -47,11 +47,21 @@
 
         <div id="recipesStrand" class="strand">
             <div>
-                <% if(merchant.pos !== "none"){ %>
+                <% if(merchant.pos === "none"){ %>
+                    <button class="button" onclick="window.recipesObj.showInput()">Add a Recipe</button>
+                <% }else{%>
                     <button class="button" id="recipeUpdate" onclick="recipesObj.updateRecipes()">Update Recipes</button>
                 <% } %>
             </div>
 
+            <div id="newRecipe">
+                <input type="text">
+
+                <button class="button-small" onclick="window.recipesObj.submitNew()">Create</button>
+
+                <button class="button-small" onclick="window.recipesObj.cancelAdd()">Cancel</button>
+            </div>
+            
             <div id="recipesContainer"></div>
         </div>
 

+ 35 - 0
views/inventoryPage/recipes.js

@@ -67,5 +67,40 @@ window.recipesObj = {
             .catch((err)=>{
                 banner.createError("There was an error and your recipes could not be updated");
             });
+    },
+
+    showInput: function(){
+        document.querySelector("#newRecipe").style.display = "block";
+    },
+
+    cancel: function(){
+        document.querySelector("#newRecipe input").value = "";
+        document.querySelector("#newRecipe").style.display = "none";
+    },
+
+    submitNew: function(){
+        let inputDiv = document.querySelector("#newRecipe");
+        let input = document.querySelector("#newRecipe input");
+
+        let data = {
+            name: input.value
+        };
+
+        input.value = "";
+        inputDiv.style.display = "none";
+
+        axios.post("/recipe/create", data)
+            .then((response)=>{
+                if(typeof(response.data) === "string"){
+                    banner.createError(response.data);
+                }else{
+                    merchant.recipes.push(response.data);
+
+                    this.populateRecipes();
+                }
+            })
+            .catch((err)=>{
+                banner.createError("Error: Unable to display new data");
+            });
     }
 }