56 lines
1.3 KiB
JavaScript
56 lines
1.3 KiB
JavaScript
const User = require("../models/user.model");
|
|
|
|
exports.test = async (req, res) => {
|
|
console.log("User route is working");
|
|
res.json({ message: "User route is working" });
|
|
};
|
|
|
|
exports.getAllUsers = async (req, res) => {
|
|
console.log(req);
|
|
const users = await User.getAllUsers();
|
|
res.json(users);
|
|
};
|
|
|
|
|
|
exports.updateUserRole = async (req, res) => {
|
|
try {
|
|
const { id, role } = req.body;
|
|
|
|
console.log(`Updating user ${id} to role ${role}`);
|
|
if (!Object.values(User.ROLES).includes(role))
|
|
return res.status(400).json({ error: "Invalid role" });
|
|
|
|
const updated = await User.updateUserRole(id, role);
|
|
if (!updated)
|
|
return res.status(404).json({ error: "User not found" });
|
|
|
|
res.json({ message: "Role updated", id, role });
|
|
} catch (err) {
|
|
res.status(500).json({ error: "Failed to update role" });
|
|
}
|
|
};
|
|
|
|
exports.deleteUser = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
|
|
const deleted = await User.deleteUser(id);
|
|
if (!deleted)
|
|
return res.status(404).json({ error: "User not found" });
|
|
|
|
|
|
res.json({ message: "User deleted", id });
|
|
} catch (err) {
|
|
res.status(500).json({ error: "Failed to delete user" });
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.checkIfUserExists = async (req, res) => {
|
|
const { username } = req.query;
|
|
const users = await User.checkIfUserExists(username);
|
|
res.json(users);
|
|
};
|