costco-grocery-list/backend/controllers/users.controller.js
Nico d9c4a2caf9 Made everything pretty
Fix registration
2025-11-23 00:12:17 -08:00

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 } = req.params;
const { role } = req.body;
if (!Object.values(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);
};