소스 검색

Save for branch

Lee Morgan 6 년 전
부모
커밋
33069b4d4b
5개의 변경된 파일84개의 추가작업 그리고 14개의 파일을 삭제
  1. 56 0
      controllers/home.js
  2. 7 7
      models/ingredient.js
  3. 4 4
      models/recipe.js
  4. 1 0
      routes.js
  5. 16 3
      views/merchantSetupPage/merchantSetup.js

+ 56 - 0
controllers/home.js

@@ -2,6 +2,7 @@ const axios = require("axios");
 
 const Merchant = require("../models/merchant");
 const Ingredient = require("../models/ingredient");
+const Recipe = require("../models/recipe");
 
 const merchantId = "HCVKASXH94531";
 const token = "f1c88a69-e3e4-059a-da06-8858d0636e82";
@@ -28,6 +29,7 @@ module.exports = {
             .then((ingredients)=>{
                 axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
                     .then((recipes)=>{
+                        console.log(recipes);
                         return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
                     })
                     .catch((err)=>{
@@ -50,5 +52,59 @@ module.exports = {
             .catch((err)=>{
                 return res.json(err);
             });
+    },
+
+    createMerchant: (req, res)=>{
+        let newIngredient = [];
+        let data = JSON.parse(req.body.data);
+        for(let ingredient of data.new){
+            newIngredient.push({
+                name: ingredient.name,
+                category: ingredient.category,
+                unitType: ingredient.unitType
+            });
+        }
+
+        Ingredient.create(newIngredient)
+            .then((ingredients)=>{
+                ingredientIds = [];
+                for(let i = 0; i < ingredients.length; i++){
+                    data.existing.push({
+                        id: ingredient[i]._id,
+                        quantity: data.new[i].quantity
+                    });
+                    ingredientIds.push({
+                        tempId: data.new[i].id,
+                        permId: ingredients[i]._id
+                    });
+                }
+
+                Recipe.create()
+                axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
+                    .then((merchant)=>{
+                        Merchant.create({
+                            cloverId: merchant._id,
+                            lastUpdateTime: Date.now,
+                            ingredients: data.existing
+                        })
+                            .then((merchant)=>{
+                                console.log(merchant);
+                            })
+                            .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");
+            });
     }
 }

+ 7 - 7
models/ingredient.js

@@ -3,7 +3,7 @@ const mongoose = require("mongoose");
 const IngredientSchema = new mongoose.Schema({
     name: {
         type: String,
-        minlength: [2, "Name of ingredient is two short.  Please have at least two characters"],
+        minlength: [2, "Name of ingredient is too short.  Please have at least two characters"],
         required: [true, "Must provide a name for the ingredient"]
     },
     category: {
@@ -12,12 +12,12 @@ const IngredientSchema = new mongoose.Schema({
     },
     unitType: {
         type: String,
-        required: [true, "You must provide the measurment unit for this item"]
-    },
-    recipes: [{
-        type: mongoose.Schema.Types.ObjectId,
-        ref: "Recipe"
-    }]
+        required: [true, "You must provide the measurement unit for this item"]
+    }
+    // recipes: [{
+    //     type: mongoose.Schema.Types.ObjectId,
+    //     ref: "Recipe"
+    // }]
 })
 
 module.exports = mongoose.model("Ingredient", IngredientSchema);

+ 4 - 4
models/recipe.js

@@ -10,10 +10,10 @@ const RecipeSchema = new mongoose.Schema({
         required: true,
         minlength: [3, "Name of recipe must contain at least three characters"]
     },
-    merchant: {
-        type: mongoose.Schema.Types.ObjectId,
-        ref: "Merchant"
-    },
+    // merchant: {
+    //     type: mongoose.Schema.Types.ObjectId,
+    //     ref: "Merchant"
+    // },
     ingredients: [{
         type: mongoose.Schema.Types.ObjectId,
         ref: "Ingredient"

+ 1 - 0
routes.js

@@ -4,4 +4,5 @@ module.exports = function(app){
     app.get("/", home.displayInventory);
     app.get("/merchant/new", home.merchantSetup);
     app.get("/getrecipes", home.getRecipes);
+    app.post("/merchant/create", home.createMerchant);
 }

+ 16 - 3
views/merchantSetupPage/merchantSetup.js

@@ -1,8 +1,7 @@
-console.log(ingredients);
-console.log(recipes);
-
 let state = 0;
 let data = {};
+console.log(ingredients);
+console.log(recipes);
 
 let addIngredients = document.querySelector("#addIngredients");
 let newIngredients = document.querySelector("#newIngredients");
@@ -264,7 +263,21 @@ let submitAll = ()=>{
         }
         data.recipes.push(newRecipe);
     }
+    
+
+    let form = document.createElement("form");
+    form.method = "post";
+    form.action = "/merchant/create"
+    
+    let dataInput = document.createElement("input");
+    dataInput.type = "hidden";
+    dataInput.name = "data";
+    dataInput.value = JSON.stringify(data);
+
+    form.appendChild(dataInput);
+    document.body.appendChild(form);
     console.log(data);
+    // form.submit();
 }
 
 populateIngredients();