mirror of
https://github.com/kevin-DL/complete-node-bootcamp.git
synced 2026-01-16 21:24:38 +00:00
Initial commit 🚀
This commit is contained in:
70
4-natours/after-section-09/controllers/errorController.js
Normal file
70
4-natours/after-section-09/controllers/errorController.js
Normal file
@@ -0,0 +1,70 @@
|
||||
const AppError = require('./../utils/appError');
|
||||
|
||||
const handleCastErrorDB = err => {
|
||||
const message = `Invalid ${err.path}: ${err.value}.`;
|
||||
return new AppError(message, 400);
|
||||
};
|
||||
|
||||
const handleDuplicateFieldsDB = err => {
|
||||
const value = err.errmsg.match(/(["'])(\\?.)*?\1/)[0];
|
||||
console.log(value);
|
||||
|
||||
const message = `Duplicate field value: ${value}. Please use another value!`;
|
||||
return new AppError(message, 400);
|
||||
};
|
||||
const handleValidationErrorDB = err => {
|
||||
const errors = Object.values(err.errors).map(el => el.message);
|
||||
|
||||
const message = `Invalid input data. ${errors.join('. ')}`;
|
||||
return new AppError(message, 400);
|
||||
};
|
||||
|
||||
const sendErrorDev = (err, res) => {
|
||||
res.status(err.statusCode).json({
|
||||
status: err.status,
|
||||
error: err,
|
||||
message: err.message,
|
||||
stack: err.stack
|
||||
});
|
||||
};
|
||||
|
||||
const sendErrorProd = (err, res) => {
|
||||
// Operational, trusted error: send message to client
|
||||
if (err.isOperational) {
|
||||
res.status(err.statusCode).json({
|
||||
status: err.status,
|
||||
message: err.message
|
||||
});
|
||||
|
||||
// Programming or other unknown error: don't leak error details
|
||||
} else {
|
||||
// 1) Log error
|
||||
console.error('ERROR 💥', err);
|
||||
|
||||
// 2) Send generic message
|
||||
res.status(500).json({
|
||||
status: 'error',
|
||||
message: 'Something went very wrong!'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = (err, req, res, next) => {
|
||||
// console.log(err.stack);
|
||||
|
||||
err.statusCode = err.statusCode || 500;
|
||||
err.status = err.status || 'error';
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
sendErrorDev(err, res);
|
||||
} else if (process.env.NODE_ENV === 'production') {
|
||||
let error = { ...err };
|
||||
|
||||
if (error.name === 'CastError') error = handleCastErrorDB(error);
|
||||
if (error.code === 11000) error = handleDuplicateFieldsDB(error);
|
||||
if (error.name === 'ValidationError')
|
||||
error = handleValidationErrorDB(error);
|
||||
|
||||
sendErrorProd(error, res);
|
||||
}
|
||||
};
|
||||
165
4-natours/after-section-09/controllers/tourController.js
Normal file
165
4-natours/after-section-09/controllers/tourController.js
Normal file
@@ -0,0 +1,165 @@
|
||||
const Tour = require('./../models/tourModel');
|
||||
const APIFeatures = require('./../utils/apiFeatures');
|
||||
const catchAsync = require('./../utils/catchAsync');
|
||||
const AppError = require('./../utils/appError');
|
||||
|
||||
exports.aliasTopTours = (req, res, next) => {
|
||||
req.query.limit = '5';
|
||||
req.query.sort = '-ratingsAverage,price';
|
||||
req.query.fields = 'name,price,ratingsAverage,summary,difficulty';
|
||||
next();
|
||||
};
|
||||
|
||||
exports.getAllTours = catchAsync(async (req, res, next) => {
|
||||
const features = new APIFeatures(Tour.find(), req.query)
|
||||
.filter()
|
||||
.sort()
|
||||
.limitFields()
|
||||
.paginate();
|
||||
const tours = await features.query;
|
||||
|
||||
// SEND RESPONSE
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
results: tours.length,
|
||||
data: {
|
||||
tours
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
exports.getTour = catchAsync(async (req, res, next) => {
|
||||
const tour = await Tour.findById(req.params.id);
|
||||
// Tour.findOne({ _id: req.params.id })
|
||||
|
||||
if (!tour) {
|
||||
return next(new AppError('No tour found with that ID', 404));
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
data: {
|
||||
tour
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
exports.createTour = catchAsync(async (req, res, next) => {
|
||||
const newTour = await Tour.create(req.body);
|
||||
|
||||
res.status(201).json({
|
||||
status: 'success',
|
||||
data: {
|
||||
tour: newTour
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
exports.updateTour = catchAsync(async (req, res, next) => {
|
||||
const tour = await Tour.findByIdAndUpdate(req.params.id, req.body, {
|
||||
new: true,
|
||||
runValidators: true
|
||||
});
|
||||
|
||||
if (!tour) {
|
||||
return next(new AppError('No tour found with that ID', 404));
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
data: {
|
||||
tour
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
exports.deleteTour = catchAsync(async (req, res, next) => {
|
||||
const tour = await Tour.findByIdAndDelete(req.params.id);
|
||||
|
||||
if (!tour) {
|
||||
return next(new AppError('No tour found with that ID', 404));
|
||||
}
|
||||
|
||||
res.status(204).json({
|
||||
status: 'success',
|
||||
data: null
|
||||
});
|
||||
});
|
||||
|
||||
exports.getTourStats = catchAsync(async (req, res, next) => {
|
||||
const stats = await Tour.aggregate([
|
||||
{
|
||||
$match: { ratingsAverage: { $gte: 4.5 } }
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: { $toUpper: '$difficulty' },
|
||||
numTours: { $sum: 1 },
|
||||
numRatings: { $sum: '$ratingsQuantity' },
|
||||
avgRating: { $avg: '$ratingsAverage' },
|
||||
avgPrice: { $avg: '$price' },
|
||||
minPrice: { $min: '$price' },
|
||||
maxPrice: { $max: '$price' }
|
||||
}
|
||||
},
|
||||
{
|
||||
$sort: { avgPrice: 1 }
|
||||
}
|
||||
// {
|
||||
// $match: { _id: { $ne: 'EASY' } }
|
||||
// }
|
||||
]);
|
||||
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
data: {
|
||||
stats
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
exports.getMonthlyPlan = catchAsync(async (req, res, next) => {
|
||||
const year = req.params.year * 1; // 2021
|
||||
|
||||
const plan = await Tour.aggregate([
|
||||
{
|
||||
$unwind: '$startDates'
|
||||
},
|
||||
{
|
||||
$match: {
|
||||
startDates: {
|
||||
$gte: new Date(`${year}-01-01`),
|
||||
$lte: new Date(`${year}-12-31`)
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: { $month: '$startDates' },
|
||||
numTourStarts: { $sum: 1 },
|
||||
tours: { $push: '$name' }
|
||||
}
|
||||
},
|
||||
{
|
||||
$addFields: { month: '$_id' }
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0
|
||||
}
|
||||
},
|
||||
{
|
||||
$sort: { numTourStarts: -1 }
|
||||
},
|
||||
{
|
||||
$limit: 12
|
||||
}
|
||||
]);
|
||||
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
data: {
|
||||
plan
|
||||
}
|
||||
});
|
||||
});
|
||||
30
4-natours/after-section-09/controllers/userController.js
Normal file
30
4-natours/after-section-09/controllers/userController.js
Normal file
@@ -0,0 +1,30 @@
|
||||
exports.getAllUsers = (req, res) => {
|
||||
res.status(500).json({
|
||||
status: 'error',
|
||||
message: 'This route is not yet defined!'
|
||||
});
|
||||
};
|
||||
exports.getUser = (req, res) => {
|
||||
res.status(500).json({
|
||||
status: 'error',
|
||||
message: 'This route is not yet defined!'
|
||||
});
|
||||
};
|
||||
exports.createUser = (req, res) => {
|
||||
res.status(500).json({
|
||||
status: 'error',
|
||||
message: 'This route is not yet defined!'
|
||||
});
|
||||
};
|
||||
exports.updateUser = (req, res) => {
|
||||
res.status(500).json({
|
||||
status: 'error',
|
||||
message: 'This route is not yet defined!'
|
||||
});
|
||||
};
|
||||
exports.deleteUser = (req, res) => {
|
||||
res.status(500).json({
|
||||
status: 'error',
|
||||
message: 'This route is not yet defined!'
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user