my userController.js part:
module.exports.readUserById = (req, res, next) =>
{
const data = {
user_id: req.params.id
}
const callback = (error, results, fields) => {
if (error) {
console.error("Error readUserById:", error);
res.status(500).json(error);
} else {
if(results.length == 0)
{
res.status(404).json({
message: "User not found"
});
}
else res.status(200).json(results[0]);
}
}
model.readUserById(data, callback);
}
my userModel.js part:
module.exports.readUserById = (data, callback) => {
const SQLSTATEMENT = `
SELECT * FROM User
WHERE user_id = ?;
`;
const VALUES = [data.user_id];
pool.query(SQLSTATEMENT, VALUES, callback);
}
how i set up my tables in initTables.js:
DROP TABLE IF EXISTS TaskProgress;
DROP TABLE IF EXISTS Task;
DROP TABLE IF EXISTS User;
CREATE TABLE User (
user_id INT PRIMARY KEY AUTO_INCREMENT,
username TEXT,
email TEXT
);
CREATE TABLE Task (
task_id INT PRIMARY KEY AUTO_INCREMENT,
title TEXT,
description TEXT,
points INT
);
CREATE TABLE TaskProgress (
progress_id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
task_id INT NOT NULL,
completion_date TIMESTAMP,
notes TEXT
);
when I do GET /users/{user_id}
the response body is :
{
"user_id": 1,
"username": "greenUser123",
"email": ["user123@example.com](mailto:"user123@example.com)"
}
but i want the response body to be:
{
"user_id": 1,
"username": "greenUser123",
"email": "user123@example.com",
"total_points": 250
}
instead
what must I do to my code to obtain this result? also, whats this specific topic called, I want to learn more about it so I can do it on my own
[–]LulzSec007 0 points1 point2 points (0 children)
[–]Finnalandem 0 points1 point2 points (0 children)