-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
300 lines (262 loc) · 8.29 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
require('dotenv').config()
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const alert = require("alert");
const bcrypt = require("bcrypt");
const _ = require("lodash");
const saltRounds = 10;
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
app.set("view-engine", 'ejs');
mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true });
const fileSchema = new mongoose.Schema({
filename: String,
filecontent: String
});
const workspaceSchema = new mongoose.Schema({
name: String,
files: { type: [fileSchema], sparse: true }
});
const userSchema = new mongoose.Schema({
name: String,
email: String,
password: String,
nickname: String,
secret: String,
workspaces: { type: [workspaceSchema], sparse: true }
});
const User = mongoose.model("User", userSchema);
const File = mongoose.model("File", fileSchema);
const Workspace = mongoose.model("Workspace", workspaceSchema);
// ----------------------------------- GET METHODS --------------------------------------
app.get("/login", function (req, res) {
res.render("login.ejs", { passwordStatus: "" });
});
app.get("/reset", function (req, res) {
res.render("login.ejs", { passwordStatus: "" });
})
app.get("/register", function (req, res) {
res.render("register.ejs");
});
app.get("/", function (req, res) {
res.render("welcome.ejs");
});
app.get("/forgotpassword", function (req, res) {
res.render("forgotpass.ejs", { offset: "off" });
})
app.get("/user/:U_ID", function (req, res) {
User.find({ _id: req.params.U_ID }, function (err, foundList) {
if (!err) {
if (foundList) {
res.render("dashboard.ejs", { user: foundList[0] });
}
} else {
res.redirect("/");
}
});
});
app.get("/user/:U_ID/workspace/:W_ID", function (req, res) {
const requestedWorkspaceID = req.params.W_ID;
const u_id = req.params.U_ID;
Workspace.find({ _id: requestedWorkspaceID }, function (err, foundList) {
if (!err) {
if (foundList) {
res.render("workspace.ejs", {
w_id: foundList[0]._id,
w_name: foundList[0].name,
files: foundList[0].files,
content: "",
u_id: u_id,
file_name: "---"
});
} else {
alert("Something went wrong");
res.redirect("/" + req.params.U_ID);
}
} else {
alert("Something went wrong");
res.redirect("/" + req.params.U_ID);
}
})
});
app.get("/user/:U_ID/workspace/:W_ID/file/:F_ID", function (req, res) {
const req_file_ID = req.params.F_ID;
const req_workspace_ID = req.params.W_ID;
Workspace.find({ _id: req_workspace_ID }, function (err, wrkspcdoc) {
if (!err) {
File.find({ _id: req_file_ID }, function (err, filedoc) {
if (!err) {
res.render("workspace.ejs", {
content: filedoc[0].filecontent,
w_id: req_workspace_ID, files: wrkspcdoc[0].files,
u_id: req.params.U_ID,
w_name: wrkspcdoc[0].name,
file_name: filedoc[0].filename
});
}
});
} else {
alert("Error in showing file!");
res.redirect("/user/" + req.params.U_ID + "/workspace/" + req.params.W_ID);
}
});
});
app.get("/userd/:U_ID/workspaced/:W_ID", function (req, res) {
const wid = req.params.W_ID;
const uid = req.params.U_ID;
User.findOneAndUpdate({ _id: uid }, { $pull: { workspaces: { _id: wid } } }, function (err, foundList) {
if (!err) {
Workspace.findByIdAndDelete({ _id: wid }, function (err, doc) {
for (let i = 0; i < doc.files.length; i++) {
File.findByIdAndDelete({ _id: doc.files[i]._id }, function (err, founcdList) {
if (err) {
alert("Some error occured");
res.redirect("/user/" + uid);
}
})
}
res.redirect("/user/" + uid);
})
}
})
})
app.get("/userd/:U_ID/workspaced/:W_ID/filed/:F_ID", function (req, res) {
const uid = req.params.U_ID;
const wid = req.params.W_ID;
const fid = req.params.F_ID;
Workspace.findOneAndUpdate({ _id: wid }, { $pull: { files: { _id: fid } } }, function (err, userList) {
if (!err) {
File.findByIdAndDelete({ _id: fid }, function (err, foundList) {
if (!err) {
res.redirect("/user/" + uid + "/workspace/" + wid);
}
})
}
})
})
// ----------------------------------- POST METHODS --------------------------------------
app.post("/register", function (req, res) {
User.findOne({ email: req.body.email }, function (err, foundList) {
if (!err) {
if (!foundList) {
bcrypt.hash(req.body.password, saltRounds, function (err, hash) {
const newuser = new User({
name: req.body.name,
email: req.body.email,
password: hash,
nickname: _.capitalize(req.body.nickname),
secret: _.capitalize(req.body.secret),
workspaces: []
});
newuser.save(function (err) {
if (err) { console.log(err); }
else {
res.render("dashboard.ejs", { user: newuser });
}
});
})
} else {
alert("This email id was already registered");
res.redirect("/login", { passwordStatus: "" });
}
}
})
})
app.post("/login", function (req, res) {
User.findOne({ email: req.body.email }, function (err, foundList) {
if (!err) {
if (!foundList) {
alert("Seems like account has not been created using this email id");
res.redirect("/register");
} else {
bcrypt.compare(req.body.password, foundList.password, function (err, result) {
if (result === true) {
res.redirect("/user/" + foundList._id);
} else {
res.render("login.ejs", { passwordStatus: "Password Incorrect" });
}
});
}
}
})
});
app.post("/dashboard", function (req, res) {
const user_id = req.body.user_id;
const req_workspace = req.body.new_workspace;
const new_workspace = new Workspace({
name: req_workspace,
files: []
});
new_workspace.save();
User.find({ _id: user_id }, function (err, foundList) {
if (!err) {
foundList[0].workspaces.push(new_workspace);
foundList[0].save();
res.render("dashboard.ejs", { user: foundList[0] });
}
});
});
app.post("/api/saveFileAndContent", function (req, res) {
const wid = req.body.workspace_id;
const uid = req.body.user_id;
const newfile = new File({
filename: req.body.filename,
filecontent: req.body.filecontent
});
newfile.save();
Workspace.find({ _id: wid }, function (err, foundList) {
if (!err) {
foundList[0].files.push(newfile);
foundList[0].save();
res.redirect("/user/" + uid + "/workspace/" + wid);
}
})
})
app.post("/forgotpassword", function (req, res) {
const email = req.body.email;
const name = req.body.name;
const nickname = _.capitalize(req.body.nickname);
const secret = _.capitalize(req.body.secret);
User.findOne({ email: email }, function (err, foundUser) {
if (!err) {
if (foundUser) {
if (foundUser.name === name && foundUser.nickname === nickname && foundUser.secret === secret) {
res.render("forgotpass.ejs", { offset: "on", u_id: foundUser._id });
} else {
alert("One of the entry was incorrect!");
res.redirect("/forgotpassword");
}
} else {
alert("User with this email doesn't exist");
res.redirect("/forgotpassword");
}
} else {
alert("Some error occured");
res.redirect("/");
}
})
});
app.post("/reset", function (req, res) {
User.findById({ _id: req.body.u_id }, function (err, foundUser) {
if (!err) {
bcrypt.hash(req.body.new_password, saltRounds, function (err, hash) {
foundUser.password = hash;
foundUser.save(function (err) {
if (!err) {
res.render("login.ejs", { passwordStatus: "Password Updated Sucessfully !!" });
}
});
});
}
})
})
// ---------------------------------- LISTEN METHOD --------------------------------------
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function (res, req) {
console.log("Server started on port 3000");
});