-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (139 loc) · 5.14 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
// express
const express = require("express");
const jwt = require("jsonwebtoken");
const app = express();
const port = process.env.PORT || 5000;
//Mongodb
const { MongoClient, ServerApiVersion, ObjectId } = require('mongodb');
// corse & dotenv
const cors = require("cors")
require("dotenv").config()
app.use(cors());
app.use(express.json());
// Mongdb configuration
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.lbqhd62.mongodb.net/?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
// jwt verfication
function verifyJWT(req, res, next) {
const authHeaders = req.headers.authorization;
if (!authHeaders) {
res.status(401).send("Unauthorized Request")
}
// verifying token
const token = authHeaders.split(" ")[1];
jwt.verify(token, process.env.ACCESS_TOKEN, function (error, decoded) {
if (error) {
res.status(403).send("Unauthorized Access")
}
req.decoded = decoded;
next();
})
}
async function run() {
try {
// jwt token generation
app.post("/jwt", (req, res) => {
const user = req.body;
const token = jwt.sign(user, process.env.ACCESS_TOKEN, { expiresIn: "1h" });
res.send({ token });
})
// service collection
const serviceCollections = client.db("consultio").collection("services");
// reviews collection
const customerReviewCollection = client.db("consultio").collection("customerReviews");
// service api
app.get("/services", async (req, res) => {
const query = {}
const cursor = serviceCollections.find(query);
const services = await cursor.toArray();
res.send(services);
})
// add service
app.post("/addservice", async (req, res) => {
const query = req.body;
const result = serviceCollections.insertOne(query);
res.send(result)
})
// home page limit service api
app.get("/services/limited", async (req, res) => {
const query = {}
const cursor = serviceCollections.find(query).sort({ _id: -1 });
const services = await cursor.limit(3).toArray();
res.send(services);
});
// get single service
app.get("/services/:id", async (req, res) => {
const service_id = req.params.id;
const query = { _id: ObjectId(service_id) };
const singleServiceDetails = await serviceCollections.findOne(query);
res.send(singleServiceDetails);
});
// Get Reviews for single service
app.get("/reviews/:serviceId", async (req, res) => {
const serviceId = req.params.serviceId;
const query = { serviceId: serviceId };
const cursor = customerReviewCollection.find(query).sort({ date: -1 });
const result = await cursor.toArray();
res.send(result)
})
// Post Reviews
app.post("/reviewpost", async (req, res) => {
const query = req.body;
const result = await customerReviewCollection.insertOne(query);
res.send(result);
});
// get all reviews of an user
app.get("/userreviews", verifyJWT, async (req, res) => {
let query = {};
if (req.query.email) {
query = {
email: req.query.email
}
} else {
query = {
uid: req.query.uid
}
}
const cursor = customerReviewCollection.find(query);
const result = await cursor.toArray();
res.send(result);
})
// get a single review
app.get("/review/:id", async (req, res) => {
const reviewId = req.params.id;
const query = { _id: ObjectId(reviewId) };
const result = await customerReviewCollection.findOne(query);
res.send(result)
});
// update a single review
app.patch("/update/:id", async (req, res) => {
const data = req.body;
const reviewId = req.params.id;
const query = { _id: ObjectId(reviewId) };
const updateDoc = {
$set: {
customerRating: data.customerRating,
customerReview: data.customerReview
}
}
const result = await customerReviewCollection.updateOne(query, updateDoc)
res.send(result)
})
// delete a review
app.delete("/delete/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) }
const result = await customerReviewCollection.deleteOne(query);
res.send(result);
})
}
finally {
}
}
run().catch(error => console.log(error));
app.get("/", (req, res) => {
res.send("Consultio server is running !");
})
app.listen(port, () => {
console.log("Consultio server is running at port : ", port);
})