-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
199 lines (173 loc) · 6.3 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
188
189
190
191
192
193
194
195
196
197
198
199
const express = require('express');
const { MongoClient } = require('mongodb');
const cors = require('cors');
const app = express();
require('dotenv').config();
const ObjectId = require('mongodb').ObjectId;
//PORT
const port = process.env.PORT || 5000;
// Middleware
app.use(cors());
app.use(express.json());
//MongoBD Connection
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.kbuol.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
async function run() {
try {
await client.connect();
const database = client.db("shop-and-shoot");
const haiku = database.collection("test");
const productCollection = database.collection("productCollection");
const purchaseInitCollection = database.collection("purchaseInitCollection");
const ordersCollection = database.collection("ordersCollection");
const usersCollection = database.collection("usersCollection");
const reviewCollection = database.collection("reviewCollection");
const accessoriesCollection = database.collection("accessoriesCollection");
// GET Products API
app.get('/products', async (req, res) => {
const cursor = productCollection.find({});
const products = await cursor.toArray();
res.send(products);
});
// GET Accessories API for home page
app.get('/accessories', async (req, res) => {
const cursor = accessoriesCollection.find({});
const accessory = await cursor.toArray();
res.send(accessory);
});
// GET all users API
app.get('/users', async (req, res) => {
const cursor = usersCollection.find({});
const users = await cursor.toArray();
res.send(users);
});
// GET Orders API
app.get('/orders', async (req, res) => {
const cursor = ordersCollection.find({});
const orders = await cursor.toArray();
res.send(orders);
});
// GET specific Orders API
app.get('/orders/:id', async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const singleOrder = await ordersCollection.findOne(query);
res.send(singleOrder);
});
// GET API (Get single product by id)
app.get('/products/:id', async (req, res) => {
const id = req.params.id;
console.log('getting a single product', id);
const query = { _id: ObjectId(id) };
const singleProduct = await productCollection.findOne(query);
res.json(singleProduct);
});
// GET API (Check whether admin or not)
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.json({admin: isAdmin});
})
// POST API (Post users info email password regestration)
app.post('/users', async (req, res) => {
const user = req.body;
const result = await usersCollection.insertOne(user);
res.json(result);
})
// PUT API ( upsert userinfo for Google login)
app.put('/users', async (req, res) => {
const user = req.body;
const filter ={email : user.email};
const option = {upsert:true};
const updateDoc = {$set: user};
const result = await usersCollection.updateOne(filter, updateDoc, option);
res.json(result);
})
// PUT API (add role to Make an 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);
})
// POST API (Post Orders)
app.post('/orders', async (req, res) => {
const order = req.body;
const status2 = { status: 'pending' };
const userOrder = { ...order, ...status2 };
const result = await ordersCollection.insertOne(userOrder);
res.json(result);
})
// POST API (Proceed Order)
app.post('/purchaseinit', async (req, res) => {
const purchaseInit = req.body;
const result = await purchaseInitCollection.insertOne(purchaseInit);
res.json(result);
});
// POST Review API
app.post('/reviews', async (req, res)=>{
const review = req.body;
const result = await reviewCollection.insertOne(review);
res.json(result);
});
// GET Review API
app.get('/reviews', async (req, res) => {
const cursor = reviewCollection.find({});
const reviews = await cursor.toArray();
res.send(reviews);
});
// POST API (Add new Product)
app.post('/products', async (req, res) => {
const product = req.body;
const result = await productCollection.insertOne(product);
// console.log("hit the post");
res.json(result);
});
// UPDATE API (Update shipping info)
app.put('/orders/:id', async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const updateDoc = {
$set: {
status: "Shipped"
},
};
const result = await ordersCollection.updateOne(query, updateDoc);
// console.log(result);
res.json(result);
})
// DELETE API (Delete order from my orders)
app.delete('/orders/:id', async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await ordersCollection.deleteOne(query);
console.log('deleting id', result)
res.json(result);
});
// DELETE API (Delete product from my products)
app.delete('/products/:id', async (req, res) => {
let id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await productCollection.deleteOne(query);
console.log('deleting id', result)
res.json(result);
});
// const result = await haiku.insertOne(doc);
// console.log(`A document was inserted with the _id: ${result.insertedId}`);
} finally {
// await client.close();
}
}
run().catch(console.dir);
app.get('/', (req, res) => {
res.send('Hello World! the shop and shoot server has just started! Happy Browsing!!! X-D');
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
})