-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
203 lines (170 loc) · 5.51 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
200
201
202
203
const express = require("express");
const { MongoClient } = require("mongodb");
const ObjectId = require("mongodb").ObjectId;
require("dotenv").config();
const cors = require("cors");
const fileUpload = require("express-fileupload");
const app = express();
// Set CSP headers
app.use((req, res, next) => {
res.setHeader('Content-Security-Policy', "default-src 'none'; font-src 'self' https://digital-service-server.vercel.app");
next();
});
// Serve your HTML files
app.use(express.static('index.js'));
// middleware area
app.use(cors());
app.use(express.json());
app.use(fileUpload());
const port = process.env.PORT || 5000;
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.sx8wv.mongodb.net/algo-digital?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
async function run() {
try {
await client.connect();
const database = client.db("algo-digital");
const servicesCollection = database.collection("services");
const teamCollection = database.collection("team-members");
const ordersCollection = database.collection("orders");
const usersCollection = database.collection("users");
const reviewsCollection = database.collection("reviews");
// get area
// get all services
app.get("/services", async (req, res) => {
const cursor = servicesCollection.find({});
const services = await cursor.toArray();
res.send(services);
});
//get a specific service
app.get("/services/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const service = await servicesCollection.findOne(query);
res.send(service);
});
// get team members
app.get("/team", async (req, res) => {
const cursor = teamCollection.find({});
const team = await cursor.toArray();
res.send(team);
});
//get orders
app.get("/orders", async (req, res) => {
const cursor = ordersCollection.find({});
const orders = await cursor.toArray();
res.send(orders);
});
//get reviews
app.get("/reviews", async (req, res) => {
const cursor = reviewsCollection.find({});
const reviews = await cursor.toArray();
res.send(reviews);
});
//get single review
app.get("/reviews/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const review = await reviewsCollection.findOne(query);
res.send(review);
});
// get a specific order
app.get("/orders/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const order = await ordersCollection.findOne(query);
res.json(order);
});
// get admin data
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 area
//post orders
app.post("/orders", async (req, res) => {
const order = req.body;
const result = await ordersCollection.insertOne(order);
res.json(result);
});
// post review
app.post("/reviews", async (req, res) => {
const review = req.body;
const result = await reviewsCollection.insertOne(review);
res.json(result);
});
//post user data
app.post("/users", async (req, res) => {
const user = req.body;
const result = await usersCollection.insertOne(user);
res.json(result);
});
// post a new service
app.post("/services", async (req, res) => {
const car = req.body;
const result = await servicesCollection.insertOne(car);
res.json(result);
});
//update area
// upsert user
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);
});
// add admin to db
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);
});
// delete area
//delete order
app.delete("/orders/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await ordersCollection.deleteOne(query);
res.json(result);
});
// delete service
app.delete("/services/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await servicesCollection.deleteOne(query);
res.json(result);
});
// delete review of a user
app.delete("/reviews/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await reviewsCollection.deleteOne(query);
res.json(result);
});
} finally {
// await client.close();
}
}
run().catch(console.dir);
app.get("/", (req, res) => {
res.send("ALGO DIGITAL SERVER");
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});