-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithoutMVC.js
More file actions
147 lines (113 loc) · 3.01 KB
/
Copy pathwithoutMVC.js
File metadata and controls
147 lines (113 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const express = require("express");
const mongoose = require("mongoose");
const { Schema } = mongoose;
const productRouter=express.Router()
const server = express();
conncetDB().catch((err) => console.log(err));
async function conncetDB() {
// async non blocking
await mongoose.connect("mongodb://127.0.0.1:27017/ecommerce");
console.log("connected to database");
}
const getProduct = async (req, res) => {
// get all products
try {
const _id = req.params.id;
const product = await Product.findById(_id);
res.status(201).json(product);
} catch (err) {
res.json({
success: false,
});
}
};
const getAllProducts = async (req, res) => {
// get all products
try {
const products = await Product.find();
res.status(201).json(products);
} catch (err) {
res.json({
success: false,
});
}
};
const saveProduct = async (req, res) => {
const product = new Product(req.body);
try {
await product.save();
res.status(201).json({
success: true,
});
} catch (err) {
res.json(err);
}
};
const replaceProduct = async (req, res) => {
const id = req.params.id;
try {
const oldProduct = await Product.findOneAndReplace({ id: id }, req.body);
res.status(201).json({
success: true,
});
} catch (err) {
res.json(err);
}
};
const updateProduct = async (req, res) => {
const id = req.params.id;
try {
const oldProduct = await Product.findOneAndUpdate({ id: id }, req.body);
res.status(201).json({
success: true,
});
} catch (err) {
res.json(err);
}
};
const deleteProduct = async (req, res) => {
const id = req.params.id;
try {
const oldProduct = await Product.findOneAndDelete({ id: id });
res.status(201).json({
success: true,
});
} catch (err) {
res.json(err);
}
};
const productSchema = new Schema({
id: { type: Number, required: true, unique: true },
productCategory: { type: String, required: true },
name: { type: String, required: true },
brand: { type: String, required: true },
description: { type: String, required: true },
basePrice: { type: Number, required: true },
inStock: { type: Boolean, required: true },
stock: {
type: Number,
min: [0, "Wrong Stock number"],
max: [50, "wrong max stock number"],
},
featuredImage: String,
thumbnailImage: String,
storageOptions: [String],
comments: [{ body: String, date: { type: Date, default: Date.now } }],
date: { type: Date, default: Date.now },
});
const Product = mongoose.model("Product", productSchema); // now Product is collection name using product schema
/*
const product = new Product()
product.name="My Phone"
product.brand="Chumber"
console.log(product)
*/
productRouter.use(express.json());
productRouter.get("/products", getAllProducts)
.get("/products/:id", getProduct)
.post("/", saveProduct)
.put("/products/:id", replaceProduct)
.patch("/products/:id", updateProduct)
.delete("/products/:id", deleteProduct)
server.use("/productApi", productRouter)
server.listen("8080")