-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
187 lines (162 loc) · 5.33 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
183
184
185
186
187
const express = require('express');
const cors = require('cors');
const { MongoClient } = require('mongodb');
const ObjectId = require('mongodb').ObjectId;
require('dotenv').config();
const app = express();
const port = process.env.PORT || 5000;
// middleware
app.use(
cors({
origin: 'https://niche-watch-shop.web.app'
})
);
app.use(express.json());
// const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.ytnhs.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.ytnhs.mongodb.net/`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true
});
async function run() {
try {
await client.connect();
const database = client.db('watch-Shop');
const watchesCollection = database.collection('watches');
const myBuyingWatchCollection = database.collection('myBuyingWatch');
const usersCollection = database.collection('users');
const userReviewCollection = database.collection('reviews');
// console.log('Database connected successfully');
//GET API for all watch products
app.get('/allWatches', async (req, res) => {
const cursor = watchesCollection.find({});
const watches = await cursor.toArray();
res.json(watches);
});
//GET Watch details
app.get('/watch/:id', async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await watchesCollection.findOne(query);
res.json(result);
});
// POST api for add products
app.post('/allWatches', async (req, res) => {
const watches = req.body;
const result = await watchesCollection.insertOne(watches);
res.json(result);
});
// DeleteProducts API
app.delete('/deleteProduct/:id', async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await watchesCollection.deleteOne(query);
res.send(result);
});
//GET ai fot my order list
app.get('/myBuyingList/:email', async (req, res) => {
const email = req.params.email;
const query = { email: email };
const cursor = myBuyingWatchCollection.find(query);
const result = await cursor.toArray();
res.json(result);
});
//POST api for my buying list
app.post('/myBuyingList/:email', async (req, res) => {
const email = req.params.email;
const myBookingInfo = req.body;
const result = await myBuyingWatchCollection.insertOne(myBookingInfo);
res.json(result);
});
// DELETE MyBooking api
app.delete('/deleteOrders/:id', async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await myBuyingWatchCollection.deleteOne(query);
res.send(result);
});
//manage All Orders api
app.get('/manageAllOrders', async (req, res) => {
const cusor = myBuyingWatchCollection.find({});
const result = await cusor.toArray();
res.send(result);
});
// UPDATE mange Booking api
app.put('/manageAllOrders/:id', async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const updatedOrder = req.body;
const options = { upsert: true };
const updateDoc = {
$set: {
status: updatedOrder.status
}
};
const result = await myBuyingWatchCollection.updateOne(
query,
updateDoc,
options
);
res.send(result);
});
// post user
app.post('/users', async (req, res) => {
const user = req.body;
const result = await usersCollection.insertOne(user);
res.json(result);
});
// update user to database
app.put('/users', async (req, res) => {
const user = req.body;
const filter = { email: user.email };
const options = { upsert: true };
const updateDoc = { $set: user };
const result = await usersCollection.updateOne(
filter,
updateDoc,
options
);
res.json(result);
});
//get admin
app.get('/users/:email', async (req, res) => {
const email = req.params.email;
const query = { email: email };
const user = await usersCollection.findOne(query);
let isAdmin = false;
if (user?.role === 'admin') {
isAdmin = true;
}
res.send({ admin: isAdmin });
});
// make an user admin
app.put('/users/admin', async (req, res) => {
const user = req.body;
const filter = { email: user.email };
const updateDoc = { $set: { role: 'admin' } };
const result = await usersCollection.updateOne(filter, updateDoc);
res.json(result);
});
//get all users reviews
app.get('/user/review', async (req, res) => {
const cursor = userReviewCollection.find({});
const result = await cursor.toArray();
res.json(result);
});
// user review post
app.post('/user/review', async (req, res) => {
const review = req.body;
const result = await userReviewCollection.insertOne(review);
res.json(result);
});
} finally {
// await client.close();
}
}
run().catch(console.dir);
app.get('/', (req, res) => {
res.send('Watch Dialz server running');
});
app.listen(port, () => {
console.log('Running server on port', port);
});