-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
359 lines (306 loc) · 9.14 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
const express = require("express"),
mongoose = require("mongoose"),
flash = require("connect-flash"),
passport = require("passport"),
LocalStrategy = require("passport-local"),
methodOverride = require("method-override"),
bodyParser = require("body-parser");
// Departments = require("./models/departments"),
// Treatmentreceipt = require("./models/treatmentreceipt"),
User = require("./models/user");
const userRoutes=require("./routes/users");
const app = express();
require('dotenv').config();
app.use(bodyParser.urlencoded({extended: true}));
app.use(flash());
mongoose.set('useUnifiedTopology', true);
const url = process.env.MONGODB_URI || 3000
mongoose.connect(url, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
}, () => {
console.log("Connected");
});
app.set("view engine","ejs");
app.use(express.static(__dirname + "/public"));
// PASSPORT CONFIGURATION
app.use(require("express-session")({
secret: "oh! bhaieee, kya majak ho rha hai ye????",
resave: false,
saveUninitialized: false
}));
app.use((req,res,next)=>{
res.locals.currentUser=req.user;
res.locals.success=req.flash("success");
res.locals.error=req.flash("error");
next();
});
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
}, User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
/*var userSchema = new mongoose.Schema({
firstName: String,
lastName: String,
address: String,
city: String,
state: String,
blood: String,
username: String,
password: String,
phone: String,
email: String,
aadhar: String,
prescriptions: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Prescription"
}
],
});*/
//var User = mongoose.model("User", userSchema);
//prescription model
var prescriptionSchema = new mongoose.Schema({
doctorname: String,
date: String,
note: String,
});
var Prescription = mongoose.model("Prescription", prescriptionSchema);
app.use("/",userRoutes);
app.get("/", (req,res)=>{
res.render("home");
});
app.get("/aboutUs",(req,res)=>{
res.render("aboutUs");
});
app.get("/departments",(req,res)=>{
res.render("departments");
});
app.get("/bloodStorage",(req,res)=>{
res.render("bloodStorage");
});
app.get("/contact", (req, res) => {
res.render("contact");
});
app.get("/covid",(req,res)=>{
res.render("covid");
});
app.get("/login", (req, res) => {
res.render("login");
});
app.post("/login", (req, res)=>{
res.send("you hit the post route");
});
app.get("/register",(req,res)=>{
res.render("register");
});
app.get("/prescription",(req,res)=>{
res.render("prescription");
});
//INDEX - show all users
app.get("/user", (req, res)=>{
User.find({}, (err, allUsers)=>{
if(err){
console.log(err);
} else {
res.render("user", {users: allUsers});
}
});
});
//CREATE - add new user to the database
app.post("/user", (req, res)=>{
// get data from form and add to user array
var firstName = req.body.firstName;
var lastName = req.body.lastName;
var address = req.body.address;
var city = req.body.city;
var state = req.body.state;
var blood = req.body.blood;
var username = req.body.username;
var password = req.body.password;
var phone = req.body.phone;
var email = req.body.email;
var aadhar = req.body.aadhar;
var newUser = {
firstName: firstName,
lastName: lastName,
address: address,
city: city,
state: state,
blood: blood,
username: username,
password: password,
phone: phone,
email: email,
aadhar: aadhar,
};
// users.push(newUser);
//create new user and save to database
User.create(newUser, (err, newlyCreated)=>{
if(err){
console.log(err);
} else {
//redirect to user page
res.redirect("user");
}
});
});
// NEW - show form to create new user
app.get("/user/new",(req,res)=>{
res.render("register");
});
// SHOW - shows more info about one user
app.get("/user/:id", (req, res)=>{
//find the user with provided id
User.findById(req.params.id).populate("prescriptions").exec((err, foundUser)=>{
if(err){
console.log(err);
} else {
//show template with that user
res.render("patientProfile", {user: foundUser});
}
});
});
//==============doctor=======
/*app.get("/doctorRegister",(req,res)=>{
res.render("doctorRegister");
});
app.get("/doctorProfile",(req,res)=>{
res.render("doctorProfile");
});*/
var doctorSchema = new mongoose.Schema({
firstName: String,
lastName: String,
address: String,
city: String,
state: String,
blood: String,
department: String,
specialization: String,
age: String,
username: String,
password: String,
phone: String,
email: String,
aadhar: String,
});
var Doctor = mongoose.model("Doctor", doctorSchema);
app.get("/showDoctors", (req, res)=>{
Doctor.find({}, (err, allDoctors)=>{
if(err){
console.log(err);
} else {
res.render("showDoctors", {doctors: allDoctors});
}
});
});
//CREATE - add new user to the database
app.post("/showDoctors", (req, res)=>{
// get data from form and add to user array
var firstName = req.body.firstName;
var lastName = req.body.lastName;
var address = req.body.address;
var city = req.body.city;
var state = req.body.state;
var blood = req.body.blood;
var department = req.body.department;
var specialization = req.body.specialization;
var age = req.body.age;
var username = req.body.username;
var password = req.body.password;
var phone = req.body.phone;
var email = req.body.email;
var aadhar = req.body.aadhar;
var newDoctor = {
firstName: firstName,
lastName: lastName,
address: address,
city: city,
state: state,
blood: blood,
department: department,
specialization: specialization,
age: age,
username: username,
password: password,
phone: phone,
email: email,
aadhar: aadhar,
};
// users.push(newUser);
//create new user and save to database
Doctor.create(newDoctor, (err, newlyCreatedDoctor)=>{
if(err){
console.log(err);
} else {
//redirect to user page
res.redirect("showDoctors");
}
});
});
// NEW - show form to create new user
app.get("/showDoctors/new",(req,res)=>{
res.render("doctorRegister");
});
// SHOW - shows more info about one user
app.get("/showDoctors/:id", (req, res)=>{
//find the user with provided id
Doctor.findById(req.params.id, (err, foundDoctor)=>{
if(err){
console.log(err);
} else {
//show template with that user
res.render("doctorProfile", {doctor: foundDoctor});
}
});
});
//======prescription==========
app.get("/user/:id/prescription/new", (req, res)=>{
User.findById(req.params.id, function(err,foundUser){
if(err){
console.log(err);
}else{
res.render("prescription",{user: foundUser});
}
}
);
});
//CREATE - add new prescription to the database
app.post("/user/:id/prescription", (req, res)=>{
User.findById(req.params.id, function(err, campground) {
if(err) {
console.log(err);
res.redirect("/user/"+ User._id);
}else{
Prescription.create(req.body.prescription, function(err,prescription){
if(err){
//req.flash("Something went wrong");
console.log(err);
}else{
/*var doctorname = req.body.doctorname;
var date = req.body.date;
var note = req.body.note;
var newPrescription={
doctorname: doctorname,
date: date,
note: note
}
//newPrescription.save();*/
User.prescriptions.push(prescription);
User.save();
req.flash("success","Successfully added comment");
res.redirect("/user/" + User._id);
}
});
}
});
});
let port = process.env.PORT || 3000
app.listen(port, process.env.IP, function(){
console.log("started!");
});