-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (155 loc) · 4.68 KB
/
index.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
const express = require('express')
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express()
const { MongoClient, ServerApiVersion } = require('mongodb');
require('dotenv').config()
const port = 3001
app.use(cors())
app.use(bodyParser.json())
// Connect to mongodb
const uri = `mongodb+srv://${process.env.USER_NAME}:${process.env.USER_PASS}@cluster0.llje0.mongodb.net/${process.env.DB_NAME}?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1
});
client.connect(err => {
const productsCollection = client.db("a-ama-clone").collection("products");
const deliveryCollection = client.db("a-ama-clone").collection("users-delivery-address");
const categoriesCollection = client.db("a-ama-clone").collection("categories");
const ordersCollection = client.db("a-ama-clone").collection("orders");
const adminsCollection = client.db("a-ama-clone").collection("admins");
app.get('/products', (req, res) => {
productsCollection.find({})
.toArray((err, docx) => {
docx = docx.sort(() => Math.random() - 0.5)
res.send(docx)
})
})
// Get products data by category
app.get('/products/:category', (req, res) => {
productsCollection.find({
category: req.params.category
})
.toArray((err, docx) => {
docx = docx.sort(() => Math.random() - 0.5)
res.send(docx)
})
})
// Get one product data by category & id
app.get('/product/:category/:id', (req, res) => {
productsCollection.findOne({
category: req.params.category,
id: req.params.id
})
.then(data => {
res.send(data)
})
})
// Get one product data by id
app.get('/product/:id', (req, res) => {
productsCollection.findOne({ id: req.params.id })
.then(data => {
res.send(data)
})
})
// Get limited products data by category
app.get('/products/limit/:limit', (req, res) => {
productsCollection.find({}).limit(req.params.limit)
.toArray((err, docx) => {
res.send(docx)
})
})
// Get products by search
app.get('/search', (req, res) => {
if (req.query.name.length > 0) {
productsCollection.find(
{
"$or": [
{ title: { $regex: req.query.name, $options: "$i" } },
{ brand: { $regex: req.query.name, $options: "$i" } },
{ category: { $regex: req.query.name, $options: "$i" } }
]
}
)
.toArray((err, docx) => {
res.status(200).send(docx)
})
} else {
res.status(400).message("Search value empty.")
}
})
// Set category
app.get('/categoriesAdd', (req, res) => {
// categoriesCollection.insertMany(categories)
res.send("Added...")
})
// Get category
app.get('/category', (req, res) => {
categoriesCollection.find({})
.toArray((err, docx) => {
re.status(200).send(docx)
})
})
// Get products by more id
app.post('/getproductsbymoreid', (req, res) => {
productsCollection.find({ id: { $in: req.body } })
.toArray((err, docx) => {
res.status(200).send(docx)
})
})
// Add a delivery address
app.post('/addADeliveryAddress', (req, res) => {
deliveryCollection.insertOne(req.body)
res.status(200).send("Deleivery Added Successfull.")
})
// Get delivery address
app.get('/get-delivery-address', (req, res) => {
deliveryCollection.find({
userId: req.query.userId,
})
.toArray((err, docx) => {
res.send(docx)
})
})
// Post order data in DB
app.post('/post-order', (req, res) => {
ordersCollection.insertOne(req.body)
.then(response => {
res.send(response)
// if (response.insertedCount === 1) {
// res.status(200).send("Order Place Successful...")
// }
})
})
// Send Admin Items
app.post('/admin/check', (req, res) => {
if (req.body.email) {
adminsCollection.findOne({ email: req.body.email })
} else {
res.status(407).message("Email Is Reaqured.")
}
})
// Post order data in DB
// app.get('/post-admin', (req, res) => {
// adminsCollection.insertOne({
// email: "mxasraful2000@gmail.com",
// role: "admin",
// that_added: "mxasraful2000@gmail.com"
// })
// .then(response => {
// res.send("Added...")
// })
// })
app.get('/productsAdd', (req, res) => {
productsCollection.insertMany(products)
.then(response => {
res.send("Added...")
})
})
});
app.get('/', (req, res) => {
res.send('Asrafuls amazon clone backend server is running......')
})
app.listen(process.env.PORT || port)