-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewsController.js
executable file
·87 lines (74 loc) · 2.23 KB
/
viewsController.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
const Tour = require('../models/tourModel');
const User = require('../models/userModel');
const Booking = require('../models/bookingModel');
const catchAsync = require('../utils/catchAsync');
const AppError = require('../utils/appError');
exports.alerts = (req,res,next) => {
const {alert} = req.query;
if(alert === 'booking') res.locals.alert ='Your booking was successful!Please check your email for a confirmation.If your booking doesn\'t come here immediately, please come back later.';
next();
}
exports.getOverview = catchAsync(async (req, res, next) => {
// 1) Get tour data from collection
const tours = await Tour.find();
// 2) Build template
// 3) Render that template using tour data from 1)
res.status(200).render('overview', {
title: 'All Tours',
tours
});
});
exports.getTour = catchAsync(async (req, res, next) => {
// 1) Get the data, for the requested tour (including reviews and guides)
const tour = await Tour.findOne({ slug: req.params.slug }).populate({
path: 'reviews',
fields: 'review rating user'
});
if (!tour) {
return next(new AppError('There is no tour with that name.', 404));
}
// 2) Build template
// 3) Render template using data from 1)
res.status(200).render('tour', {
title: `${tour.name} Tour`,
tour
});
});
exports.getLoginForm = (req, res) => {
res.status(200).render('login', {
title: 'Log into your account'
});
};
exports.getAccount = (req, res) => {
res.status(200).render('account', {
title: 'Your account'
});
};
exports.getMyTours = catchAsync(async (req, res, next) => {
// 1) Find all bookings
const bookings = await Booking.find({ user: req.user.id });
// 2) Find tours with the returned IDs
const tourIDs = bookings.map(el => el.tour);
const tours = await Tour.find({ _id: { $in: tourIDs } });
res.status(200).render('overview', {
title: 'My Tours',
tours
});
});
exports.updateUserData = catchAsync(async (req, res, next) => {
const updatedUser = await User.findByIdAndUpdate(
req.user.id,
{
name: req.body.name,
email: req.body.email
},
{
new: true,
runValidators: true
}
);
res.status(200).render('account', {
title: 'Your account',
user: updatedUser
});
});