Skip to content

Add product creation functionality to the API #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion model/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,23 @@

}

function create(product) {

Check failure on line 52 in model/products.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

model/products.js#L52

`meta.messages` must contain at least one violation message.

Check warning on line 52 in model/products.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

model/products.js#L52

`meta.schema` is required (use [] if rule has no schema).

Check failure on line 52 in model/products.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

model/products.js#L52

`meta.type` is required (must be either `problem`, `suggestion`, or `layout`).
var q = "INSERT INTO products(name, description, price) VALUES('" +
product.name + "', '" +
product.description + "', '" +
product.price +
"');";

return db.one(q);
}

var actions = {
"list": list_products,
"getProduct": getProduct,
"search": search,
"purchase": purchase,
"getPurchased": get_purcharsed
"getPurchased": get_purcharsed,
"create": create
}

module.exports = actions;
18 changes: 18 additions & 0 deletions routes/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,24 @@

});

router.all('/products/create', function(req, res, next) {

Check failure on line 147 in routes/products.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

routes/products.js#L147

'res' is defined but never used.

Check failure on line 147 in routes/products.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

routes/products.js#L147

'res' is defined but never used.
let params = null;
if (req.method == "GET"){
params = url.parse(req.url, true).query;
} else {
params = req.body;
}

let product = null;
product = {
name: params.name,
description: params.description,
price: params.price,
image: params.image,
username: req.session.user_name
}

db_products.create(product)
Copy link
Preview

Copilot AI Apr 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The product creation endpoint does not send a response back to the client after attempting to create the product. Consider adding proper response handling (e.g., res.send() or res.json()) to confirm the operation’s success or failure.

Suggested change
db_products.create(product)
db_products.create(product)
.then(function () {
res.json({ message: "Product created successfully" });
})
.catch(function (err) {
console.log(err);
res.status(500).json({ message: "Error creating product" });
});

Copilot uses AI. Check for mistakes.

});

module.exports = router;
Loading