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/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://:@:/ 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": { 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": { diff --git a/router/product.js b/router/product.js index 0f1dafa..0fc98c5 100644 --- a/router/product.js +++ b/router/product.js @@ -10,43 +10,56 @@ product_router.get('/', (req, res) => { else if(result){ res.json(result); } - }); -}) - -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('/saban', (req, res) => { - const item = productModel.find({name : "saban"},(err, result)=>{ - if(err){ - throw err; +// get product +product_router.get('/:name', (req, res) => { + let query = { + name: req.params.name, } - else if(result){ - res.json(result); + let q = req.query + if (q.price) { + query.price = q.price } - }); -}) - -product_router.get('/lobon', (req, res) => { - const item = productModel.find({name : "lobon"},(err, result)=>{ - if(err){ + 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){ - res.json(result); + else if (result) { + res.json({ + "name": result.name, + "price": result.price, + "brand": result.brand, + "quantity": result.quantity, + "size": result.size, + "unit": result.unit, + "color": result.color, + }) + } + else { + res.status(404).json({"message": "Sorry, product not found."}) } - }); + }) }) -product_router.post('/create', (req, res) => { +// create product +product_router.post('/create', (req, res) => { let item = new productModel({ "name": req.body.name, "price": req.body.price, @@ -59,20 +72,46 @@ 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 (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 +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