Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 16 additions & 18 deletions model/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,48 @@

function getProduct(product_id) {

var q = "SELECT * FROM products WHERE id = '" + product_id + "';";
var q = "SELECT * FROM products WHERE id = $1;";

return db.one(q);
return db.one(q, [product_id]);
}

function search(query) {

var q = "SELECT * FROM products WHERE name ILIKE '%" + query + "%' OR description ILIKE '%" + query + "%';";
var q = "SELECT * FROM products WHERE name ILIKE $1 OR description ILIKE $2;";

return db.many(q);
return db.many(q, ['%' + query + '%', '%' + query + '%']);

}

function purchase(cart) {

var q = "INSERT INTO purchases(mail, product_name, user_name, product_id, address, phone, ship_date, price) VALUES('" +
cart.mail + "', '" +
cart.product_name + "', '" +
cart.username + "', '" +
cart.product_id + "', '" +
cart.address + "', '" +
cart.ship_date + "', '" +
cart.phone + "', '" +
cart.price +
"');";
var q = "INSERT INTO purchases(mail, product_name, user_name, product_id, address, phone, ship_date, price) VALUES($1, $2, $3, $4, $5, $6, $7, $8);";

return db.one(q);
return db.one(q, [cart.mail, cart.product_name, cart.username, cart.product_id, cart.address, cart.phone, cart.ship_date, cart.price]);

}

function get_purcharsed(username) {

var q = "SELECT * FROM purchases WHERE user_name = '" + username + "';";
var q = "SELECT * FROM purchases WHERE user_name = $1;";

return db.many(q);
return db.many(q, [username]);

}

function create(product) {

Check warning on line 43 in model/products.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

model/products.js#L43

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

Check warning on line 43 in model/products.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

model/products.js#L43

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

Check warning on line 43 in model/products.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

model/products.js#L43

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

return db.one(q, [product.name, product.description, product.price]);
}

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

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

});

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

Check warning on line 147 in routes/products.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

routes/products.js#L147

'next' is defined but never used.

Check warning on line 147 in routes/products.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

routes/products.js#L147

'next' 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)
.then(function () {
res.json({ message: "Product created successfully" });
})
.catch(function (err) {
console.log(err);
res.status(500).json({ message: "Error creating product" });
});
});

module.exports = router;
Loading