-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
403 lines (370 loc) · 11.5 KB
/
app.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
const express = require("express");
const app = express();
const PORT = 3000;
//Environment variables
require("dotenv").config();
//session requirements
const session = require("express-session");
//middleware for login
const { isLoggedIn } = require("./middleware.js");
//authentication requirements
const passport = require("passport");
const LocalStrategy = require("passport-local");
//model requirements
const User = require("./models/user.js");
const Community = require("./models/community.js");
const AlertBox = require("./models/alertbox.js");
const Review = require("./models/review.js");
const path = require("path");
const methodOverride = require("method-override");
//flash for messages
const flash = require("connect-flash");
app.use(flash());
//sessions
//session
const sessionOptions = {
secret: "supersecretcode",
resave: false,
saveUninitialized: true,
cookie: {
expires: Date.now() + 7 * 24 * 60 * 60 * 1000,
maxAge: 7 * 24 * 60 * 60 * 1000,
httpOnly: true,
},
};
app.use(session(sessionOptions));
//all the requirements for ejs
const ejsMate = require("ejs-mate");
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride("_method"));
app.engine("ejs", ejsMate);
app.use(express.static(path.join(__dirname, "/public")));
//for user autentication
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//for mongodb
const mongoose = require("mongoose");
const MONGO_URL = process.env.MONGO_URL;
main()
.then(() => {
console.log("DB is runing");
})
.catch((err) => console.log(err));
async function main() {
await mongoose.connect(MONGO_URL);
}
//for local variables
app.use((req, res, next) => {
res.locals.success = req.flash("success");
res.locals.error = req.flash("error");
res.locals.currUser = req.user;
next();
});
//for port of the express
app.listen(PORT, () => {
console.log(`listening on ${PORT}`);
});
app.get("/", (req, res) => {
req.flash("success", "Welcome to CM");
res.render("./home.ejs");
});
app.get("/login", (req, res) => {
res.render("./login.ejs");
});
app.post(
"/login",
passport.authenticate("local", {
failureRedirect: "/login",
failureFlash: true,
}),
(req, res) => {
req.flash("success", "Sucessfully loggedin");
res.redirect("/home");
}
);
app.get("/signup", (req, res) => {
res.render("./signup.ejs");
});
app.post("/signup", async (req, res) => {
let { username, email, password, phno, flatno } = req.body;
const newUser = new User({ email, username, phno, flatno });
try {
const registeredUser = await User.register(newUser, password);
console.log(registeredUser);
req.login(registeredUser, (err) => {
if (err) {
req.flash("error", "Invalid details");
}
req.flash("success", "Sign up Successfull");
res.redirect("/login");
});
} catch (err) {
req.flash("error", "User already exists");
res.render("./signup.ejs");
}
});
app.get("/logout", (req, res) => {
req.logout((err) => {
if (err) {
return next(err);
}
req.flash("success", "you are looged out!");
res.redirect("/login");
});
});
// app.get("/login/fail", (req, res) => {
// req.flash("error", "Invalid username or password");
// res.redirect("/login");
// });
// to get about section
app.get("/home", (req, res) => {
res.render("./home.ejs");
});
//Add community
app.get("/addCommunity", isLoggedIn, (req, res) => {
res.render("./addCommunity.ejs");
});
app.post("/community", async (req, res) => {
console.log(req.body);
const newCommunity = new Community(req.body);
newCommunity.owner = req.user;
newCommunity.resident.push(req.user);
console.log(newCommunity);
await newCommunity.save();
req.flash("success", "New Community created!");
const newAlertBox = new AlertBox();
newAlertBox.community = newCommunity;
await newAlertBox.save();
res.redirect("/home");
});
//showing all the communities
app.get("/community", isLoggedIn, async (req, res) => {
const allCommunity = await Community.find({});
res.render("index.ejs", { allCommunity });
});
//show route
app.get("/community/:id", isLoggedIn, async (req, res) => {
let { id } = req.params;
const community = await Community.findById(id)
.populate("resident")
.populate("owner");
if (!req.user) {
res.redirect("/login");
}
if (!community) {
req.flash("error", "NO such community exits");
}
if (!req.user) {
res.render("/login.ejs");
}
const user = req.user;
if (!user || !user.username) {
req.flash("success", "Please login");
res.render("/login.ejs");
}
const flag = community.resident.some(
(resi) => resi.username === user.username
);
const alertbox = await AlertBox.find({ community }).populate("messages.user");
const residents = community.resident;
const myalertbox = alertbox[0];
const messagesAndUsers = myalertbox.messages.map((message) => ({
message: message.msg,
user: message.user,
}));
const newCommunity = await community.populate("reviews");
const reviews = newCommunity.reviews;
res.render("./community/show.ejs", {
community,
flag,
residents,
myalertbox,
messagesAndUsers,
user,
reviews,
});
});
//join the community
app.post("/add/:id", async (req, res) => {
let { id } = req.params;
const community = await Community.findById(id).populate("resident");
community.resident.push(req.user);
const code = req.body.code;
if (code == community.code) {
await community.save();
req.flash("success", "Joined community successfully");
res.redirect(`/community/${id}`);
} else {
req.flash("error", "Invalid code");
res.redirect(`/community/${id}`);
}
});
//my community
app.use("/mycommunity", isLoggedIn, async (req, res) => {
const allCommunity = await Community.find({})
.populate("resident")
.populate("owner");
const user = req.user;
const joinedCommunities = [];
for (const community of allCommunity) {
const flag = community.resident.some(
(resi) => resi.username === user.username
);
if (flag) {
joinedCommunities.push(community);
}
}
res.render("./myCommunity.ejs", { joinedCommunities });
});
//edit route for community
app.get("/community/:id/edit", async (req, res) => {
let { id } = req.params;
const community = await Community.findById(id);
console.log(community);
res.render("./community/edit.ejs", { community });
});
//update route for community
app.put("/community/:id", async (req, res) => {
const { id } = req.params;
console.log(id);
const community = await Community.findByIdAndUpdate(id, {
...req.body.community,
});
await community.save();
req.flash("success", "Community updated successfully");
res.redirect(`/community/${community.id}`);
});
//to added a message to the alert box
app.post("/:communityId/alertbox", async (req, res) => {
const { communityId } = req.params;
const { id, msg } = req.body;
const userId = req.user._id;
const alertbox = await AlertBox.findById(id);
alertbox.messages.push({ user: userId, msg: msg });
console.log(alertbox);
await alertbox.save();
req.flash("success", "Message sent");
res.redirect(`/community/${communityId}`);
});
//delete resident route
app.delete("/community/:communityId/reviews/:residentId", async (req, res) => {
const { communityId, residentId } = req.params;
// console.log(communityId);
console.log("hi");
const community = await Community.findById(communityId).populate("resident");
if (!community) {
return res.status(404).send({ error: "Community not found" });
}
let newresident = community.resident;
newresident = newresident.filter((resi) => resi._id != residentId);
console.log(
await Community.findByIdAndUpdate(communityId, { resident: newresident })
);
req.flash("success", "Resident deleted successfully");
res.redirect(`/community/${communityId}`);
});
//To view the user profile
app.get("/user", async (req, res) => {
const user = req.user;
res.render("./user/user.ejs", { user });
});
//edit route
app.get("/user/:id/edit", async (req, res) => {
const { id } = req.params;
const user = await User.findById(id);
if (!user) {
req.redirec("/");
}
res.render("./user/edit.ejs", { user });
});
//update route
app.put("/user/:id", async (req, res) => {
console.log(req.body);
const { id } = req.params;
const user = await User.findByIdAndUpdate(id, { ...req.body.user });
await user.save();
req.flash("success", "User updated successfully");
res.redirect("/user");
});
//adding annoucement route
app.post("/announcement/:id/add", async (req, res) => {
const { id } = req.params;
const community = await Community.findById(id);
community.announcement = req.body.announcement;
await community.save();
req.flash("success", "Announcement added successfully");
res.redirect(`/community/${id}`);
});
//Pay rent route
app.post("/pay/:communityId/:residentId", async (req, res) => {
const { communityId, residentId } = req.params;
const resident = await User.findById(residentId);
resident.isPaid = true;
await resident.save();
req.flash("success", "Rent paid");
res.redirect(`/community/${communityId}`);
});
//To reset all the rents of the community page
app.put("/community/:id/reset", async (req, res) => {
const { id } = req.params;
const community = await Community.findById(id);
community.resident.forEach(async (resi) => {
let user = await User.findById(resi._id);
user.isPaid = false;
await user.save();
});
// Save the new community
await community.save();
res.redirect(`/community/${id}`);
});
//to save the review of the user
app.post("/community/:id/reviews", async (req, res) => {
let community = await Community.findById(req.params.id);
let newReview = new Review(req.body.review);
newReview.username = req.user.username;
community.reviews.push(newReview);
await newReview.save();
await community.save();
req.flash("success", "New review created!");
res.redirect(`/community/${community._id}`);
});
//leave the community
app.use("/community/:communityId/remove/:userId", async (req, res) => {
const communityId = req.params.communityId;
const memberIdToRemove = req.body.userId; // Assuming you send memberId in the request body
try {
// Find the community by ID
const community = await Community.findById(communityId);
// Check if the member exists in the resident array
const index = community.resident.indexOf(memberIdToRemove);
// Remove the member from the resident array
community.resident.splice(index, 1);
// Save the updated community
await community.save();
req.flash("success", "Left the community successfully!");
res.redirect(`/community/${community._id}`);
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal server error" });
}
});
//to delete comment
// app.delete("/community/:communityId/reviews/:reviewId", async (req, res) => {
// let { id, reviewid } = req.params;
// await Community.findByIdAndUpdate(id, { $pull: { reviews: reviewid } });
// await Review.findByIdAndDelete(reviewid);
// req.flash("success", "review deleted successfully");
// res.redirect(`/community/${id}`);
// });
app.use((err, req, res, next) => {
let { status, message } = err;
res.status(status).render("./error.ejs", { message });
global.currentRoute = req.path; // You can use req.originalUrl if you need the original URL including query parameters
next();
// res.status(status).send(message);
});