فهرست منبع

Update createRecipe to work with new session id.

Lee Morgan 5 سال پیش
والد
کامیت
1d72f4a609
4فایلهای تغییر یافته به همراه5 افزوده شده و 16 حذف شده
  1. 0 1
      controllers/orderData.js
  2. 0 1
      controllers/otherData.js
  3. 4 13
      controllers/recipeData.js
  4. 1 1
      routes.js

+ 0 - 1
controllers/orderData.js

@@ -1,5 +1,4 @@
 const Order = require("../models/order.js");
-const Merchant = require("../models/merchant.js");
 
 const helper = require("./helper.js");
 

+ 0 - 1
controllers/otherData.js

@@ -2,7 +2,6 @@ const Merchant = require("../models/merchant");
 
 const bcrypt = require("bcryptjs");
 const axios = require("axios");
-const path = require("path");
 
 module.exports = {
     /*

+ 4 - 13
controllers/recipeData.js

@@ -22,13 +22,8 @@ module.exports = {
     Return = newly created recipe in same form as above, with _id
     */
     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,
+            merchant: res.locals.merchant._id,
             name: req.body.name,
             price: Math.round(req.body.price * 100),
             ingredients: req.body.ingredients
@@ -36,6 +31,9 @@ module.exports = {
 
         recipe.save()
             .then((newRecipe)=>{
+                res.locals.merchant.recipes.push(recipe);
+                res.locals.merchant.save().catch((err)=>{throw err});
+
                 return res.json(newRecipe);
             })
             .catch((err)=>{
@@ -47,13 +45,6 @@ module.exports = {
                 }
                 return res.json("ERROR: UNABLE TO SAVE INGREDIENT");
             });
-
-        Merchant.findOne({_id: req.session.user})
-            .then((merchant)=>{
-                merchant.recipes.push(recipe);
-                return merchant.save();
-            })
-            .catch((err)=>{});
     },
 
     /*

+ 1 - 1
routes.js

@@ -34,7 +34,7 @@ module.exports = function(app){
     app.delete("/ingredients/remove/:id", session, ingredientData.removeIngredient);
 
     //Recipes
-    app.post("/recipe/create", recipeData.createRecipe);
+    app.post("/recipe/create", session, recipeData.createRecipe);
     app.put("/recipe/update", recipeData.updateRecipe);
     app.delete("/recipe/remove/:id", recipeData.removeRecipe);
     app.get("/recipe/update/clover", recipeData.updateRecipesClover);