upd
This commit is contained in:
@@ -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;
|
||||
Reference in New Issue
Block a user