-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
278 lines (242 loc) · 7.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
const express = require("express");
const app = express();
const pool = require("./db");
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.set("view engine", "ejs");
const logData = (query, output) => {
console.log("~~~~Query/ Function~~~~");
console.log(query, "\n");
console.log("~~~~Output~~~~");
console.log(output, "\n", "\n");
};
const errorProduced = "";
app.get("/", async (req, res) => {
try {
const productData = await pool.query("SELECT * FROM product;");
logData("SELECT * FROM product;", productData.rows);
res.render("homepage", { productList: productData.rows , error:"Sorry Some Error has occured"});
} catch (err) {
console.log(err);
}
});
app.get("/product", async (req, res) => {
try {
const newOrder = await pool.query("SELECT * FROM product;");
res.send(newOrder);
} catch (err) {
console.log(err);
}
});
app.get("/caterer", async (req, res) => {
try {
const productList = await pool.query("SELECT * FROM product;");
logData("SELECT * FROM product;", productList.rows);
const orderList = await pool.query("SELECT * FROM orderTable;");
logData("SELECT * FROM orderTable;", orderList);
res.render("caterer", {
productList: productList.rows,
orderList: orderList.rows,
error: errorProduced
});
} catch (err) {
console.log(err);
}
});
// get product list
app.get("/caterer/product", async (req, res) => {
try {
const newOrder = await pool.query("SELECT * FROM product;");
logData("SELECT * FROM product;", newOrder.rows);
res.render(caterer);
} catch (err) {
console.log(err);
}
});
//
app.post("/caterer/product/new", async (req, res) => {
try {
const orderData = req.body;
// const newOrder = await pool.query(
// "INSERT INTO product (product_id, product_name, availability, price) VALUES ($1, $2, $3, $4);",
// [
// orderData.product_id,
// orderData.product_name,
// orderData.availability,
// orderData.price,
// ]
// );
const newProduct = await pool.query(
"CALL newProduct($1, $2, $3, $4,$5);",
[
parseInt(orderData.product_id),
parseInt(orderData.price),
String(orderData.product_name),
Boolean(orderData.availability),
parseInt(orderData.mat_req_id)
]
)
logData("CALL newProduct($1, $2, $3, $4)", newProduct);
res.redirect("/caterer");
} catch (err) {
console.log(err);
}
});
//delete the product
app.post("/caterer/product/delete", async (req, res) => {
try {
const productData = req.body;
const queryResult = await pool.query(
"call deleteProduct($1);",
[parseInt(productData.product_id)]
);
res.redirect("/caterer");
} catch (err) {
console.log(err);
}
});
// get pending order
app.get("/caterer/pendingOrder", async (req, res) => {
try {
const orderData = await pool.query("SELECT getPendingOrder();");
res.redirect("/caterer");
console.log(orderData);
} catch (err) {
console.log(err);
}
});
app.get("/analytics", async (req, res) => {
try {
const totalSale = await pool.query("SELECT getTotalSale();");
const totalOrders = await pool.query("SELECT getTotalOrders();");
const customerCount = await pool.query("SELECT getCustomerCount();");
const materialCost = await pool.query("SELECT getMaterialCost();");
const maxPayingCustomer = await pool.query("SELECT getMaxPayingCustomer();");
const pendingOrder = await pool.query("SELECT getPendingOrder();")
const feedbackList = await pool.query("SELECT * from feedback;")
const pendingOrderCount = pendingOrder.rows.length;
// logData("SELECT getTotalSale();",totalSale.rows);
// logData("SELECT getTotalOrders();",totalOrders.rows);
// logData("SELECT getCustomerCount();",customerCount.rows);
// logData("SELECT getMaterialCost();",materialCost.rows);
// logData("SELECT getMaxPayingCustomer();", maxPayingCustomer.rows);
// logData("SELECT getPendingOrder();", pendingOrder.rows);
res.render("analytics",
{
totalSale: totalSale.rows[0].gettotalsale,
totalOrders: totalOrders.rows[0].gettotalorders,
customerCount: customerCount.rows[0].getcustomercount,
materialCost: materialCost.rows[0].getmaterialcost,
maxPayingCustomer: maxPayingCustomer.rows[0].getmaxpayingcustomer,
feedback: feedbackList.rows,
error:"Sorry some error!"
});
} catch (err) {
console.log(err);
res.redirect("/");
}
});
app.get("/customer", async (req, res) => {
try {
const customerData = await pool.query("SELECT * from customer;")
res.render("customer",{customerList: customerData.rows, customerSelected: false, customerData: [], error: 'Please add initial values'});
} catch (err) {
console.log(err);
res.redirect("/");
}
});
app.post("/customer/new", async (req, res) => {
const formData = req.body;
try {
const customerData = await pool.query("CALL newCustomer($1,$2)", [formData.customerName,formData.customerId])
logData("CALL newCustomer($1,$2), [formData.customerName,formData.customerId]",customerData.rows)
res.redirect("/customer");
} catch (err) {
console.log(err);
res.redirect("/customer");
}
});
app.post("/customer/getDetails" ,async (req, res) => {
const customer_id = req.body.customer_id;
try {
const customerData = await pool.query("SELECT getCustomerDetails($1)",[customer_id]);
console.log(customerData);
const allCustomerData = await pool.query("SELECT * from customer;")
logData("SELECT getCustomerDetails($1)", customerData.rows)
res.render("customer",{customerList: allCustomerData.rows, customerSelected: true, customerData: customerData.rows, error: 'Please add initial values'});
} catch (err) {
console.log(err);
res.redirect("/");
}
});
app.post("/order/new", async (req, res) => {
try {
const orderData = req.body;
console.log(String(orderData.product_list).split(',').map(Number));
const resultData = await pool.query(
"CALL newOrder($1,$2,$3,$4,$5);", [
parseInt(orderData.product_id),
String(orderData.customer_id),
parseInt(orderData.quantity),
String(orderData.product_list).split(',').map(Number),
// JSON.parse("[" + JSON.stringify(orderData.product_list) + "]"),
String(orderData.customer_name)
]
)
// logData("CALL newOrder( customer_id, product_id, items, customer_NAME) VALUES ($1,$2,$3);", [
// int(orderData.product_id),
// String(orderData.customer_id),
// int(orderData.quantity),
// String(orderData.customer_name)
// ]);
res.redirect("/");
} catch (err) {
console.log(err);
}
});
app.post("/order/cancel", async (req, res) => {
try {
const orderData = req.body;
console.log(orderData);
const queryResult = await pool.query(
"call deleteOrder($1);",
[parseInt(orderData.order_id)]
);
console.log("done");
res.redirect("/");
} catch (err) {
console.log(err);
}
});
app.post("/caterer/order/update", async (req, res) => {
try {
const orderData = req.body;
console.log(orderData);
const queryResult = await pool.query(
"UPDATE orderTable SET processed = TRUE WHERE order_id = $1",
[parseInt(orderData.updateOrder_id)]
);
console.log("done");
res.redirect("/caterer");
} catch (err) {
console.log(err);
}
});
app.post("/feedback/new", async (req, res) => {
try {
const orderData = req.body;
console.log(orderData);
const queryResult = await pool.query(
"call newFeedback($1,$2,$3)",
[String(orderData.customer_id),String(orderData.customer_name),String(orderData.feedback_desc)]
);
console.log("done");
res.redirect("/");
} catch (err) {
console.log(err);
}
});
app.listen(5000, () => {
console.log("server Running");
});