Skip to content

Commit 692e118

Browse files
committed
User Deleted Request Success
1 parent 74dbe72 commit 692e118

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

controller/userController.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// External Imports:
22
const bcrypt = require("bcrypt");
3+
const { unlink } = require("fs");
4+
const path = require("path");
35

46
// Internal Modules:
57
const User = require("../models/People");
68

7-
// Get User Page page
9+
// Get User
810
const getUsers = async (req, res, next) => {
911
try {
1012
const users = await User.find();
11-
console.log(users);
1213
res.render("users", {
1314
users: users,
1415
});
@@ -52,5 +53,33 @@ const addUser = async (req, res, next) => {
5253
}
5354
};
5455

56+
// Get User
57+
const deleteUser = async (req, res, next) => {
58+
try {
59+
const users = await User.findByIdAndDelete({ _id: req.params.id });
60+
61+
// Remove User Avatar if have:
62+
if (users.avatar) {
63+
unlink(
64+
path.join(__dirname, `/../public/uploads/avatars/${user.avatar}`),
65+
(err) => {
66+
if (err) console.log(err);
67+
}
68+
);
69+
}
70+
res.status(200).json({
71+
message: "User removed successfully!",
72+
});
73+
} catch (error) {
74+
res.status(500).json({
75+
errors: {
76+
common: {
77+
msg: "Could not delete the user!",
78+
},
79+
},
80+
});
81+
}
82+
};
83+
5584
// Module Export
56-
module.exports = { getUsers, addUser };
85+
module.exports = { getUsers, addUser, deleteUser };

router/userRouter.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ const express = require("express");
33
const router = express.Router();
44

55
// Internal Modules
6-
const { getUsers, addUser } = require("../controller/userController");
6+
const {
7+
getUsers,
8+
addUser,
9+
deleteUser,
10+
} = require("../controller/userController");
711
const decorateHtmlResponse = require("../middlewares/common/decorateHtmlResponse");
812
const avatarUpload = require("../middlewares/users/avatarUpload");
913
const {
@@ -14,6 +18,9 @@ const {
1418
// Get User Page
1519
router.get("/", decorateHtmlResponse("User"), getUsers);
1620

21+
// Delete User:
22+
router.delete("/:id", deleteUser);
23+
1724
// Add User With Avatar {Rest API}
1825
router.post(
1926
"/",

views/users.ejs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</td>
3232
<td><%= user.email %></td>
3333
<td class="manage">
34-
<img src="./images/trash.png" alt="Delete" />
34+
<img src="./images/trash.png" alt="Delete" onclick="deleteUser('<%= user._id %>')" />
3535
</td>
3636
</tr>
3737
<% }); %>
@@ -41,4 +41,33 @@
4141
</div>
4242
<%- include('./partials/add_user_modal.ejs') %>
4343
</body>
44+
<script>
45+
46+
// Toast Message Mannage
47+
const deleteToast = Toastify({
48+
text: "User Deleted Successfully!",
49+
duration:2000
50+
})
51+
52+
// Toast Message Mannage
53+
const deleteToastError = Toastify({
54+
text: "Could not deleted the user!",
55+
duration:2000
56+
})
57+
58+
// Delete User Request Mannage:
59+
const deleteUser = async(id)=>{
60+
let response = await fetch(`/users/${id}`, {method:"DELETE"});
61+
let result= await response.json();
62+
63+
console.log(result)
64+
65+
if(result.errors){
66+
deleteToastError.showToast();
67+
}else{
68+
deleteToast.showToast();
69+
document.getElementById(id).remove();
70+
}
71+
}
72+
</script>
4473
</html>

0 commit comments

Comments
 (0)