deleteUser + get all routes

This commit is contained in:
Kfir Dayan 2023-06-14 14:32:16 +03:00
parent 5723e3516c
commit 68d0c2600f
2 changed files with 28 additions and 2 deletions

View file

@ -84,4 +84,28 @@ export async function logout(req: Request, res: Response) {
console.error('Error during logout:', error); console.error('Error during logout:', error);
res.status(500).json({ error: 'An error occurred during logout' }); res.status(500).json({ error: 'An error occurred during logout' });
} }
}
export async function getAllUsers(req: Request, res: Response) {
try {
const users = await User.find().select('-__v -password');
res.status(200).json({ users });
} catch (error) {
console.error('Error getting all users:', error);
res.status(500).json({ error: 'An error occurred while getting all users' });
}
}
export async function deleteUser(req: Request, res: Response) {
try {
const { id } = req.params;
const user = await User.findByIdAndDelete(id);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
res.status(200).json({ message: 'User deleted successfully' });
} catch (error) {
console.error('Error deleting user:', error);
res.status(500).json({ error: 'An error occurred while deleting the user' });
}
} }

View file

@ -1,10 +1,12 @@
import express from 'express'; import express from 'express';
import { createUser, login, logout } from '../controllers/UserController'; import { createUser, login, logout, getAllUsers, deleteUser } from '../controllers/UserController';
const userRouter = express.Router(); const userRouter = express.Router();
userRouter.post('/', createUser); userRouter.post('/', createUser);
userRouter.post('/login', login); userRouter.post('/login', login);
userRouter.post('/logout', logout); userRouter.post('/logout', logout);
userRouter.get('/all', getAllUsers);
userRouter.delete('/:id', deleteUser)
export default userRouter; export default userRouter;