deleteUser + get all routes
This commit is contained in:
parent
5723e3516c
commit
68d0c2600f
2 changed files with 28 additions and 2 deletions
|
@ -85,3 +85,27 @@ export async function logout(req: Request, res: Response) {
|
|||
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' });
|
||||
}
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
import express from 'express';
|
||||
import { createUser, login, logout } from '../controllers/UserController';
|
||||
import { createUser, login, logout, getAllUsers, deleteUser } from '../controllers/UserController';
|
||||
|
||||
const userRouter = express.Router();
|
||||
|
||||
userRouter.post('/', createUser);
|
||||
userRouter.post('/login', login);
|
||||
userRouter.post('/logout', logout);
|
||||
userRouter.get('/all', getAllUsers);
|
||||
userRouter.delete('/:id', deleteUser)
|
||||
|
||||
export default userRouter;
|
Loading…
Reference in a new issue