Lee Morgan 6 лет назад
Родитель
Сommit
cbd23a1268
5 измененных файлов с 70 добавлено и 1 удалено
  1. 22 0
      controllers/orderData.js
  2. 4 0
      models/order.js
  3. 2 0
      routes.js
  4. 21 0
      views/dashboardPage/dashboard.ejs
  5. 21 1
      views/dashboardPage/orders.js

+ 22 - 0
controllers/orderData.js

@@ -0,0 +1,22 @@
+const Order = require("../models/order.js");
+
+module.exports = {
+    getOrders: function(){
+        if(!req.session.user){
+            req.session.error = "Must be logged in to do that";
+            return res.redirect("/");
+        }
+
+        Order.aggregate([
+            {$match: {merchant: req.session.user}},
+            {$sort: {date: -1}},
+            {$limit: 25}
+        ]).toArray()
+            .then((orders)=>{
+                return res.json({orders});
+            })
+            .catch((err)=>{
+                return res.json("Error: unable to retrieve your orders");
+            });
+    }
+}

+ 4 - 0
models/order.js

@@ -21,6 +21,10 @@ const OrderSchema = new mongoose.Schema({
             type: Number,
             required: true,
             min: 0
+        },
+        price: {
+            type: Number,
+            min: 0
         }
     }]
 });

+ 2 - 0
routes.js

@@ -4,6 +4,7 @@ const ingredientData = require("./controllers/ingredientData");
 const otherData = require("./controllers/otherData");
 const transactionData = require("./controllers/transactionData");
 const recipeData = require("./controllers/recipeData");
+const orderData = require("./controllers/orderData.js");
 
 module.exports = function(app){
     //Render page
@@ -35,6 +36,7 @@ module.exports = function(app){
     app.put("/recipe/update", recipeData.updateRecipe);
 
     //Orders
+    app.get("/order", orderData.getOrders);
 
     //Other
     // app.post("/purchases/create", otherData.createOrder);

+ 21 - 0
views/dashboardPage/dashboard.ejs

@@ -83,6 +83,27 @@
 
             <div id="ordersStrand" class="strand">
                 <h1 class="strandTitle">Orders</h1>
+
+                <table id="orderList">
+                    <thead>
+                        <tr>
+                            <th>ID #</th>
+                            <th>Item Count</th>
+                            <th>Date</th>
+                            <th>Cost</th>
+                        </tr>
+                    </thead>
+                    <tbody></tbody>
+                </table>
+
+                <template id="order">
+                    <tr class="order">
+                        <td></td>
+                        <td></td>
+                        <td></td>
+                        <td></td>
+                    </div>
+                </template>
             </div>
         </div>
 

+ 21 - 1
views/dashboardPage/orders.js

@@ -1,5 +1,25 @@
 window.ordersStrandObj = {
+    isPopulated: false,
+
     display: function(){
-        console.log("displaying orders");
+        if(!this.isPopulated){
+            fetch("/orders", {
+                method: "GET",
+                headers: {
+                    "Content-Type": "application/json;charset=utf-8"
+                },
+            })
+                .then((response) => response.json())
+                .then((response)=>{
+                    if(typeof(response) === "string"){
+                        banner.createError(response);
+                    }else{
+                        
+                    }
+                })
+                .catch((err)=>{
+                    banner.createError("Unable to retrieve your orders at the moment");
+                });
+        }
     }
 }