├── db ├── config.js ├── User.js └── Product.js ├── .gitignore ├── package.json └── index.js /db/config.js: -------------------------------------------------------------------------------- 1 | const mongoose= require('mongoose'); 2 | mongoose.connect("mongodb://localhost:27017/e-commerce") -------------------------------------------------------------------------------- /db/User.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | 3 | const userSchema = new mongoose.Schema({ 4 | name: String, 5 | email: String, 6 | password: String 7 | }); 8 | 9 | module.exports = mongoose.model("users", userSchema); -------------------------------------------------------------------------------- /db/Product.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | 3 | const productSchema = new mongoose.Schema({ 4 | name: String, 5 | price: String, 6 | category: String, 7 | userId:String, 8 | company:String 9 | }); 10 | 11 | module.exports = mongoose.model("products", productSchema); -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "e-dashboard", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "nodemon", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "cors": "^2.8.5", 14 | "express": "^4.17.1", 15 | "jsonwebtoken": "^8.5.1", 16 | "mongoose": "^6.0.13", 17 | "nodemon": "^2.0.14" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const cors = require("cors"); 3 | require("./db/config"); 4 | const User = require('./db/User'); 5 | const Product = require("./db/Product") 6 | const Jwt = require('jsonwebtoken'); 7 | const jwtKey = 'e-com'; 8 | const app = express(); 9 | 10 | app.use(express.json()); 11 | app.use(cors()); 12 | 13 | app.post("/register", async (req, resp) => { 14 | let user = new User(req.body); 15 | let result = await user.save(); 16 | result = result.toObject(); 17 | delete result.password 18 | Jwt.sign({result}, jwtKey, {expiresIn:"2h"},(err,token)=>{ 19 | if(err){ 20 | resp.send("Something went wrong") 21 | } 22 | resp.send({result,auth:token}) 23 | }) 24 | }) 25 | 26 | app.post("/login", async (req, resp) => { 27 | if (req.body.password && req.body.email) { 28 | let user = await User.findOne(req.body).select("-password"); 29 | if (user) { 30 | Jwt.sign({user}, jwtKey, {expiresIn:"2h"},(err,token)=>{ 31 | if(err){ 32 | resp.send("Something went wrong") 33 | } 34 | resp.send({user,auth:token}) 35 | }) 36 | } else { 37 | resp.send({ result: "No User found" }) 38 | } 39 | } else { 40 | resp.send({ result: "No User found" }) 41 | } 42 | }); 43 | 44 | app.post("/add-product", async (req, resp) => { 45 | let product = new Product(req.body); 46 | let result = await product.save(); 47 | resp.send(result); 48 | }); 49 | 50 | app.get("/products", async (req, resp) => { 51 | const products = await Product.find(); 52 | if (products.length > 0) { 53 | resp.send(products) 54 | } else { 55 | resp.send({ result: "No Product found" }) 56 | } 57 | }); 58 | 59 | app.delete("/product/:id", async (req, resp) => { 60 | let result = await Product.deleteOne({ _id: req.params.id }); 61 | resp.send(result) 62 | }), 63 | 64 | app.get("/product/:id", async (req, resp) => { 65 | let result = await Product.findOne({ _id: req.params.id }) 66 | if (result) { 67 | resp.send(result) 68 | } else { 69 | resp.send({ "result": "No Record Found." }) 70 | } 71 | }) 72 | 73 | app.put("/product/:id", async (req, resp) => { 74 | let result = await Product.updateOne( 75 | { _id: req.params.id }, 76 | { $set: req.body } 77 | ) 78 | resp.send(result) 79 | }); 80 | 81 | app.put("/product/:id", async (req, resp) => { 82 | let result = await Product.updateOne( 83 | { _id: req.params.id }, 84 | { $set: req.body } 85 | ) 86 | resp.send(result) 87 | }); 88 | 89 | app.get("/search/:key", async (req, resp) => { 90 | let result = await Product.find({ 91 | "$or": [ 92 | { 93 | name: { $regex: req.params.key } 94 | }, 95 | { 96 | company: { $regex: req.params.key } 97 | }, 98 | { 99 | category: { $regex: req.params.key } 100 | } 101 | ] 102 | }); 103 | resp.send(result); 104 | }) 105 | 106 | app.listen(5000); --------------------------------------------------------------------------------