From 1cdca39f6d4cbe73f6196b523a822a63e5ce5f8b Mon Sep 17 00:00:00 2001 From: Anisuzzaman Khan Date: Sat, 17 Nov 2018 23:56:30 +0600 Subject: [PATCH 1/8] tweaked get endpoints for product some dynamic query params --- router/product.js | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/router/product.js b/router/product.js index 0f1dafa..983294a 100644 --- a/router/product.js +++ b/router/product.js @@ -24,15 +24,45 @@ product_router.get('/tel', (req, res) => { }); }) +product_router.get('/teilla', (req, res) => { + const item = productModel.find({name : "teilla"},(err, result)=>{ + if(err || result.length == 0){ + res.send(404, {"message" : "pai nai", "error": err}) + // throw err; + } + else if(result){ + res.end("result paisi"); + } + }); +}) + product_router.get('/saban', (req, res) => { - const item = productModel.find({name : "saban"},(err, result)=>{ - if(err){ - throw err; - } - else if(result){ - res.json(result); - } - }); + const price_value = req.query.price + // console.log(req.query.price) + + final_res = {} + const item = productModel.findOne({name : "saban", mullo: price_value},(err, result)=>{ + if(err){ + throw err; + } + else if(result){ + final_res = result + } + + if (final_res) { + return res.send(200, { + "result" : { + "name": final_res.name, + "price": final_res.price, + "brand": final_res.brand, + "color": final_res.color, + "smell": final_res.smell, + } + }); + } else { + return res.send(404, {"message": "nai, sorry for waiting"}) + } + }); }) product_router.get('/lobon', (req, res) => { From 62af913e6997497551224c37c7d92808076010b0 Mon Sep 17 00:00:00 2001 From: Anisuzzaman Khan Date: Sun, 18 Nov 2018 00:04:37 +0600 Subject: [PATCH 2/8] lint --- router/product.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/router/product.js b/router/product.js index 983294a..074b814 100644 --- a/router/product.js +++ b/router/product.js @@ -10,7 +10,7 @@ product_router.get('/', (req, res) => { else if(result){ res.json(result); } - }); + }); }) product_router.get('/tel', (req, res) => { @@ -21,7 +21,7 @@ product_router.get('/tel', (req, res) => { else if(result){ res.json(result); } - }); + }); }) product_router.get('/teilla', (req, res) => { @@ -42,10 +42,10 @@ product_router.get('/saban', (req, res) => { final_res = {} const item = productModel.findOne({name : "saban", mullo: price_value},(err, result)=>{ - if(err){ + if (err) { throw err; } - else if(result){ + else if (result) { final_res = result } @@ -66,17 +66,17 @@ product_router.get('/saban', (req, res) => { }) product_router.get('/lobon', (req, res) => { - const item = productModel.find({name : "lobon"},(err, result)=>{ - if(err){ + const item = productModel.find({name : "lobon"}, (err, result) => { + if (err) { throw err; } - else if(result){ + else if (result) { res.json(result); } - }); + }); }) -product_router.post('/create', (req, res) => { +product_router.post('/create', (req, res) => { let item = new productModel({ "name": req.body.name, "price": req.body.price, @@ -89,13 +89,13 @@ product_router.post('/create', (req, res) => { "license": req.body.license, "smell": req.body.smell }); - + // error object definition let client_err = {} item.save( (err) => { if (err) client_err = err }) - + if (client_err) { return res.send(422, { "message": "tumi kichu ekta vul korso vai/bon", From eaf3f6eca174bc5a13a4f6dd757591825cff4bd3 Mon Sep 17 00:00:00 2001 From: Akram Khan Date: Sun, 18 Nov 2018 10:57:21 +0600 Subject: [PATCH 3/8] ignoring .idea folder --- .gitignore | 1 + package-lock.json | 122 +++++++++++++++++++++++----------------------- 2 files changed, 62 insertions(+), 61 deletions(-) diff --git a/.gitignore b/.gitignore index 3c3629e..34977ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.idea \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 313edbf..08c9201 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.5.tgz", "integrity": "sha1-63d99gEXI6OxTopywIBcjoZ0a9I=", "requires": { - "mime-types": "2.1.20", + "mime-types": "~2.1.18", "negotiator": "0.6.1" } }, @@ -23,7 +23,7 @@ "resolved": "https://registry.npmjs.org/async/-/async-2.6.1.tgz", "integrity": "sha512-fNEiL2+AZt6AlAw/29Cr0UDe4sRAHCpEHh54WMz+Bb7QfNcFw4h3loofyJpLeQs4Yx7yuqu/2dLgM5hKOs6HlQ==", "requires": { - "lodash": "4.17.11" + "lodash": "^4.17.10" } }, "bluebird": { @@ -37,15 +37,15 @@ "integrity": "sha1-WykhmP/dVTs6DyDe0FkrlWlVyLQ=", "requires": { "bytes": "3.0.0", - "content-type": "1.0.4", + "content-type": "~1.0.4", "debug": "2.6.9", - "depd": "1.1.2", - "http-errors": "1.6.3", + "depd": "~1.1.2", + "http-errors": "~1.6.3", "iconv-lite": "0.4.23", - "on-finished": "2.3.0", + "on-finished": "~2.3.0", "qs": "6.5.2", "raw-body": "2.3.3", - "type-is": "1.6.16" + "type-is": "~1.6.16" }, "dependencies": { "qs": { @@ -128,36 +128,36 @@ "resolved": "http://registry.npmjs.org/express/-/express-4.16.3.tgz", "integrity": "sha1-avilAjUNsyRuzEvs9rWjTSL37VM=", "requires": { - "accepts": "1.3.5", + "accepts": "~1.3.5", "array-flatten": "1.1.1", "body-parser": "1.18.2", "content-disposition": "0.5.2", - "content-type": "1.0.4", + "content-type": "~1.0.4", "cookie": "0.3.1", "cookie-signature": "1.0.6", "debug": "2.6.9", - "depd": "1.1.2", - "encodeurl": "1.0.2", - "escape-html": "1.0.3", - "etag": "1.8.1", + "depd": "~1.1.2", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", "finalhandler": "1.1.1", "fresh": "0.5.2", "merge-descriptors": "1.0.1", - "methods": "1.1.2", - "on-finished": "2.3.0", - "parseurl": "1.3.2", + "methods": "~1.1.2", + "on-finished": "~2.3.0", + "parseurl": "~1.3.2", "path-to-regexp": "0.1.7", - "proxy-addr": "2.0.4", + "proxy-addr": "~2.0.3", "qs": "6.5.1", - "range-parser": "1.2.0", + "range-parser": "~1.2.0", "safe-buffer": "5.1.1", "send": "0.16.2", "serve-static": "1.13.2", "setprototypeof": "1.1.0", - "statuses": "1.4.0", - "type-is": "1.6.16", + "statuses": "~1.4.0", + "type-is": "~1.6.16", "utils-merge": "1.0.1", - "vary": "1.1.2" + "vary": "~1.1.2" }, "dependencies": { "body-parser": { @@ -166,15 +166,15 @@ "integrity": "sha1-h2eKGdhLR9hZuDGZvVm84iKxBFQ=", "requires": { "bytes": "3.0.0", - "content-type": "1.0.4", + "content-type": "~1.0.4", "debug": "2.6.9", - "depd": "1.1.2", - "http-errors": "1.6.3", + "depd": "~1.1.1", + "http-errors": "~1.6.2", "iconv-lite": "0.4.19", - "on-finished": "2.3.0", + "on-finished": "~2.3.0", "qs": "6.5.1", "raw-body": "2.3.2", - "type-is": "1.6.16" + "type-is": "~1.6.15" } }, "iconv-lite": { @@ -206,7 +206,7 @@ "depd": "1.1.1", "inherits": "2.0.3", "setprototypeof": "1.0.3", - "statuses": "1.4.0" + "statuses": ">= 1.3.1 < 2" } }, "setprototypeof": { @@ -224,12 +224,12 @@ "integrity": "sha512-Y1GUDo39ez4aHAw7MysnUD5JzYX+WaIj8I57kO3aEPT1fFRL4sr7mjei97FgnwhAyyzRYmQZaTHb2+9uZ1dPtg==", "requires": { "debug": "2.6.9", - "encodeurl": "1.0.2", - "escape-html": "1.0.3", - "on-finished": "2.3.0", - "parseurl": "1.3.2", - "statuses": "1.4.0", - "unpipe": "1.0.0" + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "on-finished": "~2.3.0", + "parseurl": "~1.3.2", + "statuses": "~1.4.0", + "unpipe": "~1.0.0" } }, "forwarded": { @@ -247,10 +247,10 @@ "resolved": "http://registry.npmjs.org/http-errors/-/http-errors-1.6.3.tgz", "integrity": "sha1-i1VoC7S+KDoLW/TqLjhYC+HZMg0=", "requires": { - "depd": "1.1.2", + "depd": "~1.1.2", "inherits": "2.0.3", "setprototypeof": "1.1.0", - "statuses": "1.4.0" + "statuses": ">= 1.4.0 < 2" } }, "iconv-lite": { @@ -258,7 +258,7 @@ "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.23.tgz", "integrity": "sha512-neyTUVFtahjf0mB3dZT77u+8O0QB89jFdnBkd5P1JgYPbPaia3gXXOVL2fq8VyU2gMMD7SaN7QukTB/pmXYvDA==", "requires": { - "safer-buffer": "2.1.2" + "safer-buffer": ">= 2.1.2 < 3" } }, "inherits": { @@ -322,7 +322,7 @@ "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.20.tgz", "integrity": "sha512-HrkrPaP9vGuWbLK1B1FfgAkbqNjIuy4eHlIYnFi7kamZyLLrGlo2mpcx0bBmNpKqBtYtAfGbodDddIgddSJC2A==", "requires": { - "mime-db": "1.36.0" + "mime-db": "~1.36.0" } }, "mongodb": { @@ -331,7 +331,7 @@ "integrity": "sha512-E5QJuXQoMlT7KyCYqNNMfAkhfQD79AT4F8Xd+6x37OX+8BL17GyXyWvfm6wuyx4wnzCCPoCSLeMeUN2S7dU9yw==", "requires": { "mongodb-core": "3.1.5", - "safe-buffer": "5.1.2" + "safe-buffer": "^5.1.2" }, "dependencies": { "safe-buffer": { @@ -346,10 +346,10 @@ "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.1.5.tgz", "integrity": "sha512-emT/tM4ZBinqd6RZok+EzDdtN4LjYJIckv71qQVOEFmvXgT5cperZegVmTgox/1cx4XQu6LJ5ZuIwipP/eKdQg==", "requires": { - "bson": "1.1.0", - "require_optional": "1.0.1", - "safe-buffer": "5.1.2", - "saslprep": "1.0.2" + "bson": "^1.1.0", + "require_optional": "^1.0.1", + "safe-buffer": "^5.1.2", + "saslprep": "^1.0.0" }, "dependencies": { "bson": { @@ -370,7 +370,7 @@ "integrity": "sha512-DIUWOyYgZv2zGi/BoFEaFiaCVuDonnzGhW3cnc3JFjBScYn6z24tS2j3VB0dtMoX8FFjxmmMVnlmHPEIbV4PKA==", "requires": { "async": "2.6.1", - "bson": "1.0.9", + "bson": "~1.0.5", "kareem": "2.3.0", "lodash.get": "4.4.2", "mongodb": "3.1.6", @@ -461,7 +461,7 @@ "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.4.tgz", "integrity": "sha512-5erio2h9jp5CHGwcybmxmVqHmnCBZeewlfJ0pex+UW7Qny7OOZXTtH56TGNyBizkgiOwhJtMKrVzDTeKcySZwA==", "requires": { - "forwarded": "0.1.2", + "forwarded": "~0.1.2", "ipaddr.js": "1.8.0" } }, @@ -496,8 +496,8 @@ "resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz", "integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==", "requires": { - "resolve-from": "2.0.0", - "semver": "5.6.0" + "resolve-from": "^2.0.0", + "semver": "^5.1.0" } }, "resolve-from": { @@ -521,7 +521,7 @@ "integrity": "sha512-4cDsYuAjXssUSjxHKRe4DTZC0agDwsCqcMqtJAQPzC74nJ7LfAJflAtC1Zed5hMzEQKj82d3tuzqdGNRsLJ4Gw==", "optional": true, "requires": { - "sparse-bitfield": "3.0.3" + "sparse-bitfield": "^3.0.3" } }, "semver": { @@ -535,18 +535,18 @@ "integrity": "sha512-E64YFPUssFHEFBvpbbjr44NCLtI1AohxQ8ZSiJjQLskAdKuriYEP6VyGEsRDH8ScozGpkaX1BGvhanqCwkcEZw==", "requires": { "debug": "2.6.9", - "depd": "1.1.2", - "destroy": "1.0.4", - "encodeurl": "1.0.2", - "escape-html": "1.0.3", - "etag": "1.8.1", + "depd": "~1.1.2", + "destroy": "~1.0.4", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", "fresh": "0.5.2", - "http-errors": "1.6.3", + "http-errors": "~1.6.2", "mime": "1.4.1", "ms": "2.0.0", - "on-finished": "2.3.0", - "range-parser": "1.2.0", - "statuses": "1.4.0" + "on-finished": "~2.3.0", + "range-parser": "~1.2.0", + "statuses": "~1.4.0" } }, "serve-static": { @@ -554,9 +554,9 @@ "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.13.2.tgz", "integrity": "sha512-p/tdJrO4U387R9oMjb1oj7qSMaMfmOyd4j9hOFoxZe2baQszgHcSWjuya/CiT5kgZZKRudHNOA0pYXOl8rQ5nw==", "requires": { - "encodeurl": "1.0.2", - "escape-html": "1.0.3", - "parseurl": "1.3.2", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "parseurl": "~1.3.2", "send": "0.16.2" } }, @@ -576,7 +576,7 @@ "integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=", "optional": true, "requires": { - "memory-pager": "1.1.0" + "memory-pager": "^1.0.2" } }, "statuses": { @@ -590,7 +590,7 @@ "integrity": "sha512-HRkVv/5qY2G6I8iab9cI7v1bOIdhm94dVjQCPFElW9W+3GeDOSHmy2EBYe4VTApuzolPcmgFTN3ftVJRKR2J9Q==", "requires": { "media-typer": "0.3.0", - "mime-types": "2.1.20" + "mime-types": "~2.1.18" } }, "unpipe": { From 2f91050d5403ee18efee5d73020c865ffb4951c7 Mon Sep 17 00:00:00 2001 From: Akram Khan Date: Sun, 18 Nov 2018 12:24:39 +0600 Subject: [PATCH 4/8] dynamic product searching feature added --- router/product.js | 91 +++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 55 deletions(-) diff --git a/router/product.js b/router/product.js index 074b814..fd0d294 100644 --- a/router/product.js +++ b/router/product.js @@ -13,67 +13,48 @@ product_router.get('/', (req, res) => { }); }) -product_router.get('/tel', (req, res) => { - const item = productModel.find({name : "tel"},(err, result)=>{ - if(err){ - throw err; - } - else if(result){ - res.json(result); - } - }); -}) - -product_router.get('/teilla', (req, res) => { - const item = productModel.find({name : "teilla"},(err, result)=>{ - if(err || result.length == 0){ - res.send(404, {"message" : "pai nai", "error": err}) - // throw err; - } - else if(result){ - res.end("result paisi"); - } - }); -}) - -product_router.get('/saban', (req, res) => { - const price_value = req.query.price - // console.log(req.query.price) - - final_res = {} - const item = productModel.findOne({name : "saban", mullo: price_value},(err, result)=>{ +product_router.get('/:name', (req, res) => { + let query = { + name: req.params.name, + } + let q = req.query + if (q.price) { + query.price = q.price + } + if (q.brand) { + query.brand = q.brand + } + if (q.quantity) { + query.quantity = q.quantity + } + if (q.size) { + query.size = q.size + } + if (q.unit) { + query.unit = q.unit + } + if (q.color) { + query.color = q.color + } + productModel.findOne(query, (err, result) => { if (err) { throw err; } else if (result) { - final_res = result + res.json({ + "name": result.name, + "price": result.price, + "brand": result.brand, + "quantity": result.quantity, + "size": result.size, + "unit": result.unit, + "color": result.color, + }) } - - if (final_res) { - return res.send(200, { - "result" : { - "name": final_res.name, - "price": final_res.price, - "brand": final_res.brand, - "color": final_res.color, - "smell": final_res.smell, - } - }); - } else { - return res.send(404, {"message": "nai, sorry for waiting"}) + else { + res.status(404).json({"message": "Sorry, product not found."}) } - }); -}) - -product_router.get('/lobon', (req, res) => { - const item = productModel.find({name : "lobon"}, (err, result) => { - if (err) { - throw err; - } - else if (result) { - res.json(result); - } - }); + }) }) product_router.post('/create', (req, res) => { From b814d791366cba180f6bb04f641ee73ca2f09ae0 Mon Sep 17 00:00:00 2001 From: Anisuzzaman Khan Date: Sat, 24 Nov 2018 19:09:46 +0600 Subject: [PATCH 5/8] added update and delete method --- router/product.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/router/product.js b/router/product.js index fd0d294..958c363 100644 --- a/router/product.js +++ b/router/product.js @@ -13,6 +13,7 @@ product_router.get('/', (req, res) => { }); }) +// get product product_router.get('/:name', (req, res) => { let query = { name: req.params.name, @@ -57,6 +58,7 @@ product_router.get('/:name', (req, res) => { }) }) +// create product product_router.post('/create', (req, res) => { let item = new productModel({ "name": req.body.name, @@ -86,4 +88,32 @@ product_router.post('/create', (req, res) => { return res.send(201, {"message": "item jog hoyeche, apnake dhonnobad!"}) }) +// update product +product_router.patch('/:product_id', (req, res) => { + const reqBody = req.body + + productModel.updateOne({_id: req.params.product_id}, reqBody, (err, result) => { + if (err) { + return res.send(400, {message: "something went wrong!"}) + } + + if (result) { + return res.send(200, {message: "successfully updated", data: result}) + } + }) +}) + +// delete product +product_router.delete('/:product_id', (req, res) => { + productModel.deleteOne({_id: req.params.product_id}, (err, result) => { + if (err) { + return res.send(400, {message: "something went wrong!"}) + } + + if (result) { + return res.send(204) + } + }) +}) + module.exports = product_router \ No newline at end of file From a77370ac23c2a492be0bfc7adb209fef70fe51c1 Mon Sep 17 00:00:00 2001 From: Anisuzzaman Khan Date: Sat, 1 Dec 2018 18:33:07 +0600 Subject: [PATCH 6/8] added npm script --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 9931b4b..7b3af11 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "A shop you can get item for free", "main": "index.js", "scripts": { + "start": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { From f55375d3140fd9f6a0b9b5e2c308381d4610465b Mon Sep 17 00:00:00 2001 From: Anisuzzaman Khan Date: Sat, 1 Dec 2018 18:45:30 +0600 Subject: [PATCH 7/8] set dynamic PORT --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 49a0ecf..8973eec 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ const express = require('express') const app = express() const bodyParser = require('body-parser') const mongoose = require('mongoose') -const port = 3001 +const port = process.env.PORT || 80 // connect mongoDB // mongodb://:@:/ From 57193a88b77122deb928b36ef7c72d8087ccc298 Mon Sep 17 00:00:00 2001 From: Anisuzzaman Khan Date: Sat, 1 Dec 2018 19:47:23 +0600 Subject: [PATCH 8/8] fixed critical save issue related to async task --- router/product.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/router/product.js b/router/product.js index 958c363..0fc98c5 100644 --- a/router/product.js +++ b/router/product.js @@ -74,18 +74,16 @@ product_router.post('/create', (req, res) => { }); // error object definition - let client_err = {} item.save( (err) => { - if (err) client_err = err + if (err) { + console.log(client_err) + return res.send(422, { + "message": "tumi kichu ekta vul korso vai/bon", + "error": client_err + }) + } + return res.send(201, {"message": "item jog hoyeche, apnake dhonnobad!"}) }) - - if (client_err) { - return res.send(422, { - "message": "tumi kichu ekta vul korso vai/bon", - "error": client_err - }) - } - return res.send(201, {"message": "item jog hoyeche, apnake dhonnobad!"}) }) // update product