-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
682 lines (551 loc) · 23.8 KB
/
app.js
File metadata and controls
682 lines (551 loc) · 23.8 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Import required modules
require('dotenv').config(); // Ensure you have dotenv configured if using .env files
// Environment variable validation
if (!process.env.MONGO_URI) {
console.error('FATAL ERROR: MONGO_URI is not defined in environment variables.');
process.exit(1);
}
if (!process.env.SESSION_SECRET) {
console.error('FATAL ERROR: SESSION_SECRET is not defined in environment variables.');
process.exit(1);
}
const express = require('express'); // Importing Express framework
const mongoose = require('mongoose'); // Importing Mongoose for MongoDB interaction
const bodyParser = require('body-parser'); // Importing body-parser for parsing incoming request bodies
const session = require('express-session'); // Importing express-session for session management
const path = require('path'); // Importing path for handling file paths
const helmet = require('helmet'); // Importing Helmet for security middleware
const { body, validationResult } = require('express-validator');
// Initialize Express application
const app = express();
// Serve static files from 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
// Serve static files from 'public' directory under the '/public' route
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
// Serve static files from 'images' directory under the '/images' route
app.use('/images', express.static(path.join(__dirname, 'images')));
// Serve static files from 'public/styles' directory under the '/styles' route
app.use('/styles', express.static(path.join(__dirname, 'public/styles')));
// Serve static files from 'public/js' directory under the '/js' route
app.use('/js', express.static(path.join(__dirname, 'public/js')));
const multer = require('multer'); // Import Multer for handling file uploads
// Set up Multer storage
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/uploads/') // Specify the directory for storing uploaded files
},
filename: function (req, file, cb) {
cb(null, file.originalname) // Use the original file name for storing the uploaded file
}
});
const upload = multer({ storage: storage });
const uri = process.env.MONGO_URI;
async function connectDB() {
try {
await mongoose.connect(uri, {
serverSelectionTimeoutMS: 30000, // Increase timeout for server selection
});
console.log('Connected to MongoDB with Mongoose successfully');
} catch (error) {
console.error('Error connecting to MongoDB with Mongoose:', error);
}
}
connectDB();
// Event listener for MongoDB connection error
mongoose.connection.on('error', (err) => {
console.error('MongoDB connection error:', err);
});
const ContactForm = require('./models/ContactForm');
const User = require('./models/User');
const Product = require('./models/Product');
const Behavior = require('./models/Behavior');
const Progress = require('./models/Progress');
const contactRoutes = require('./routes/contact');
app.use('/', contactRoutes);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
cookie: {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: process.env.NODE_ENV === 'production' ? 'lax' : false
}
}));
app.use(helmet());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use((req, res, next) => {
res.locals.session = req.session;
next();
});
app.post('/submitBehavior', async (req, res) => {
try {
// Extract form data from request body
const { behaviorName, behaviorImg, behaviorDesc, behaviorObj, behaviorTech, progress, comments } = req.body;
// Retrieve pet parent username from the request body
const behaviorUser = req.body.behaviorUser;
const behaviorPet = req.body.behaviorPet;
// Check if a behavior with the same name already exists in the database
let existingBehavior = await Behavior.findOne({ behaviorName, behaviorUser });
// If the behavior exists, update its progress and comments
if (existingBehavior) {
existingBehavior.progress = progress;
existingBehavior.comments = comments;
await existingBehavior.save();
} else {
// If the behavior does not exist, create a new behavior record
const newBehavior = new Behavior({
behaviorName,
behaviorImg,
behaviorDesc,
behaviorObj,
behaviorTech,
progress,
comments,
behaviorUser, // Update to use behaviorUser
behaviorPet
});
// Save the new behavior to the database
await newBehavior.save();
}
const behaviors = await Behavior.find({ behaviorUser: behaviorUser });
const selectedUserPet = behaviorUser + "_" + behaviorPet;
// Fetch behaviors from the database
const users = await User.find(); // Assuming you want to fetch all users
const currentUser = req.session.username;
const petUser = behaviorUser;
// Render the behavioralAnalysisTrainer view with the fetched behaviors and selectedUserPet
res.render('behavioralAnalysisTrainer', { users, currentUser, selectedUserPet, behaviors, petUser });
} catch (error) {
// Handle error
console.error(error);
res.status(500).send('Error saving behavior');
}
});
app.post('/submitDay', upload.single('video'), async (req, res) => {
try {
// Extract form data from request body
const { progressDay, comments } = req.body;
// Retrieve pet parent username from the request body
const progressUser = req.body.progressUser;
const progressPet = req.body.progressPet;
const progressVideo = req.file.path; // Path to the uploaded video file
// Check if a behavior with the same name already exists in the database
let existingProgress = await Progress.findOne({ progressDay, progressUser });
// If the behavior exists, update its progress and comments
if (existingProgress) {
existingProgress.comments = comments;
existingProgress.progressVideo = progressVideo; // Update video file path
await existingProgress.save();
} else {
// If the behavior does not exist, create a new behavior record
const newProgress = new Progress({
progressDay,
comments,
progressUser, // Update to use behaviorUser
progressPet,
progressVideo,
});
// Save the new behavior to the database
await newProgress.save();
}
const progresses = await Progress.find({ progressUser: progressUser });
const selectedUserPet = progressUser + "_" + progressPet;
// Fetch behaviors from the database
const users = await User.find(); // Assuming you want to fetch all users
const currentUser = req.session.username;
const petUser = progressUser;
// Render the behavioralAnalysisTrainer view with the fetched behaviors and selectedUserPet
res.render('progressCheckTrainer', { users, currentUser, selectedUserPet, progresses, petUser });
} catch (error) {
// Handle error
console.error(error);
res.status(500).send('Error saving behavior');
}
});
// Assuming you're using Express.js for your server
app.post('/getBehaviorStatus', async (req, res) => {
try {
// Get the selected user and pet from the request body
const selectedUserPet = req.body.selectedUserPet;
// Split the selectedUserPet to get username and petname
const [username, petname] = selectedUserPet.split('_');
// Fetch behavior statuses for the selected user and pet from the database
const behaviorStatuses = await Behavior.find({ behaviorUser: username, behaviorPet: petname });
// Send the behavior statuses as a response
res.json(behaviorStatuses);
} catch (error) {
console.error('Error fetching behavior statuses:', error);
res.status(500).send('Error fetching behavior statuses');
}
});
//Handle page load
app.get('/getBehavior', async (req, res) => {
try {
const { behaviorName, behaviorUser, behaviorPet } = req.query;
// Retrieve behavior from the database
const behavior = await Behavior.findOne({ behaviorName, behaviorUser, behaviorPet });
// If the behavior is found, send the progress back to the client
if (behavior) {
res.status(200).json({ progress: behavior.progress, user: behavior.behaviorUser, petname: behavior.behaviorPet });
} else {
res.status(404).send('Behavior not found');
}
} catch (error) {
console.error(error);
res.status(500).send('Error retrieving behavior');
}
});
// Submitted Form Route - GET
app.get('/submittedform', (req, res) => {
res.render('submittedform');
});
// Root Route - Redirect to Home
app.get('/', (req, res) => {
res.redirect('/home');
});
// Home Route - GET
app.get('/home', (req, res) => {
const username = req.session.username;
res.render('home', { username });
});
// About Us Route - GET
app.get('/aboutus', (req, res) => {
res.render('aboutus');
});
// Login Route - GET
app.get('/customerlogin', (req, res) => {
const errorMessage = req.query.errorMessage;
res.render('customerlogin', { errorMessage });
});
// Trainer Login Route - GET
app.get('/trainerlogin', (req, res) => {
const errorMessage = req.query.errorMessage;
res.render('trainerlogin', { errorMessage });
});
// Trainer Signup Route - GET
app.get('/trainersignup', (req, res) => {
// Your logic for rendering the trainer signup page
res.render('trainersignup');
});
app.get('/behavioralAnalysisTrainer', async(req, res) => {
try {
// Fetch behaviors from the database
const users = await User.find(); // Assuming you want to fetch all behaviors
const currentUser = req.session.username;
const selectedUserPet = "Select User_Pet";
const behaviors = await Behavior.find({ behaviorUser: "Select User_Pet" });
// Render the behavioralAnalysisPetParent view with the fetched behaviors
res.render('behavioralAnalysisTrainer', { users, currentUser, selectedUserPet, behaviors });
} catch (err) {
console.error('Error fetching behaviors:', err);
res.status(500).render('error', { errorMessage: 'Internal Server Error' });
}
});
app.get('/behavioralAnalysisPetParent', async (req, res) => {
try {
// Fetch behaviors from the database
const currentUser = req.session.username;
const behaviors = await Behavior.find({ behaviorUser: currentUser });
// Render the behavioralAnalysisPetParent view with the fetched behaviors
res.render('behavioralAnalysisPetParent', { behaviors });
} catch (err) {
console.error('Error fetching behaviors:', err);
res.status(500).render('error', { errorMessage: 'Internal Server Error' });
}
});
app.get('/progressCheckTrainer', async(req, res) => {
try {
// Fetch behaviors from the database
const users = await User.find(); // Assuming you want to fetch all behaviors
const currentUser = req.session.username;
const selectedUserPet = "Select User_Pet";
const progresses = await Progress.find({ progressUser: "Select User_Pet" });
// Render the behavioralAnalysisPetParent view with the fetched behaviors
res.render('progressCheckTrainer', { users, currentUser, selectedUserPet, progresses });
} catch (err) {
console.error('Error fetching behaviors:', err);
res.status(500).render('error', { errorMessage: 'Internal Server Error' });
}
});
app.get('/progressCheckPetParent', async (req, res) => {
try {
// Fetch behaviors from the database
const currentUser = req.session.username;
const petName = req.session.petName;
const progresses = await Progress.find({ progressUser: currentUser });
// Render the behavioralAnalysisPetParent view with the fetched behaviors
res.render('progressCheckPetParent', { progresses, petName });
} catch (err) {
console.error('Error fetching behaviors:', err);
res.status(500).render('error', { errorMessage: 'Internal Server Error' });
}
});
// POST endpoint for saving comments
app.post('/saveComment', (req, res) => {
const { day, comment } = req.body;
// Here you would save the comment to your database
console.log(`Comment saved for day ${day}: ${comment}`);
// Respond with a success message
res.status(200).send('Comment saved successfully');
});
app.get('/paymentPage', (req, res) => {
// Retrieve the total from session storage
const total = req.session.total;
// Render the paymentPage view with the total as a template variable
res.render('paymentPage', { total });
});
app.post('/paymentPage', (req, res) => {
// Validate and process the form data here
// For example, you can retrieve the form data from req.body
const formData = req.body;
// Redirect the user to the payment success page
res.redirect('/paymentSuccessPage');
});
// Payment Success Route - GET
app.get('/paymentSuccessPage', (req, res) => {
// Retrieve the payment details and user data from the session
const paymentDetails = req.session.paymentDetails;
const user = req.session.user;
// Render the payment success page, passing the payment details and user data as template variables
res.render('paymentSuccessPage', { paymentDetails, user });
});
// Userspace Route - GET
app.get('/userspace', (req, res) => {
const username = req.session.username;
res.render('userspace', { username });
});
// Signup Route - GET
app.get('/signup', (req, res) => {
res.render('signup');
});
// customerSignupMethod Route - POST
app.post('/customerSignupMethod', [
body('username').trim().isLength({ min: 3, max: 30 }).withMessage('Username must be 3-30 characters long').matches(/^[A-Za-z0-9_]+$/).withMessage('Username must contain only letters, numbers, and underscores'),
body('petName').trim().isLength({ min: 2, max: 30 }).withMessage('Pet name must be 2-30 characters long').matches(/^[A-Za-z ]+$/).withMessage('Pet name must contain only letters and spaces'),
body('petBreed').trim().isLength({ min: 2, max: 30 }).withMessage('Pet breed must be 2-30 characters long').matches(/^[A-Za-z ]+$/).withMessage('Pet breed must contain only letters and spaces'),
body('password').isLength({ min: 6, max: 50 }).withMessage('Password must be 6-50 characters long'),
body('confirmpassword').custom((value, { req }) => value === req.body.password).withMessage('Passwords do not match'),
], async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).render('signup', { errorMessage: errors.array().map(e => e.msg).join('<br>') });
}
const { username, petName, petBreed, password } = req.body;
try {
// Check if the user already exists
const existingUser = await User.findOne({ name: username });
if (existingUser) {
return res.render('signup', { errorMessage: 'Username already exists. Choose a different username.' });
}
// If the user doesn't exist, create and save a new user
const newUser = new User({
name: username,
petName: petName,
petBreed: petBreed,
password: password,
role: 'Customer',
});
await newUser.save();
req.session.username = username;
req.session.role = 'Customer';
res.redirect('/home');
} catch (err) {
console.error('Error during signup:', err);
res.sendStatus(500);
}
});
// trainerSignupMethod Route - POST
app.post('/trainerSignupMethod', async (req, res) => {
const { username, password } = req.body;
try {
// Check if the user already exists
const existingUser = await User.findOne({ name: username });
if (existingUser) {
// If the user already exists, handle accordingly
return res.render('signup', { errorMessage: 'Username already exists. Choose a different username.' });
}
// If the user doesn't exist, create and save a new user
const newUser = new User({
name: username,
password: password,
role: 'Trainer', // set the role here as a string
});
await newUser.save();
// Store username in the session
req.session.username = username;
req.session.role = 'Trainer'; // set the role in the session
// Redirect to the home page after successful signup
res.redirect('/home');
} catch (err) {
console.error('Error during signup:', err);
res.sendStatus(500);
}
});
//Trainer Login Route - POST
app.post('/trainerLoginMethod', async (req, res) => {
const { username, password } = req.body;
try {
// Check if the user exists
const user = await User.findOne({ name: username });
if (!user) {
// If the user doesn't exist, display a message
return res.render('trainerlogin', { errorMessage: 'Invalid username or password.' });
}
// Check if the password is correct
if (user.password !== password) {
// If the password is incorrect, display an error message
return res.render('trainerlogin', { errorMessage: 'Invalid username or password.' });
}
// Set the username and role in the session
req.session.username = username;
req.session.role = 'Trainer'; // assuming you have a role field in your user model
res.redirect('/home');
} catch (err) {
console.error('Error during login:', err);
res.sendStatus(500);
}
});
//Customer Login Route - POST
app.post('/customerLoginMethod', async (req, res) => {
const { username, password } = req.body;
try {
// Check if the user exists
const user = await User.findOne({ name: username });
if (!user) {
// If the user doesn't exist, display a message
return res.render('customerlogin', { errorMessage: 'Invalid username or password.' });
} else if (user.password !== password) {
// If the password is incorrect, display an error message
return res.render('customerlogin', { errorMessage: 'Invalid username or password.' });
}
// Set the username and role in the session
req.session.username = username;
req.session.role = 'Customer'; // assuming you have a role field in your user model
res.redirect('/home');
} catch (err) {
console.error('Error during login:', err);
res.sendStatus(500);
}
});
// ContactForm Route - POST
app.post('/ContactForm', [
body('name').trim().isLength({ min: 2, max: 50 }).withMessage('Name must be 2-50 characters long').matches(/^[A-Za-z ]+$/).withMessage('Name must contain only letters and spaces'),
body('phone').matches(/^\d{3}-\d{3}-\d{4}$/).withMessage('Phone number must be in the format xxx-xxx-xxxx'),
body('email').isEmail().withMessage('Invalid email address'),
body('petname').trim().isLength({ min: 2, max: 30 }).withMessage('Pet name must be 2-30 characters long').matches(/^[A-Za-z ]+$/).withMessage('Pet name must contain only letters and spaces'),
body('address').trim().isLength({ min: 5, max: 100 }).withMessage('Address must be 5-100 characters long'),
], async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).render('ContactForm', { errors: errors.array() });
}
const { name, phone, email, petname, address } = req.body;
console.log('Form data:', req.body);
try {
// Save the form data to the database
const newContactForm = new ContactForm({
name,
phone,
email,
petname,
address
});
await newContactForm.save();
// Render the "Thank You" page
res.redirect('/submittedform');
} catch (err) {
console.error('Error saving contact form data:', err);
res.status(500).render('error', { errorMessage: 'Internal Server Error' });
}
});
// Logout Route - GET
app.get('/logout-success', (req, res) => {
// Clear the session
req.session.destroy((err) => {
if (err) {
console.error('Error during logout:', err);
res.sendStatus(500);
} else {
// Redirect to the login page or another page
res.redirect('/');
}
});
});
// Products Route - GET
app.get('/Buy', async (req, res) => {
try {
// Fetch products from the database based on categories
const categories = ['Dog Toys', 'Dog Jackets', 'Tennis Balls', 'Dog Food'];
// Fetch products for each category
const toys = await Product.find({ category: 'Dog Toys' });
const jackets = await Product.find({ category: 'Dog Jackets' });
const tennisBalls = await Product.find({ category: 'Tennis Balls' });
const dogFoods = await Product.find({ category: 'Dog Food' });
// Render the 'Buy' view with the fetched products
res.render('Buy', { toys, jackets, tennisBalls, dogFoods });
} catch (err) {
console.error('Error fetching products:', err);
res.sendStatus(500);
}
});
// Add to Cart Route - POST
app.post('/addToCart', async (req, res) => {
const productId = req.body.productId;
try {
// Fetch the selected product from the database
const product = await Product.findById(productId);
// Add the product to the user's cart
req.session.cart = req.session.cart || [];
req.session.cart.push(product);
// Redirect to the products page after adding to cart
res.redirect('/Buy');
} catch (err) {
console.error('Error adding to cart:', err);
res.sendStatus(500);
}
});
// Add this route along with other routes
app.post('/submittrainersignup', async (req, res) => {
const { name, email, password } = req.body;
try {
// Create a new Trainer document
const newTrainer = new Trainer({
name,
email,
password,
// Add other fields as needed
});
// Save the new trainer to MongoDB
await newTrainer.save();
// Redirect or respond as needed
res.redirect('/trainersignupsuccess'); // Redirect to a success page, for example
} catch (err) {
console.error('Error during trainer signup:', err);
res.status(500).render('error', { errorMessage: 'Internal Server Error' });
}
});
app.get('/logout-success', (req, res) => res.render('logout-success'));
// Cart Route - GET
app.get('/cart', (req, res) => {
// Retrieve the cart items from the session
const cartItems = req.session.cart || [];
// Render the cart page, passing the cart items to the view
res.render('cart', { cartItems });
});
// Error Handling Middleware
app.use((err, req, res, next) => {
console.error('Error:', err);
res.status(500).render('error', { errorMessage: 'Internal Server Error' });
});
// 404 handler (should be after all other routes)
app.use((req, res, next) => {
res.status(404).render('error', { errorMessage: '404 - Page Not Found' });
});
// Server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));