ID-specific operations
This commit is contained in:
@@ -4,18 +4,17 @@ const { Op } = require("sequelize");
|
||||
const { User } = require("../models");
|
||||
const router = express.Router();
|
||||
|
||||
let validationSchema = yup.object({
|
||||
id: yup.number().min(0).required(),
|
||||
firstName: yup.string().trim().min(1).max(100).required(),
|
||||
lastName: yup.string().trim().min(1).max(100).required(),
|
||||
email: yup.string().trim().min(5).max(69).email().required(),
|
||||
phoneNumber: yup.string().trim().length(8).required(),
|
||||
passwordHash: yup.string().trim().min(128).max(255).required(),
|
||||
});
|
||||
|
||||
router.post("/", async (req, res) => {
|
||||
let data = req.body;
|
||||
// Validate request body
|
||||
let validationSchema = yup.object({
|
||||
id: yup.number().min(0).required(),
|
||||
firstName: yup.string().trim().min(1).max(100).required(),
|
||||
lastName: yup.string().trim().min(1).max(100).required(),
|
||||
email: yup.string().trim().min(5).max(69).email().required(),
|
||||
phoneNumber: yup.string().trim().length(8).required(),
|
||||
passwordHash: yup.string().trim().min(128).max(255).required(),
|
||||
description: yup.string().trim().min(3).max(500).required(),
|
||||
});
|
||||
try {
|
||||
data = await validationSchema.validate(data, { abortEarly: false });
|
||||
// Process valid data
|
||||
@@ -43,4 +42,62 @@ router.get("/", async (req, res) => {
|
||||
res.json(list);
|
||||
});
|
||||
|
||||
router.get("/:id", async (req, res) => {
|
||||
let id = req.params.id;
|
||||
let user = await User.findByPk(id);
|
||||
if (!user) {
|
||||
res.sendStatus(404);
|
||||
return;
|
||||
}
|
||||
res.json(user);
|
||||
});
|
||||
|
||||
router.put("/:id", async (req, res) => {
|
||||
let id = req.params.id;
|
||||
let user = await User.findByPk(id);
|
||||
|
||||
if (!user) {
|
||||
res.sendStatus(404);
|
||||
return;
|
||||
}
|
||||
|
||||
let data = req.body;
|
||||
|
||||
try {
|
||||
data = await validationSchema.validate(data, { abortEarly: false });
|
||||
|
||||
let num = await User.update(data, {
|
||||
where: { id: id },
|
||||
});
|
||||
|
||||
if (num == 1) {
|
||||
res.json({
|
||||
message: "User was updated successfully.",
|
||||
});
|
||||
} else {
|
||||
res.status(400).json({
|
||||
message: `Cannot update tutorial with id ${id}.`,
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
res.status(400).json({ errors: err.errors });
|
||||
}
|
||||
});
|
||||
|
||||
router.delete("/:id", async (req, res) => {
|
||||
let id = req.params.id;
|
||||
let num = await User.destroy({
|
||||
where: { id: id },
|
||||
});
|
||||
if (num == 1) {
|
||||
res.json({
|
||||
message: "User was deleted successfully.",
|
||||
});
|
||||
} else {
|
||||
res.status(400).json({
|
||||
message: `Cannot delete user with id ${id}.`,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user