-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
184 lines (155 loc) · 4.38 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
const express = require("express");
const cors = require("cors");
const jwt = require("jsonwebtoken");
const {
MongoClient,
ServerApiVersion,
ObjectId,
Timestamp,
} = require("mongodb");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 5000;
//MiddleWare
app.use(cors());
app.use(express.json());
const uri = `mongodb+srv://${process.env.DB_USER_NAME}:${process.env.DB_USER_PASSWORD}@cluster0.yfy0tas.mongodb.net/?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
});
//DB collections
const serviceCollection = client.db("dentisia").collection("services");
const reviewsCollection = client.db("dentReviews").collection("reviews");
//DB connect
async function dbConnect() {
try {
await client.connect();
console.log("Database is Connected");
} catch (error) {
console.log(error);
}
}
dbConnect();
//JWT Verification Middleware
const verifyJWT = (req, res, next) => {
const verifyHeader = req.headers.authorization;
if (!verifyHeader) {
res.status(401).send({ message: "Unauthorized Access" });
}
const token = verifyHeader.split(" ")[1];
jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, function (error, decoded) {
if (error) {
console.log(error.message);
} else {
req.decoded = decoded;
return next();
}
});
};
//For Home Section
app.get("/home", async (req, res) => {
try {
const cursor = serviceCollection.find({});
const services = await cursor.limit(3).sort({ _id: -1 }).toArray();
res.send(services);
} catch (error) {
console.log(error.message);
}
});
//For Services Section
app.get("/services", async (req, res) => {
try {
const cursor = serviceCollection.find({});
const services = await cursor.sort({ _id: -1 }).toArray();
res.send(services);
} catch (error) {
console.log(error.message);
}
});
app.get("/services/:id", async (req, res) => {
const { id } = req.params;
const result = await serviceCollection.findOne({ _id: ObjectId(id) });
res.send(result);
});
//Add Service
app.post("/services", async (req, res) => {
try {
const query = req.body;
const result = await serviceCollection.insertOne(query);
res.send(result);
} catch (error) {
console.log(error.message);
}
});
//Add Review
app.post("/reviews/", async (req, res) => {
try {
const query = req.body;
const { serviceId, email, username, photoURL, message, name , serviceImg} = query;
const result = await reviewsCollection.insertOne({
serviceId,
serviceImg,
email,
username,
photoURL,
message,
name,
timestamp: new Date(),
});
res.send(result);
} catch (error) {
console.log(error.message);
}
});
//Get Review
app.get("/reviews", async (req, res) => {
const query = req.query.serviceId;
const cursor = reviewsCollection.find({ serviceId: query });
const result = await cursor.sort({ timestamp: -1 }).toArray();
res.send(result);
});
//JWT Authorization
app.post("/jwt", async (req, res) => {
const user = req.body;
const token = jwt.sign(user, process.env.ACCESS_TOKEN_SECRET);
res.send({ token });
});
//Get my reviews with JWT Verification
app.get("/myreviews", verifyJWT, async (req, res) => {
const decoded = req.decoded;
if (decoded.email !== req.query.email) {
res.status(403).send({ message: "Forbidden" });
}
const query = req.query.email;
const cursor = reviewsCollection.find({ email: query });
const result = await cursor.sort({ _id: -1 }).toArray();
res.send(result);
});
//Delete my reviews
app.delete("/myreviews/:id", async (req, res) => {
const { id } = req.params;
const result = await reviewsCollection.deleteOne({ _id: ObjectId(id) });
res.send(result);
});
//Edit my Review
app.get("/editreview/:id" , async(req,res)=>{
const {id} = req.params;
const result = await reviewsCollection.findOne({_id: ObjectId(id)})
res.send(result)
})
app.patch("/editreview/:id", async (req, res) => {
const { id } = req.params;
const message = req.body.message;
const result = await reviewsCollection.updateOne(
{ _id: ObjectId(id) },
{ $set: { message: message } }
);
res.send(result);
});
//Server Connection Status
app.get("/", (req, res) => {
res.send("API is Running");
});
app.listen(port, () => console.log("Server is running through port: ", port));