-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
192 lines (163 loc) · 5.68 KB
/
Copy pathindex.js
File metadata and controls
192 lines (163 loc) · 5.68 KB
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
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const { MongoClient, ServerApiVersion, ObjectId } = require("mongodb");
const admin = require("firebase-admin");
const app = express();
const port = process.env.PORT || 3000;
const decoded = Buffer.from(
process.env.FIREBASE_SERVICE_KEY,
"base64"
).toString("utf-8");
var serviceAccount = JSON.parse(decoded);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
app.use(
cors({
origin: [
"http://localhost:5173",
"https://assignment-11-category-10.web.app",
],
credentials: true,
})
);
app.use(express.json());
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.zwhgf1c.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0`;
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
});
const verifyJWT = async (req, res, next) => {
const token = req?.headers?.authorization?.split(" ")[1];
if (!token) return res.status(401).send({ message: "Unauthorized Access!" });
try {
const decoded = await admin.auth().verifyIdToken(token);
req.tokenEmail = decoded.email;
next();
} catch (err) {
return res.status(401).send({ message: "Unauthorized Access!" });
}
};
async function run() {
try {
const VolunteerDB = client.db("VolunteerDB");
const userData = VolunteerDB.collection("userData");
const VolunteerNeedPost = VolunteerDB.collection("VolunteerNeedPost");
const VolunteerDetails = VolunteerDB.collection("VolunteerDetails");
app.get("/AllVolunteerNeedposts", async (req, res) => {
const result = await VolunteerNeedPost.find().toArray();
res.send(result);
});
app.get("/AllVolunteerNeedposts/Search", async (req, res) => {
const { searchParams } = req.query;
let query = {};
if (searchParams) {
query = { posttitle: { $regex: searchParams, $options: "i" } };
}
const result = await VolunteerNeedPost.find(query).toArray();
res.send(result);
});
app.get("/VolunteerDetails/:email", verifyJWT, async (req, res) => {
const email = req.params.email;
const query = { volunteeremail: email };
const result = await VolunteerDetails.find(query).toArray();
res.send(result);
});
app.get(
"/AllVolunteerNeedposts/volunteerneedpostdetailspage/:id",
verifyJWT,
async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const result = await VolunteerNeedPost.findOne(query);
res.send(result);
}
);
app.get("/AddVolunteerNeedPost/featuresdete", async (req, res) => {
const today = new Date();
const yyyy = today.getFullYear();
const mm = String(today.getMonth() + 1).padStart(2, "0");
const dd = String(today.getDate()).padStart(2, "0");
const formattedToday = `${yyyy}-${mm}-${dd}`;
const query = { startDate: { $gt: formattedToday } };
const result = await VolunteerNeedPost.find(query)
.sort({ startDate: 1 })
.limit(6)
.toArray();
res.json(result);
});
app.get("/ManageMyPosts/:email", verifyJWT, async (req, res) => {
const email = req.params.email;
const query = { organizeremail: email };
const result = await VolunteerNeedPost.find(query).toArray();
res.send(result);
});
app.post("/signup", async (req, res) => {
const data = req.body;
const result = await userData.insertOne(data);
res.send({ message: "Data Insert Success", data: result });
});
app.post("/AddVolunteerNeedPost", verifyJWT, async (req, res) => {
const data = req.body;
const result = await VolunteerNeedPost.insertOne(data);
res.send({ message: "Data Insert Success", data: result });
});
app.post("/VolunteerDetails", verifyJWT, async (req, res) => {
const data = req.body;
const result = await VolunteerDetails.insertOne(data);
res.send({ message: "Data Insert Success", data: result });
});
app.put("/Myvolunteerneedposts/:id", async (req, res) => {
const id = req.params.id;
const filter = { _id: new ObjectId(id) };
const options = { upsert: true };
const updateMyVolunteerNeedPosts = req.body;
const updateDoc = {
$set: updateMyVolunteerNeedPosts,
};
const result = await VolunteerNeedPost.updateOne(
filter,
updateDoc,
options
);
res.send(result);
});
app.patch("/AllVolunteerNeedposts/:id", verifyJWT, async (req, res) => {
const id = req.params.id;
const { Noofvolunteersneeded } = req.body;
const UpdateNoofvolunteersneeded = {
$inc: { Noofvolunteersneeded: Noofvolunteersneeded },
};
const filter = { _id: new ObjectId(id) };
const result = await VolunteerNeedPost.updateOne(
filter,
UpdateNoofvolunteersneeded
);
res.send(result);
});
app.delete("/VolunteerDetails/:id", verifyJWT, async (req, res) => {
const id = req?.params?.id;
const query = { _id: new ObjectId(id) };
const result = await VolunteerDetails.deleteOne(query);
res.send(result);
});
app.delete("/Myvolunteerneedpost/:id", verifyJWT, async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const result = await VolunteerNeedPost.deleteOne(query);
res.send(result);
});
} finally {
}
}
run().catch(console.dir);
app.get("/", (req, res) => {
res.send("HelpingHub Server");
});
app.listen(port, () => {
console.log(`Sever is runing on port : ${port}`);
});