-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
212 lines (170 loc) · 5.65 KB
/
index.js
File metadata and controls
212 lines (170 loc) · 5.65 KB
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
/*This code uses express and ejs to render the index.ejs file from the views folder.
The app.listen(8080) function listens for requests on port 8080.
app.get is basically used to specify what function should execute when there is a GET request to the given path.*/
var express = require('express');
var ejs = require('ejs');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var session = require('express-session');
mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "node_project"
})
var app = express(); //instance of an Express application
//configure the Express app.
app.use(express.static('public'));
app.set('view engine', 'ejs'); //use ejs as the view engine
app.listen(8080); //starts the server on a port.
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({ secret: "secret" }))
function isProductInCart(cart, id) {
for (let i = 0; i < cart.length; i++) {
if (cart[i].id == id) {
return true;
}
}
return false;
}
function calculateTotal(cart, req) {
var total = 0;
for (let i = 0; i < cart.length; i++) {
if (cart[i].sale_price) {
total = total + (cart[i].sale_price * cart[i].quantity);
} else {
total = total + (cart[i].price * cart[i].quantity);
}
}
req.session.total = total;
return total;
}
//localhost:8080
//app.get is a route handler for GET requests to the given path.
app.get('/', function(req, res) {
//render the index.ejs file
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "node_project"
})
con.query("SELECT * FROM products", (err, result) => {
res.render('pages/index', { result: result });
})
});
app.post('/add_to_cart', function(req, res) {
var id = req.body.id;
var name = req.body.name;
var price = req.body.price;
var sale_price = req.body.sale_price;
var quantity = req.body.quantity;
var image = req.body.image;
var product = { id: id, name: name, price: price, sale_price: sale_price, quantity: quantity, image: image };
if (req.session.cart) {
var cart = req.session.cart;
if (!isProductInCart(cart, id)) {
cart.push(product);
}
} else {
req.session.cart = [product];
var cart = req.session.cart;
}
//to calculate total
calculateTotal(cart, req);
res.redirect('/cart');
});
app.get('/cart', function(req, res) {
var cart = req.session.cart;
var total = req.session.total;
res.render('pages/cart', { cart: cart, total: total });
});
app.post('/remove_product', function(req, res) {
var id = req.body.id;
var cart = req.session.cart;
for (let i = 0; i < cart.length; i++) {
if (cart[i].id == id) {
cart.splice(cart.indexOf(i), 1);
}
}
//re-calculate total
calculateTotal(cart, req);
res.redirect('/cart');
});
app.post('/edit_product_quantity', function(req, res) {
//get values from the inputs
var id = req.body.id;
var quantity = req.body.quantity;
var increase_btn = req.body.increase_product_quantity_btn;
var decrease_btn = req.body.decrease_product_quantity_btn;
var cart = req.session.cart;
if (increase_btn) {
for (let i = 0; i < cart.length; i++) {
if (cart[i].id == id) {
if (cart[i].quantity > 0) {
cart[i].quantity = parseInt(cart[i].quantity) + 1;
}
}
}
}
//similarly for decrease
if (decrease_btn) {
for (let i = 0; i < cart.length; i++) {
if (cart[i].id == id) {
if (cart[i].quantity > 1) {
cart[i].quantity = parseInt(cart[i].quantity) - 1;
}
}
}
}
calculateTotal(cart, req);
res.redirect('/cart');
});
app.get('/checkout', function(req, res) {
var total = req.session.total;
res.render('pages/checkout', { total: total });
});
app.post('/place_order', function(req, res) {
var name = req.body.name;
var email = req.body.email;
var phone = req.body.phone;
var city = req.body.city;
var address = req.body.address;
var cost = req.session.total;
var status = "not paid";
var date = new Date();
var products_ids = "";
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "node_project"
})
var cart = req.session.cart;
for (let i = 0; i < cart.length; i++) {
products_ids = products_ids + "," + cart[i].id;
}
con.connect((err) => {
if (err) {
console.log(err)
} else {
var query = "INSERT INTO orders(cost, name, email, status, city, address, phone, date, products_ids) VALUES ?";
var values = [
[cost, name, email, status, city, address, phone, date, products_ids]
];
con.query(query, [values], (err, result) => {
for (let i = 0; i < cart.length; i++) {
var query = "INSERT INTO order_items (order_id, product_id, product_name, product_price, product_image, product_quantity, order_date) VALUES ?";
var values = [
[id, cart[i].id, cart[i].name, cart[i].price, cart[i].image, cart[i].quantity, new Date()]
];
con.query(query, [values], (err, result) => {})
}
res.redirect('/payment')
});
}
});
});
app.get('/payment', function(req, res) {
res.render('pages/payment');
});