This commit is contained in:
2024-08-04 18:01:17 +05:00
parent 19bf521825
commit f0bd42a81d
36 changed files with 1194 additions and 116 deletions
@@ -0,0 +1,62 @@
import bcrypt from "bcrypt";
import { Router } from "express";
import User from "../../models/User.js";
const router = Router();
router.get("/", async (req, res) => {
try {
const result = await User.find(req.query);
res.json(result);
} catch (error) {
res.json({ error: (error as Error).message });
}
});
router.get("/:id", async (req, res) => {
try {
const result = await User.findById(req.params.id);
res.json(result);
} catch (error) {
res.json({ error: (error as Error).message });
}
});
router.post("/", async (req, res) => {
try {
const passwordHash = bcrypt.hashSync(req.body.password, 12);
const result = await User.create({ ...req.body, password: passwordHash });
res.json(result);
} catch (error) {
res.json({ error: (error as Error).message });
}
});
router.put("/:id", async (req, res) => {
try {
const result = await User.findByIdAndUpdate(req.params.id, req.body, {
new: true,
});
res.json(result);
} catch (error) {
res.json({ error: (error as Error).message });
}
});
router.delete("/:id", async (req, res) => {
try {
const result = await User.findByIdAndRemove(req.params.id);
res.json(result);
} catch (error) {
res.json({ error: (error as Error).message });
}
});
const adminUsersRoute = router;
export default adminUsersRoute;