narinder1231 commited on
Commit
5e8365d
·
1 Parent(s): b5dcc75

expose an api to delete user

Browse files
src/controllers/user.controller.ts CHANGED
@@ -194,4 +194,24 @@ const updateUserById = async (req: Request, res: Response) => {
194
  }
195
  };
196
 
197
- export { createUser, getUserById, getAllUsers, updateUserById };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  }
195
  };
196
 
197
+ const deleteUserById = async (req: Request, res: Response) => {
198
+ try {
199
+ const userId = parseInt(req.params.id, 10);
200
+
201
+ const deletedRowCount = await User.destroy({
202
+ where: { id: userId },
203
+ });
204
+
205
+ if (deletedRowCount === 0) {
206
+ return res.status(404).json({ error: 'User not found' });
207
+ }
208
+
209
+ return res.status(204).send();
210
+
211
+ } catch (error) {
212
+ console.error('Error deleting user:', error);
213
+ return res.status(500).json({ error: 'Error while deleting User.' });
214
+ }
215
+ };
216
+
217
+ export { createUser, getUserById, getAllUsers, deleteUserById, updateUserById };
src/routes/users.ts CHANGED
@@ -1,5 +1,5 @@
1
  import express from "express";
2
- import { createUser, getUserById, getAllUsers, updateUserById} from "../controllers/user.controller";
3
  import { validateUser, validateUserUpdate } from "../validators/user.validator";
4
  import { handleValidationErrors } from "../middlewares/handleValidatorError";
5
 
@@ -9,5 +9,6 @@ userRouter.post("/", validateUser, handleValidationErrors , createUser);
9
  userRouter.get("/:id", getUserById );
10
  userRouter.get("/", getAllUsers);
11
  userRouter.put("/:id",validateUserUpdate, handleValidationErrors, updateUserById);
 
12
 
13
  export default userRouter;
 
1
  import express from "express";
2
+ import { createUser, getUserById, getAllUsers, updateUserById, deleteUserById} from "../controllers/user.controller";
3
  import { validateUser, validateUserUpdate } from "../validators/user.validator";
4
  import { handleValidationErrors } from "../middlewares/handleValidatorError";
5
 
 
9
  userRouter.get("/:id", getUserById );
10
  userRouter.get("/", getAllUsers);
11
  userRouter.put("/:id",validateUserUpdate, handleValidationErrors, updateUserById);
12
+ userRouter.delete("/:id", deleteUserById);
13
 
14
  export default userRouter;