-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
159 lines (137 loc) · 3.45 KB
/
server.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
var express = require('express');
var bodyParser = require('body-parser');
var { ObjectID } = require('mongodb');
var { mongoose } = require('./db/mongoose');
var { Product } = require('./models/Product');
var { User } = require('./models/user');
var { myCart } = require('./models/myCart');
var { Current } = require('./models/Current');
var app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// 1 post products add it to inventory ||
app.post('/product', (req, res) => {
var product = new Product({
id: req.body.id,
name: req.body.name,
description: req.body.description,
price: req.body.price,
rating: req.body.rating,
image: req.body.image
});
product.save().then((doc) => {
res.send(doc);
}, (e) => {
res.status(400).send(e);
});
});
//2 get to grab the inventory ||
app.get('/products', (req, res) => {
Product.find().then((products) => {
res.send({ products });
}, (e) => {
res.status(400).send(e);
});
});
// 3 post signup ||
app.post('/signup', (req, res) => {
var user = new User({
name: req.body.name,
email: req.body.email,
mobile: req.body.mobile,
password: req.body.password
});
user.save().then((doc) => {
res.send(doc);
}, (e) => {
res.status(400).send(e);
});
});
// 4 get signin ||
app.post('/signin', (req, res) => {
User.find({ email: req.body.email, password: req.body.password }).then((result) => {
if(result.length === 0){
res.send([])
return
}
Current.remove({}, (done) => {
var current = new Current({
name: result[0].name,
email: result[0].email
})
current.save().then((doc) => {
res.send(doc);
}, (e) => {
res.status(400).send(e);
})
})
myCart.remove({}, (done) => {
})
})
})
//to get the name and email of the current user ||
app.get('/current', (req, res) => {
Current.find().then((result) => {
var resp = {
name: result[0].name,
email: result[0].email
}
res.send(resp);
})
})
//add to cart ||
app.post('/addtocart', (req, res) => {
var id = req.body.id
Current.find().then((result) => {
email = result[0].email
var mycart = new myCart({
id: id,
email: email,
quantity: 1
});
mycart.save().then((doc) => {
res.send(doc);
}, (e) => {
res.status(400).send(e);
})
})
})
//delete item from cart ||
app.delete('/deletefromcart', (req, res) => {
var id = req.body.id
Current.find().then((result) => {
email = result[result.length - 1].email
myCart.findOneAndRemove({ id: req.body.id, email: email }, (done) => {
res.send("deleted")
})
})
})
//populate cart
app.post('/cart', (req, res) => {
var myemail = req.body.email
let promises = []
let response = []
myCart.find({ email: myemail }).then((result) => {
result.forEach((item) => {
promises.push(Product.find({ id: item.id }))
})
return Promise.all(promises)
})
.then((data) => {
let modifieddata = []
data.forEach((item) => {
const objectData = item[0]
modifieddata.push(objectData)
})
res.send(modifieddata)
}).catch()
})
app.listen(port, () => {
console.log(`Started up at port ${port}`);
});
module.exports = { app };