diff --git a/model/products.js b/model/products.js index 6df3f921..93660039 100644 --- a/model/products.js +++ b/model/products.js @@ -49,12 +49,23 @@ function get_purcharsed(username) { } +function create(product) { + 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; diff --git a/routes/products.js b/routes/products.js index 814f834b..4d5d1fb6 100644 --- a/routes/products.js +++ b/routes/products.js @@ -144,6 +144,24 @@ router.all('/products/buy', function(req, res, next) { }); +router.all('/products/create', function(req, res, next) { + 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) +}); module.exports = router;