-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoutes.js
303 lines (288 loc) · 9.3 KB
/
Routes.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
const express = require("express");
const connection = require("./dbconfig");
const Router = express.Router();
const jwt = require("jsonwebtoken");
const cookie = require("cookie");
const niceInvoice = require("nice-invoice");
const path = require("path");
Router.get("/", (req, res, next) => {
res.render("landing");
});
Router.get("/signup", (req, res, next) => {
res.render("signup");
});
Router.get("/signin", (req, res, next) => {
res.render("signin");
});
Router.get("/products", (req, res, next) => {
connection.query(`SELECT * FROM PRODUCTS`, function (err, result) {
if (err) throw err;
res.render("product", { products: result });
});
});
Router.get("/products/:sort_type", (req, res, next) => {
connection.query(`SELECT * FROM PRODUCTS`, function (err, result) {
if (err) throw err;
if(req.params.sort_type === 'PRICE_ASC'){
result.sort((a,b) => (a.productPrice >= b.productPrice) ? 1 : -1);
} else if(req.params.sort_type === 'PRICE_DESC'){
result.sort((a,b) => (a.productPrice >= b.productPrice) ? -1 : 1);
} else if(req.params.sort_type==='RATING'){
result.sort((a,b) => (a.productRating >= b.productRating) ? -1 : 1);
}
res.render("product", { products: result });
});
});
Router.post("/addCart", (req, res, next) => {
let cookies = cookie.parse(req.headers.cookie);
if (cookies.auth) {
jwt.verify(cookies.auth, "secret", function (err, decoded) {
if (err) {
res.redirect("/signin");
} else {
connection.query(
`SELECT * FROM ORDERS WHERE username='${decoded.data}' and productId='${req.body.id}'`,
function (err, previous_record) {
// console.log(previous_record);
if (err) throw err;
if (previous_record.length === 0) {
connection.query(
`INSERT INTO ORDERS VALUES('${decoded.data}', '${parseInt(
req.body.id
)}', '${parseInt("1")}')`,
function (err, result) {
if (err) throw err;
else console.log("Added to Cart");
}
);
} else {
connection.query(
`UPDATE ORDERS SET quantity='${
previous_record[0].quantity + 1
}' WHERE username='${
previous_record[0].username
}' and productId='${previous_record[0].productId}'`,
function (err, result) {
if (err) throw err;
else console.log("Updated Product Details");
}
);
}
}
);
}
});
} else {
res.redirect("/signin");
}
});
Router.post("/signup", (req, res, next) => {
connection.query(
`SELECT * from users where username='${req.body.username}'`,
function (err, result) {
if (err) throw err;
if (result.length === 0) {
connection.query(
`INSERT INTO users values('${req.body.username}', '${req.body.userpass}', '${req.body.useremail}','${req.body.usercountry}','${req.body.usergender}')`,
function (err, result) {
if (err) throw err;
console.log("Successful Registration");
let token = jwt.sign({ data: req.body.username }, "secret", {
expiresIn: "24hr",
});
connection.query(`SELECT * FROM PRODUCTS`, function (err, result) {
if (err) throw err;
res.cookie("auth", token).render("product", { products: result });
});
}
);
} else {
res.redirect("signup");
}
}
);
});
Router.post("/signin", (req, res, next) => {
connection.query(
`SELECT * from users where username='${req.body.username}' and password='${req.body.password}'`,
function (err, result) {
if (err) throw err;
if (result.length === 1) {
let token = jwt.sign({ data: req.body.username }, "secret", {
expiresIn: "24hr",
});
connection.query(`SELECT * FROM PRODUCTS`, function (err, result) {
if (err) throw err;
res.cookie("auth", token).render("product", { products: result });
});
} else {
res.redirect("signin");
}
}
);
});
Router.get("/cart", (req, res, next) => {
connection.query(
`SELECT * FROM orders NATURAL JOIN products`,
function (err, result) {
if (err) throw err;
else {
let total = 0;
let tax = 0;
for (let i = 0; i < result.length; i++) {
total += result[i].productPrice * result[i].quantity;
}
tax = 0.1 * total;
let grandTotal = total + tax;
res.render("cart", {
orders: result,
subtotal: total,
tax: tax,
grandtotal: grandTotal,
});
}
}
);
});
Router.post("/cart", (req, res, next) => {
let cookies = cookie.parse(req.headers.cookie);
if (cookies.auth) {
jwt.verify(cookies.auth, "secret", function (err, decoded) {
if (err) throw err;
else {
connection.query(
`DELETE FROM ORDERS WHERE username = '${
decoded.data
}' and productId='${parseInt(req.body.id)}'`,
function (err, result) {
if (!result.affectedRows) return;
if (err) throw err;
else {
console.log("Item Removed");
res.redirect("cart");
}
}
);
}
});
}
});
Router.post("/updateCart", (req, res, next) => {
let cookies = cookie.parse(req.headers.cookie);
if (cookies.auth) {
jwt.verify(cookies.auth, "secret", function (err, decoded) {
connection.query(
`SELECT * FROM ORDERS where username='${
decoded.data
}' and productId='${parseInt(req.body.id)}'`,
function (err, previous_record) {
//console.log(previous_record);
if (err) throw err;
else {
connection.query(
`UPDATE ORDERS SET quantity='${parseInt(
req.body.quantity
)}' WHERE username='${decoded.data}' and productId='${
previous_record[0].productId
}'`,
function (err, result) {
if (err) throw err;
else {
console.log("Updated Product Details");
res.redirect("cart");
}
}
);
}
}
);
});
}
});
Router.post(
"/checkout",
(req, res, next) => {
console.log("checkout");
let cookies = cookie.parse(req.headers.cookie);
if (cookies.auth) {
jwt.verify(cookies.auth, "secret", function (err, decoded) {
connection.query(
`SELECT * FROM ORDERS NATURAL JOIN products where username='${decoded.data}'`,
function (err, previous_record, fields) {
let items = [];
let total = 0,
tax = 0;
let order_id = Math.random() * 1e20;
for (let i = 0; i < previous_record.length; i++) {
total +=
previous_record[i].productPrice * previous_record[i].quantity;
let obj = {
item: previous_record[i].productName,
quantity: previous_record[i].quantity,
price: previous_record[i].productPrice,
tax: "10%",
};
items.push(obj);
}
tax = 0.1 * total;
const invoiceDetail = {
shipping: {
name: `${decoded.data}`,
},
items: items,
subtotal: total,
total: total + tax,
order_number: `${order_id}`,
header: {
company_name: "My Shop",
company_address:
"123 William Street 1th Floor New York, NY 123456",
},
footer: {
text: "Any footer text - you can add any text here",
},
currency_symbol: "$",
date: {
billing_date: `${Date(new Date())}`,
},
};
niceInvoice(
invoiceDetail,
path.join(__dirname, "public", `${order_id}.pdf`)
);
//res.sendFile(path.join(__dirname, 'invoice.pdf'));
if (err) throw err;
else {
for (let i = 0; i < previous_record.length; i++) {
connection.query(
`INSERT INTO past_orders VALUES('${decoded.data}','${
previous_record[i].productId
}', '${previous_record[i].quantity}', '${
new Date().toISOString().split("T")[0]
}', '${order_id}')`
);
}
connection.query(
`DELETE FROM ORDERS where username='${decoded.data}'`,
function (err, result) {
if (err) throw err;
else {
req.order_id = order_id;
next();
}
}
);
}
}
);
});
}
},
(req, res, next) => {
res.redirect(`/placed/${req.order_id}`);
}
);
Router.get("/placed/:order_id", (req, res, next) => {
res.render("placed", { link: req.params.order_id });
});
module.exports = Router;