-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcartController.js
78 lines (71 loc) · 1.96 KB
/
cartController.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
var db = require("../models");
var isAuthenticated = require('../config/middleware/isAuthenticated.js');
// ROUTES
function router(app) {
// show cart by user id
app.get("/cart", isAuthenticated, function(request, response) {
db.Cart
.findAll({
where: {
UserId: request.user.id
},
include: [db.Product]
})
.then(function(cartItems) {
response.render('cart', {cartItems, user: request.user});
// response.json(cartItems);
})
.catch(function(err) {
console.log(err.message);
response.send(err);
});
});
// add new item to cart
app.post("/cart/:itemId", isAuthenticated, function(request, response) {
db.Cart.create({
UserId: request.user.id,
ProductId: request.params.itemId,
quantity: request.body.quantity, // TODO
}).then(function(addedItem) {
// response.json(addedItem);
response.redirect('/products');
}).catch(function(err) {
console.log(err.message);
response.send(err);
});
});
//update quantity
app.put("/cart/:itemId", isAuthenticated, function(request, response) {
db.Cart
.update({quantity: request.body.quantity},{
where: {
UserId: request.user.id,
ProductId: request.params.itemId
},
include: [db.Product]
})
.then(function(cartItems) {
// response.json(cartItems);
response.redirect('/cart');
})
.catch(function(err) {
console.log(err.message);
response.send(err);
});
});
//delete item from cart
app.delete('/cart/:itemId', isAuthenticated, function(request, response) {
db.Cart.destroy({
where: {
UserId: request.user.id,
id: request.params.itemId,
}
}).then(function() {
response.redirect('/cart');
}).catch(function(err) {
console.log(err.message);
response.send(err);
});
});
}
module.exports = router;