Skip to content

Commit

Permalink
chore: Add create product functionality patch task
Browse files Browse the repository at this point in the history
  • Loading branch information
austenstone committed Aug 15, 2024
1 parent 96c60f2 commit 89a6a3a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Apply products_patch.diff",
"type": "shell",
"command": "git apply products_patch.diff",
"problemMatcher": []
}
]
}
58 changes: 58 additions & 0 deletions products_patch.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
diff --git a/model/products.js b/model/products.js
index 6df3f92..9366003 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 814f834..4d5d1fb 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;

0 comments on commit 89a6a3a

Please sign in to comment.