getUserById

This commit is contained in:
p11037 2023-11-24 14:39:01 +07:00
parent 3ff828d9c9
commit f413d01bd9
2 changed files with 22 additions and 1 deletions

View file

@ -13,6 +13,25 @@ const getAllUser = asyncHandler(async (req, res) => {
res.status(200).json(users); res.status(200).json(users);
}); });
//Get single users => api/user/:id
const getUserById = async (req, res) => {
try {
const { id } = req.params;
const foundUser = await user.findById(id);
if (!foundUser) {
return res.status(404).json({ message: 'User not found' });
}
res.status(200).json(foundUser);
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Server Error' });
}
};
// Create new user => POST api/users/create // Create new user => POST api/users/create
const createUser = asyncHandler(async (req, res) => { const createUser = asyncHandler(async (req, res) => {
try { try {
@ -166,4 +185,4 @@ const googleAuth = asyncHandler(async (req, res) => {
} }
}); });
module.exports = { getAllUser, createUser, deleteUser, updateUser, loginUser, logoutUser , googleAuth}; module.exports = { getAllUser, getUserById, createUser, deleteUser, updateUser, loginUser, logoutUser , googleAuth};

View file

@ -1,6 +1,7 @@
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
const { getAllUser, const { getAllUser,
getUserById,
createUser, createUser,
deleteUser, deleteUser,
updateUser, updateUser,
@ -9,6 +10,7 @@ const { getAllUser,
googleAuth} = require('../controllers/userController'); googleAuth} = require('../controllers/userController');
router.route('/').get(getAllUser); router.route('/').get(getAllUser);
router.route('/:id').get(getUserById);
router.route('/create').post(createUser); router.route('/create').post(createUser);
router.route('/delete/:id').delete(deleteUser); router.route('/delete/:id').delete(deleteUser);
router.route('/update/:id').put(updateUser); router.route('/update/:id').put(updateUser);