-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshop.js
194 lines (177 loc) · 4.77 KB
/
shop.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
const express = require("express");
const router = express.Router();
const Product = require("./models/product.js");
const User = require("./models/user.js");
const items_per_page = 4;
function ensureAuth(req, res, next) {
if (req.session.isLoggedIn) {
return next();
} else {
console.log("You must be logged in to do this!");
res.redirect("/add-product");
}
} //Similarly can add admin validation. All such validation should be done from the server side or the user can easily change such local variables
function ensureAdmin(req, res, next) {
if (req.session.admin) {
return next();
} else {
console.log("You must be an Administrator in to do this!");
}
}
router.get("/api/products/:slug", (req, res, next) => {
console.log(req.params.slug);
Product.findOne({
slug: req.params.slug
})
.then(product => {
console.log(product);
return res.send({
product:product
});
})
.catch(err => {
console.log(err);
return next();
});
});
// Return a list of all products we have to offer
router.get("/api/allProducts/", (req, res, next) => {
console.log(req.params.slug);
Product.find({
})
.then(products => {
return res.send({
products:products
});
})
.catch(err => {
console.log(err);
return next();
});
});
router.get("/api/productsSearch/:title", (req, res, next) => {
console.log(req.params.title);
Product.findOne({
title: req.params.title
})
.then(product => {
console.log(product);
return res.send({
product:product
});
})
.catch(err => {
console.log(err);
return next();
});
});
router.get("/api/shop", (req, res, next) => {
//react me bhi handle pass hua
const page = +req.query.page || 1; //?page=1,,if req.query.handle--> gets
const k = +req.query.sort || 0;
let category="all";
if (req.query.category !== "undefined") category = req.query.category;
else {
category = "all";
}
console.log(category);
Product.find()
.countDocuments()
.then(numProducts => {
totalItems = numProducts;
if (category === "all") {
console.log("all prods");
return Product.find()
.sort({ price: k })
.skip((page - 1) * items_per_page) //skip previous items
.limit(items_per_page); //go till another page only
} else {
console.log("selected prods");
return Product.find({ category: category })
.sort({ price: k })
.skip((page - 1) * items_per_page) //skip previous items
.limit(items_per_page); //go till another page only
}
})
.then(products => {
res.send({
prods: products,
currentPage: page,
hasPreviousPage: page > 1,
nextPage: page + 1,
previousPage: page - 1,
lastPage: Math.ceil(totalItems / items_per_page),
hasNextPage: items_per_page * page < totalItems,
sort: k
});
})
.catch(err => {
console.log(err);
console.log("couldnt fetch products");
});
});
//name in html file of input was title
router.post("/api/add-product", ensureAuth, (req, res, next) => {
//../ means go up one level
const product = new Product({
title: req.body.title,
price: req.body.price,
description: req.body.description,
img: req.body.img,
category: req.body.category
});
product
.save()
.then(result => {
console.log("Product added to DB");
})
.catch(err => {
console.log(err);
});
return next();
});
router.get("/api/cart", ensureAuth, (req, res, next) => {
if (typeof req.user === "undefined") {
res.send({ loggedIn: false });
console.log("not logged in");
return next();
}
req.user //this is making reference !! Super Important
.populate("cart.items.prodid")
.execPopulate()
.then(user => {
const products = user.cart.items;
res.send({ cart: products, loggedIn: true });
})
.catch(err => console.log(err));
});
router.post("/api/addToCart", ensureAuth, (req, res, next) => {
let id = req.body.productId;
Product.findById(id) //static methods are acessed using classname
.then(addP => {
return req.user.addToCart(addP);
})
.catch(err => {
console.log(err);
console.log("not added");
});
res.redirect("/shop");
});
router.post("/api/remove", ensureAuth, (req, res, next) => {
let id = req.body.productId;
let id2 = id.toString();
console.log("id2 is ", id2);
req.user
.removeFromCart(id2)
.then(result => {
res.redirect("/cart");
})
.catch(err => {
console.log("yahan error");
console.log(err);
});
});
module.exports = router;
//let [foo, bar] = await Promise.all([getFoo(), getBar()]);
// foo=await getfoo();;
// bar=await getBar();