diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 30f010c..af58d56 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -84,4 +84,28 @@ export async function logout(req: Request, res: Response) { console.error('Error during logout:', error); 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' }); + } } \ No newline at end of file diff --git a/src/routes/user.ts b/src/routes/user.ts index 729f56b..671a81d 100644 --- a/src/routes/user.ts +++ b/src/routes/user.ts @@ -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; \ No newline at end of file