2024-09-18 04:15:21 +00:00
|
|
|
const db = require("../../config/database")
|
|
|
|
|
|
|
|
const getUsersPaged = async (req, res) => {
|
|
|
|
try {
|
|
|
|
const page = parseInt(req.query.page) || 2
|
|
|
|
const pageSize = parseInt(req.query.size) || 10
|
|
|
|
const offset = (page - 1) * pageSize
|
|
|
|
|
|
|
|
const results = await executeQuery(
|
|
|
|
`SELECT * FROM forma LIMIT ${pageSize} OFFSET ${offset}`
|
|
|
|
)
|
|
|
|
|
|
|
|
return res.json({ content: results})
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
return res.status(500).json({
|
|
|
|
success: 0,
|
|
|
|
message: "Error fetching content from the contentbase",
|
|
|
|
error: error.message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getUsers = async (req, res) => {
|
|
|
|
try {
|
|
|
|
const results = await executeQuery("SELECT * FROM forma", [])
|
|
|
|
return res.json(results)
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
return res.status(500).json({
|
|
|
|
success: 0,
|
|
|
|
message: "Error fetching content from the contentbase",
|
|
|
|
error: error.message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getUsersByUserId = async (req, res) => {
|
|
|
|
const id = req.params.id
|
|
|
|
try {
|
|
|
|
const results = await executeQuery(
|
|
|
|
"SELECT * FROM forma WHERE id = ?",
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
if (!results || results.length === 0) {
|
|
|
|
return res.json({
|
|
|
|
success: 0,
|
|
|
|
message: "Record not found",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return res.json({
|
|
|
|
success: 1,
|
|
|
|
content: results,
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
return res.status(500).json({
|
|
|
|
success: 0,
|
|
|
|
message: "Error fetching content from the contentbase",
|
|
|
|
error: error.message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const createUser = async (req, res) => {
|
|
|
|
const body = req.body
|
|
|
|
try {
|
2024-09-18 09:57:37 +00:00
|
|
|
const params = [ body.percentages,
|
2024-09-18 04:15:21 +00:00
|
|
|
|
2024-09-18 09:57:37 +00:00
|
|
|
body.recaptchat,
|
2024-09-18 04:15:21 +00:00
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
// Replace undefined with null in parameters
|
|
|
|
const sanitizedParams = params.map((param) =>
|
|
|
|
param !== undefined ? param : null
|
|
|
|
)
|
|
|
|
|
|
|
|
await executeQuery(
|
|
|
|
`
|
|
|
|
INSERT INTO forma
|
2024-09-18 09:57:37 +00:00
|
|
|
SET percentages = ?,
|
2024-09-18 04:15:21 +00:00
|
|
|
|
2024-09-18 09:57:37 +00:00
|
|
|
recaptchat = ?
|
2024-09-18 04:15:21 +00:00
|
|
|
|
|
|
|
`,
|
|
|
|
sanitizedParams
|
|
|
|
)
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
success: 1,
|
|
|
|
content: "user created successfully",
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
return res.status(500).json({
|
|
|
|
success: 0,
|
|
|
|
message: "Error creating user",
|
|
|
|
error: error.message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const updateUsers = async (req, res) => {
|
|
|
|
const body = req.body;
|
|
|
|
const id = req.params.id;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Ensure that required parameters exist and replace undefined with null
|
2024-09-18 09:57:37 +00:00
|
|
|
const params = [body.percentages,
|
2024-09-18 04:15:21 +00:00
|
|
|
|
2024-09-18 09:57:37 +00:00
|
|
|
body.recaptchat,
|
2024-09-18 04:15:21 +00:00
|
|
|
|
|
|
|
id,
|
|
|
|
];
|
|
|
|
|
|
|
|
const sanitizedParams = params.map((param) => (param !== undefined ? param : null));
|
|
|
|
|
|
|
|
const results = await executeQuery(
|
|
|
|
`
|
|
|
|
UPDATE forma
|
2024-09-18 09:57:37 +00:00
|
|
|
SET percentages = ?,
|
2024-09-18 04:15:21 +00:00
|
|
|
|
2024-09-18 09:57:37 +00:00
|
|
|
recaptchat = ?
|
2024-09-18 04:15:21 +00:00
|
|
|
|
|
|
|
WHERE id = ?
|
|
|
|
`,
|
|
|
|
sanitizedParams
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!results) {
|
|
|
|
return res.json({
|
|
|
|
success: 0,
|
|
|
|
message: "Failed to update user",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
success: 1,
|
|
|
|
content: "Updated successfully",
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
return res.status(500).json({
|
|
|
|
success: 0,
|
|
|
|
message: "Error updating user",
|
|
|
|
error: error.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteUsers = async (req, res) => {
|
|
|
|
const id = req.params.id
|
|
|
|
try {
|
|
|
|
await executeQuery("DELETE FROM forma WHERE id = ?", [id])
|
|
|
|
return res.json({
|
|
|
|
success: 1,
|
|
|
|
content: "user deleted successfully",
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
return res.status(500).json({
|
|
|
|
success: 0,
|
|
|
|
message: "Error deleting user",
|
|
|
|
error: error.message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getUsers,
|
|
|
|
getUsersPaged,
|
|
|
|
getUsersByUserId,
|
|
|
|
createUser,
|
|
|
|
updateUsers,
|
|
|
|
deleteUsers,
|
|
|
|
}
|
|
|
|
|
|
|
|
async function executeQuery(sql, values) {
|
|
|
|
const connection = await db.getConnection()
|
|
|
|
try {
|
|
|
|
const [rows] = await connection.execute(sql, values)
|
|
|
|
return rows
|
|
|
|
} finally {
|
|
|
|
connection.release()
|
|
|
|
}
|
|
|
|
}
|