From ad204d6bde019c84a1cd64811ffa53f1f350a5cb Mon Sep 17 00:00:00 2001 From: BryanField15 <4bryanfield@gmail.com> Date: Fri, 10 Nov 2023 11:43:10 +1100 Subject: [PATCH] Added error mesages to user controller --- controllers/tripController.js | 10 +- controllers/userController.js | 185 ++++++++++++++-------------------- 2 files changed, 76 insertions(+), 119 deletions(-) diff --git a/controllers/tripController.js b/controllers/tripController.js index 54655f9..ba02683 100644 --- a/controllers/tripController.js +++ b/controllers/tripController.js @@ -207,15 +207,7 @@ exports.deleteTrip = catchAsync(async (req, res, next) => { }); exports.getTrip = catchAsync(async (req, res, next) => { - const trip = await Trip.findById(req.params.id) - .populate({ - path: 'driver', - select: ['firstName', 'lastName', 'profileImage', 'ratingsAverage'], - }) - .populate({ - path: 'passenger', - select: ['firstName', 'lastName', 'profileImage', 'ratingsAverage'], - }); + const trip = await Trip.findById(req.params.id); if (!trip) { return next(new AppError('No trip found with that ID', 404)); diff --git a/controllers/userController.js b/controllers/userController.js index 1cd5ac4..f5c09ba 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -1,9 +1,10 @@ +const mongoose = require('mongoose'); const User = require('../models/userModel'); const APIFeatures = require('../utils/apiFeatures'); const catchAsync = require('../utils/catchAsync'); const AppError = require('../utils/appError'); -exports.getAllUsers = catchAsync(async (req, res) => { +exports.getAllUsers = catchAsync(async (req, res, next) => { const features = new APIFeatures(User.find(), req.query) .filter() .sort() @@ -20,116 +21,80 @@ exports.getAllUsers = catchAsync(async (req, res) => { }); }); -exports.getUserByEmail = async (req, res) => { - try { - // Get the email from the query - console.log(req); - const { email } = req.query; - - // Validate the email - if (!email) { - return res.status(400).json({ - status: 'fail', - message: 'Email is required', - }); - } - - // Find the user by email - const user = await User.findOne({ email: email }); - - // Check if a user with the provided email exists - if (!user) { - return res.status(404).json({ - status: 'fail', - message: 'No user found with that email', - }); - } - - res.status(200).json({ - status: 'success', - data: { - user, - // userId: user._id, - }, - }); - } catch (err) { - res.status(500).json({ - status: 'error', - message: err.message, - }); - } -}; - -exports.getUser = async (req, res) => { - try { - const user = await User.findById(req.params.id); - - res.status(200).json({ - status: 'success', - data: { - user, - }, - }); - } catch (err) { - res.status(404).json({ - status: 'fail', - message: err, - }); +exports.getUserByEmail = catchAsync(async (req, res, next) => { + // Get the email from the query + const { email } = req.query; + + // Validate the email/email is provided + if (!email) { + return next(new AppError('Email is required', 400)); } -}; - -exports.createUser = async (req, res) => { - try { - const newUser = await User.create(req.body); - //console.log('In the create user'); - - res.status(201).json({ - status: 'success', - data: { - user: newUser, - }, - }); - } catch (err) { - res.status(400).json({ - status: 'fail', - message: err, - }); + + // Find the user by email + const user = await User.findOne({ email: email }); + + // Check if a user with the provided email exists + if (!user) { + return next(new AppError('No user found with that email', 404)); } -}; - -exports.updateUser = async (req, res) => { - try { - const user = await User.findByIdAndUpdate(req.params.id, req.body, { - new: true, - runValidators: true, - }); - - res.status(200).json({ - status: 'success', - data: { - user, - }, - }); - } catch (err) { - res.status(404).json({ - status: 'fail', - message: err, - }); + + res.status(200).json({ + status: 'success', + data: { + user, + }, + }); +}); + +exports.getUser = catchAsync(async (req, res, next) => { + const userId = req.params.id; + + if (!mongoose.Types.ObjectId.isValid(userId)) { + return next(new AppError('Invalid user ID', 400)); } -}; - -exports.deleteUser = async (req, res) => { - try { - await User.findByIdAndDelete(req.params.id); - - res.status(204).json({ - status: 'success', - data: null, - }); - } catch (err) { - res.status(404).json({ - status: 'fail', - message: err, - }); + const user = await User.findById(req.params.id); + + if (!user) { + return next(new AppError('No user found with that ID', 404)); } -}; + res.status(200).json({ + status: 'success', + data: { + user, + }, + }); +}); + +exports.createUser = catchAsync(async (req, res, next) => { + const newUser = await User.create(req.body); + + res.status(201).json({ + status: 'success', + data: { + user: newUser, + }, + }); +}); + +exports.updateUser = catchAsync(async (req, res, next) => { + const user = await User.findByIdAndUpdate(req.params.id, req.body, { + new: true, + runValidators: true, + }); + + res.status(200).json({ + status: 'success', + data: { + user, + }, + }); +}); + +exports.deleteUser = catchAsync(async (req, res) => { + await User.findByIdAndDelete(req.params.id); + + res.status(204).json({ + status: 'success', + data: null, + }); +});